use of org.mycore.common.MCRException in project mycore by MyCoRe-Org.
the class MCRUserCommands method unassignUserFromRole.
/**
* This method removes a member user from a role
*
* @param userID
* the ID of the user which will be removed from the role represented by roleID
* @param roleID
* the ID of the role from which the user with ID mbrUserID will be removed
*/
@MCRCommand(syntax = "unassign user {0} from role {1}", help = "Removes the user {0} as secondary member from the role {1}.", order = 130)
public static void unassignUserFromRole(String userID, String roleID) throws MCRException {
try {
MCRUser user = MCRUserManager.getUser(userID);
user.unassignRole(roleID);
MCRUserManager.updateUser(user);
} catch (Exception e) {
throw new MCRException("Error while unassigning " + userID + " from role " + roleID + ".", e);
}
}
use of org.mycore.common.MCRException in project mycore by MyCoRe-Org.
the class MCRUserCommands method assignUserToRole.
/**
* This method adds a user as a member to a role
*
* @param userID
* the ID of the user which will be a member of the role represented by roleID
* @param roleID
* the ID of the role to which the user with ID mbrUserID will be added
*/
@MCRCommand(syntax = "assign user {0} to role {1}", help = "Adds a user {0} as secondary member in the role {1}.", order = 120)
public static void assignUserToRole(String userID, String roleID) throws MCRException {
try {
MCRUser user = MCRUserManager.getUser(userID);
user.assignRole(roleID);
MCRUserManager.updateUser(user);
} catch (Exception e) {
throw new MCRException("Error while assigning " + userID + " to role " + roleID + ".", e);
}
}
use of org.mycore.common.MCRException in project mycore by MyCoRe-Org.
the class MCRUserManager method updatePasswordHashToSHA256.
static void updatePasswordHashToSHA256(MCRUser user, String password) {
String newHash;
byte[] salt = generateSalt();
try {
newHash = MCRUtils.asSHA256String(HASH_ITERATIONS, salt, password);
} catch (Exception e) {
throw new MCRException("Could not update user password hash to SHA-256.", e);
}
user.setSalt(Base64.getEncoder().encodeToString(salt));
user.setHashType(MCRPasswordHashType.sha256);
user.setPassword(newHash);
}
use of org.mycore.common.MCRException in project mycore by MyCoRe-Org.
the class MCRUserManager method setPassword.
/**
* Sets password of 'user' to 'password'.
*
* Automatically updates the user in database.
*/
public static void setPassword(MCRUser user, String password) {
MCRSession session = MCRSessionMgr.getCurrentSession();
MCRUserInformation currentUser = session.getUserInformation();
// only update password
MCRUser myUser = getUser(user.getUserName(), user.getRealmID());
boolean allowed = MCRAccessManager.checkPermission(MCRUser2Constants.USER_ADMIN_PERMISSION) || currentUser.equals(myUser.getOwner()) || (currentUser.equals(user) && myUser.hasNoOwner() || !myUser.isLocked());
if (!allowed) {
throw new MCRException("You are not allowed to change password of user: " + user);
}
updatePasswordHashToSHA256(myUser, password);
updateUser(myUser);
}
use of org.mycore.common.MCRException in project mycore by MyCoRe-Org.
the class MCRObjectCommands method processFromFile.
/**
* Load or update an MCRObject's from an XML file.
*
* @param file
* the location of the xml file
* @param update
* if true, object will be updated, else object is created
* @param importMode
* if true, servdates are taken from xml file
* @throws SAXParseException
* unable to build the mycore object from the file's URI
* @throws MCRException
* the parent of the given object does not exists
* @throws MCRAccessException
* if write permission is missing
*/
private static boolean processFromFile(File file, boolean update, boolean importMode) throws MCRException, SAXParseException, IOException, MCRAccessException {
if (!file.getName().endsWith(".xml")) {
LOGGER.warn("{} ignored, does not end with *.xml", file);
return false;
}
if (!file.isFile()) {
LOGGER.warn("{} ignored, is not a file.", file);
return false;
}
LOGGER.info("Reading file {} ...", file);
MCRObject mcrObject = new MCRObject(file.toURI());
if (mcrObject.hasParent()) {
MCRObjectID parentID = mcrObject.getStructure().getParentID();
if (!MCRMetadataManager.exists(mcrObject.getStructure().getParentID())) {
throw new MCRException("The parent object " + parentID + "does not exist for " + mcrObject + ".");
}
}
mcrObject.setImportMode(importMode);
LOGGER.debug("Label --> {}", mcrObject.getLabel());
if (update) {
MCRMetadataManager.update(mcrObject);
LOGGER.info("{} updated.", mcrObject.getId());
} else {
MCRMetadataManager.create(mcrObject);
LOGGER.info("{} loaded.", mcrObject.getId());
}
return true;
}
Aggregations