Search in sources :

Example 1 with PasswordDoesNotMatchException

use of org.craftercms.studio.api.v1.exception.security.PasswordDoesNotMatchException 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);
    }
}
Also used : UserExternallyManagedException(org.craftercms.studio.api.v1.exception.security.UserExternallyManagedException) PasswordDoesNotMatchException(org.craftercms.studio.api.v1.exception.security.PasswordDoesNotMatchException) User(org.craftercms.studio.api.v2.dal.User) HashMap(java.util.HashMap) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) PasswordRequirementsFailedException(org.craftercms.studio.api.v2.exception.PasswordRequirementsFailedException) RetryingOperation(org.craftercms.studio.api.v2.annotation.RetryingOperation)

Aggregations

HashMap (java.util.HashMap)1 ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)1 PasswordDoesNotMatchException (org.craftercms.studio.api.v1.exception.security.PasswordDoesNotMatchException)1 UserExternallyManagedException (org.craftercms.studio.api.v1.exception.security.UserExternallyManagedException)1 RetryingOperation (org.craftercms.studio.api.v2.annotation.RetryingOperation)1 User (org.craftercms.studio.api.v2.dal.User)1 PasswordRequirementsFailedException (org.craftercms.studio.api.v2.exception.PasswordRequirementsFailedException)1