use of org.craftercms.studio.api.v2.exception.PasswordRequirementsFailedException in project studio by craftercms.
the class UserServiceInternalImpl method setUserPassword.
@RetryingOperation
@Override
public boolean setUserPassword(String username, String newPassword) throws UserNotFoundException, UserExternallyManagedException, ServiceLayerException {
if (!userExists(-1, username)) {
throw new UserNotFoundException();
} else {
if (verifyPasswordRequirements(newPassword)) {
Map<String, Object> params = new HashMap<String, Object>();
params.put(USER_ID, -1);
params.put(USERNAME, username);
try {
User user = userDao.getUserByIdOrUsername(params);
if (user.isExternallyManaged()) {
throw new UserExternallyManagedException();
} else {
String hashedPassword = CryptoUtils.hashPassword(newPassword);
params = new HashMap<String, Object>();
params.put(USERNAME, username);
params.put(PASSWORD, hashedPassword);
userDao.setUserPassword(params);
return true;
}
} catch (Exception e) {
throw new ServiceLayerException("Unknown database error", e);
}
} else {
throw new PasswordRequirementsFailedException("User password does not fulfill requirements");
}
}
}
use of org.craftercms.studio.api.v2.exception.PasswordRequirementsFailedException in project studio by craftercms.
the class UserServiceInternalImpl method createUser.
@Override
public User createUser(User user) throws UserAlreadyExistsException, ServiceLayerException {
if (userExists(-1, user.getUsername())) {
throw new UserAlreadyExistsException("User '" + user.getUsername() + "' already exists");
}
if (user.isExternallyManaged() || verifyPasswordRequirements(user.getPassword())) {
Map<String, Object> params = new HashMap<>();
params.put(USERNAME, user.getUsername());
params.put(PASSWORD, CryptoUtils.hashPassword(user.getPassword()));
params.put(FIRST_NAME, user.getFirstName());
params.put(LAST_NAME, user.getLastName());
params.put(EMAIL, user.getEmail());
params.put(EXTERNALLY_MANAGED, user.getExternallyManagedAsInt());
params.put(TIMEZONE, StringUtils.EMPTY);
params.put(LOCALE, StringUtils.EMPTY);
params.put(ENABLED, user.getEnabledAsInt());
try {
userDao.createUser(params);
user.setId((Long) params.get(ID));
return user;
} catch (Exception e) {
throw new ServiceLayerException("Unknown database error", e);
}
} else {
throw new PasswordRequirementsFailedException();
}
}
use of org.craftercms.studio.api.v2.exception.PasswordRequirementsFailedException in project studio by craftercms.
the class UserServiceInternalImpl method changePassword.
@RetryingOperation
@Override
public boolean changePassword(String username, String current, String newPassword) throws PasswordDoesNotMatchException, UserExternallyManagedException, ServiceLayerException {
Map<String, Object> params = new HashMap<String, Object>();
params.put(USER_ID, -1);
params.put(USERNAME, username);
try {
User user = userDao.getUserByIdOrUsername(params);
if (user.isExternallyManaged()) {
throw new UserExternallyManagedException();
} else {
if (CryptoUtils.matchPassword(user.getPassword(), current)) {
if (verifyPasswordRequirements(newPassword)) {
String hashedPassword = CryptoUtils.hashPassword(newPassword);
params = new HashMap<>();
params.put(USERNAME, username);
params.put(PASSWORD, hashedPassword);
userDao.setUserPassword(params);
return true;
} else {
throw new PasswordRequirementsFailedException();
}
} else {
throw new PasswordDoesNotMatchException();
}
}
} catch (RuntimeException e) {
throw new ServiceLayerException("Unknown database error", e);
}
}
Aggregations