Search in sources :

Example 6 with MCRISO8601Date

use of org.mycore.datamodel.common.MCRISO8601Date in project mycore by MyCoRe-Org.

the class MCRObjectFactory method getDateString.

private static String getDateString(Date date) {
    MCRISO8601Date isoDate = new MCRISO8601Date();
    isoDate.setDate(date);
    return isoDate.getISOString();
}
Also used : MCRISO8601Date(org.mycore.datamodel.common.MCRISO8601Date)

Example 7 with MCRISO8601Date

use of org.mycore.datamodel.common.MCRISO8601Date in project mycore by MyCoRe-Org.

the class MCRUserServlet method saveUser.

/**
 * Handles MCRUserServlet?action=save&id={userID}.
 * This is called by user-editor.xml editor form to save the
 * changed user data from editor submission. Redirects to
 * show user data afterwards.
 */
private void saveUser(HttpServletRequest req, HttpServletResponse res) throws Exception {
    MCRUser currentUser = MCRUserManager.getCurrentUser();
    if (!checkUserIsNotNull(res, currentUser, null)) {
        return;
    }
    boolean hasAdminPermission = MCRAccessManager.checkPermission(MCRUser2Constants.USER_ADMIN_PERMISSION);
    boolean allowed = hasAdminPermission || MCRAccessManager.checkPermission(MCRUser2Constants.USER_CREATE_PERMISSION);
    if (!allowed) {
        String msg = MCRTranslation.translate("component.user2.UserServlet.noCreatePermission");
        res.sendError(HttpServletResponse.SC_FORBIDDEN, msg);
        return;
    }
    Document doc = (Document) (req.getAttribute("MCRXEditorSubmission"));
    Element u = doc.getRootElement();
    String userName = u.getAttributeValue("name");
    String realmID = MCRRealmFactory.getLocalRealm().getID();
    if (hasAdminPermission) {
        realmID = u.getAttributeValue("realm");
    }
    MCRUser user;
    boolean userExists = MCRUserManager.exists(userName, realmID);
    if (!userExists) {
        user = new MCRUser(userName, realmID);
        LOGGER.info("create new user {} {}", userName, realmID);
        // For new local users, set password
        String pwd = u.getChildText("password");
        if ((pwd != null) && (pwd.trim().length() > 0) && user.getRealm().equals(MCRRealmFactory.getLocalRealm())) {
            MCRUserManager.updatePasswordHashToSHA256(user, pwd);
        }
    } else {
        user = MCRUserManager.getUser(userName, realmID);
        if (!(hasAdminPermission || currentUser.equals(user) || currentUser.equals(user.getOwner()))) {
            res.sendError(HttpServletResponse.SC_FORBIDDEN);
            return;
        }
    }
    XPathExpression<Attribute> hintPath = XPathFactory.instance().compile("password/@hint", Filters.attribute());
    Attribute hintAttr = hintPath.evaluateFirst(u);
    String hint = hintAttr == null ? null : hintAttr.getValue();
    if ((hint != null) && (hint.trim().length() == 0)) {
        hint = null;
    }
    user.setHint(hint);
    updateBasicUserInfo(u, user);
    if (hasAdminPermission) {
        boolean locked = "true".equals(u.getAttributeValue("locked"));
        user.setLocked(locked);
        boolean disabled = "true".equals(u.getAttributeValue("disabled"));
        user.setDisabled(disabled);
        Element o = u.getChild("owner");
        if (o != null && !o.getAttributes().isEmpty()) {
            String ownerName = o.getAttributeValue("name");
            String ownerRealm = o.getAttributeValue("realm");
            MCRUser owner = MCRUserManager.getUser(ownerName, ownerRealm);
            if (!checkUserIsNotNull(res, owner, ownerName + "@" + ownerRealm)) {
                return;
            }
            user.setOwner(owner);
        } else {
            user.setOwner(null);
        }
        String validUntilText = u.getChildTextTrim("validUntil");
        if (validUntilText == null || validUntilText.length() == 0) {
            user.setValidUntil(null);
        } else {
            String dateInUTC = validUntilText;
            if (validUntilText.length() == 10) {
                dateInUTC = convertToUTC(validUntilText, "yyyy-MM-dd");
            }
            MCRISO8601Date date = new MCRISO8601Date(dateInUTC);
            user.setValidUntil(date.getDate());
        }
    } else {
        // save read user of creator
        user.setRealm(MCRRealmFactory.getLocalRealm());
        user.setOwner(currentUser);
    }
    Element gs = u.getChild("roles");
    if (gs != null) {
        user.getSystemRoleIDs().clear();
        user.getExternalRoleIDs().clear();
        List<Element> groupList = gs.getChildren("role");
        for (Element group : groupList) {
            String groupName = group.getAttributeValue("name");
            if (hasAdminPermission || currentUser.isUserInRole(groupName)) {
                user.assignRole(groupName);
            } else {
                LOGGER.warn("Current user {} has not the permission to add user to group {}", currentUser.getUserID(), groupName);
            }
        }
    }
    if (userExists) {
        MCRUserManager.updateUser(user);
    } else {
        MCRUserManager.createUser(user);
    }
    res.sendRedirect(res.encodeRedirectURL("MCRUserServlet?action=show&id=" + URLEncoder.encode(user.getUserID(), "UTF-8")));
}
Also used : Attribute(org.jdom2.Attribute) Element(org.jdom2.Element) Document(org.jdom2.Document) MCRISO8601Date(org.mycore.datamodel.common.MCRISO8601Date)

