Search in sources :

Example 86 with MCRException

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);
    }
}
Also used : MCRException(org.mycore.common.MCRException) MCRException(org.mycore.common.MCRException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SAXParseException(org.xml.sax.SAXParseException) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 87 with MCRException

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);
    }
}
Also used : MCRException(org.mycore.common.MCRException) MCRException(org.mycore.common.MCRException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SAXParseException(org.xml.sax.SAXParseException) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 88 with MCRException

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);
}
Also used : MCRException(org.mycore.common.MCRException) NoResultException(javax.persistence.NoResultException) MCRException(org.mycore.common.MCRException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 89 with MCRException

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);
}
Also used : MCRException(org.mycore.common.MCRException) MCRSession(org.mycore.common.MCRSession) MCRUserInformation(org.mycore.common.MCRUserInformation)

Example 90 with MCRException

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;
}
Also used : MCRException(org.mycore.common.MCRException) MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID)

Aggregations

MCRException (org.mycore.common.MCRException)131 IOException (java.io.IOException)39 Element (org.jdom2.Element)26 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)19 Document (org.jdom2.Document)18 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)18 File (java.io.File)15 MCRConfigurationException (org.mycore.common.config.MCRConfigurationException)12 MCRObject (org.mycore.datamodel.metadata.MCRObject)12 ArrayList (java.util.ArrayList)11 JDOMException (org.jdom2.JDOMException)11 MCRAccessException (org.mycore.access.MCRAccessException)11 MCRPath (org.mycore.datamodel.niofs.MCRPath)10 SAXException (org.xml.sax.SAXException)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)7 List (java.util.List)7 MCRActiveLinkException (org.mycore.datamodel.common.MCRActiveLinkException)7 SAXParseException (org.xml.sax.SAXParseException)7 URI (java.net.URI)6 Path (java.nio.file.Path)6