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();
}
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")));
}
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!");
}
}
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;
}
}
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();
}
Aggregations