Search in sources :

Example 61 with MCRException

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

the class MCRUserCommands method exportAllUserToDirectory.

@MCRCommand(syntax = "export all users to directory {0}", help = "Exports the data of all users to the directory {0}.")
public static List<String> exportAllUserToDirectory(String directory) throws IOException {
    File dir = new File(directory);
    if (!dir.exists() || !dir.isDirectory()) {
        throw new MCRException("Directory does not exist: " + dir.getAbsolutePath());
    }
    List<MCRUser> users = MCRUserManager.listUsers(null, null, null);
    ArrayList<String> commands = new ArrayList<>(users.size());
    for (MCRUser user : users) {
        File userFile = new File(dir, user.getUserID() + ".xml");
        commands.add("export user " + user.getUserID() + " to file " + userFile.getAbsolutePath());
    }
    return commands;
}
Also used : MCRException(org.mycore.common.MCRException) ArrayList(java.util.ArrayList) File(java.io.File) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 62 with MCRException

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

the class MCRUserManager method createUser.

/**
 * Creates and stores a new login user in the database.
 * This will also store role membership information.
 *
 * @param user the user to create in the database.
 */
public static void createUser(MCRUser user) {
    if (isInvalidUser(user)) {
        throw new MCRException("User is invalid: " + user.getUserID());
    }
    if (user instanceof MCRTransientUser) {
        createUser((MCRTransientUser) user);
        return;
    }
    EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
    em.persist(user);
    LOGGER.info(() -> "user saved: " + user.getUserID());
    MCRRoleManager.storeRoleAssignments(user);
}
Also used : MCRException(org.mycore.common.MCRException) EntityManager(javax.persistence.EntityManager)

Example 63 with MCRException

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

the class MCRUserManager method updateUser.

/**
 * Updates an existing login user in the database.
 * This will also update role membership information.
 *
 * @param user the user to update in the database.
 */
public static void updateUser(MCRUser user) {
    if (isInvalidUser(user)) {
        throw new MCRException("User is invalid: " + user.getUserID());
    }
    EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
    Optional<MCRUser> inDb = getByNaturalID(em, user.getUserName(), user.getRealmID());
    if (!inDb.isPresent()) {
        createUser(user);
        return;
    }
    inDb.ifPresent(db -> {
        user.internalID = db.internalID;
        em.detach(db);
        em.merge(user);
        MCRRoleManager.unassignRoles(user);
        MCRRoleManager.storeRoleAssignments(user);
    });
}
Also used : MCRException(org.mycore.common.MCRException) EntityManager(javax.persistence.EntityManager)

Example 64 with MCRException

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

the class MCRUserManager method checkPassword.

/**
 * Returns a {@link MCRUser} instance if the login succeeds.
 * This method will return <code>null</code> if the user does not exist or the login is disabled.
 * If the {@link MCRUser#getHashType()} is {@link MCRPasswordHashType#crypt}, {@link MCRPasswordHashType#md5} or {@link MCRPasswordHashType#sha1}
 * the hash value is automatically upgraded to {@link MCRPasswordHashType#sha256}.
 * @param userName Name of the user to login.
 * @param password clear text password.
 * @return authenticated {@link MCRUser} instance or <code>null</code>.
 */
public static MCRUser checkPassword(String userName, String password) {
    MCRUser user = getUser(userName);
    if (user == null || user.getHashType() == null) {
        LOGGER.warn(() -> "User not found: " + userName);
        waitLoginPanalty();
        return null;
    }
    if (!user.loginAllowed()) {
        if (user.isDisabled()) {
            LOGGER.warn("User {} was disabled!", user.getUserID());
        } else {
            LOGGER.warn("Password expired for user {} on {}", user.getUserID(), MCRXMLFunctions.getISODate(user.getValidUntil(), MCRISO8601Format.COMPLETE_HH_MM_SS.toString()));
        }
        return null;
    }
    try {
        switch(user.getHashType()) {
            case crypt:
                // Wahh! did we ever thought about what "salt" means for passwd management?
                String passwdHash = user.getPassword();
                String salt = passwdHash.substring(0, 3);
                if (!MCRUtils.asCryptString(salt, password).equals(passwdHash)) {
                    // login failed
                    waitLoginPanalty();
                    return null;
                }
                // update to SHA-256
                updatePasswordHashToSHA256(user, password);
                break;
            case md5:
                if (!MCRUtils.asMD5String(1, null, password).equals(user.getPassword())) {
                    waitLoginPanalty();
                    return null;
                }
                // update to SHA-256
                updatePasswordHashToSHA256(user, password);
                break;
            case sha1:
                if (!MCRUtils.asSHA1String(HASH_ITERATIONS, Base64.getDecoder().decode(user.getSalt()), password).equals(user.getPassword())) {
                    waitLoginPanalty();
                    return null;
                }
                // update to SHA-256
                updatePasswordHashToSHA256(user, password);
                break;
            case sha256:
                if (!MCRUtils.asSHA256String(HASH_ITERATIONS, Base64.getDecoder().decode(user.getSalt()), password).equals(user.getPassword())) {
                    waitLoginPanalty();
                    return null;
                }
                break;
            default:
                throw new MCRException("Cannot validate hash type " + user.getHashType());
        }
    } catch (NoSuchAlgorithmException e) {
        throw new MCRException("Error while validating login", e);
    }
    return user;
}
Also used : MCRException(org.mycore.common.MCRException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 65 with MCRException

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

the class MCRSwordContainerHandler method deleteObject.

public void deleteObject(MCRObject object) throws SwordServerException {
    try {
        object.getStructure().getDerivates().stream().map(MCRMetaLinkID::getXLinkHrefID).forEach(id -> {
            try {
                MCRMetadataManager.deleteMCRDerivate(id);
            } catch (Exception e) {
                throw new MCRException(e);
            }
        });
        MCRMetadataManager.delete(object);
    } catch (MCRActiveLinkException | MCRAccessException | MCRException e) {
        Throwable ex = e;
        if (e instanceof MCRException && Optional.ofNullable(e.getCause()).map(Object::getClass).filter(MCRAccessException.class::isAssignableFrom).isPresent()) {
            // unwrapp
            ex = e.getCause();
        }
        throw new SwordServerException("Error while deleting Object.", ex);
    }
}
Also used : MCRException(org.mycore.common.MCRException) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException) SwordServerException(org.swordapp.server.SwordServerException) MCRAccessException(org.mycore.access.MCRAccessException) MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRException(org.mycore.common.MCRException) SwordServerException(org.swordapp.server.SwordServerException) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException) MCRAccessException(org.mycore.access.MCRAccessException)

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