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