Example 8 with MCRISO8601Date

use of org.mycore.datamodel.common.MCRISO8601Date in project mycore by MyCoRe-Org.

the class MCRCreateDateDOIGenerator method generate.

@Override
public MCRDigitalObjectIdentifier generate(MCRObjectID mcrID, String additional) throws MCRPersistentIdentifierException {
    Date createdate = MCRMetadataManager.retrieveMCRObject(mcrID).getService().getDate("createdate");
    if (createdate != null) {
        MCRISO8601Date mcrdate = new MCRISO8601Date();
        mcrdate.setDate(createdate);
        String format = mcrdate.format(DATE_PATTERN, Locale.ENGLISH);
        Optional<MCRDigitalObjectIdentifier> parse = mcrdoiParser.parse(prefix + "/" + format);
        MCRPersistentIdentifier doi = parse.get();
        return (MCRDigitalObjectIdentifier) doi;
    } else {
        throw new MCRPersistenceException("The object " + mcrID + " doesn't have a createdate!");
    }
}
Also used : MCRISO8601Date(org.mycore.datamodel.common.MCRISO8601Date) MCRPersistentIdentifier(org.mycore.pi.MCRPersistentIdentifier) MCRPersistenceException(org.mycore.common.MCRPersistenceException) MCRISO8601Date(org.mycore.datamodel.common.MCRISO8601Date) Date(java.util.Date)

Example 9 with MCRISO8601Date

use of org.mycore.datamodel.common.MCRISO8601Date in project mycore by MyCoRe-Org.

the class MCRMODSEmbargoUtils method isAfterToday.

private static boolean isAfterToday(final String embargoDate) {
    try {
        final MCRISO8601Date isoED = new MCRISO8601Date(embargoDate);
        final LocalDate now = Instant.now().atZone(ZoneId.systemDefault()).toLocalDate();
        final LocalDate ed = LocalDate.from(isoED.getDt());
        return ed.isAfter(now);
    } catch (DateTimeException ex) {
        return embargoDate.compareTo(MCRISO8601Date.now().getISOString()) > 0;
    }
}
Also used : DateTimeException(java.time.DateTimeException) MCRISO8601Date(org.mycore.datamodel.common.MCRISO8601Date) LocalDate(java.time.LocalDate)

Example 10 with MCRISO8601Date

use of org.mycore.datamodel.common.MCRISO8601Date in project mycore by MyCoRe-Org.

the class MCRXMLFunctions method getISODate.

public static String getISODate(Date date, String isoFormat) {
    MCRISO8601Date mcrdate = new MCRISO8601Date();
    mcrdate.setDate(date);
    mcrdate.setFormat(isoFormat);
    return mcrdate.getISOString();
}
Also used : MCRISO8601Date(org.mycore.datamodel.common.MCRISO8601Date)

Aggregations

MCRISO8601Date (org.mycore.datamodel.common.MCRISO8601Date)10 Date (java.util.Date)4 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)3 Document (org.jdom2.Document)2 Element (org.jdom2.Element)2 MCRCategoryID (org.mycore.datamodel.classifications2.MCRCategoryID)2 ProviderMismatchException (java.nio.file.ProviderMismatchException)1 DateTimeException (java.time.DateTimeException)1 LocalDate (java.time.LocalDate)1 HashSet (java.util.HashSet)1 Locale (java.util.Locale)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 Attribute (org.jdom2.Attribute)1 MCRCache (org.mycore.common.MCRCache)1 MCRPersistenceException (org.mycore.common.MCRPersistenceException)1 MCRCategLinkReference (org.mycore.datamodel.classifications2.MCRCategLinkReference)1 MCRCategory (org.mycore.datamodel.classifications2.MCRCategory)1 MCRPath (org.mycore.datamodel.niofs.MCRPath)1 MCRPersistentIdentifier (org.mycore.pi.MCRPersistentIdentifier)1