Search in sources :

Example 1 with PasswordRequirementsFailedException

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

Example 2 with PasswordRequirementsFailedException

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();
    }
}
Also used : HashMap(java.util.HashMap) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) UserAlreadyExistsException(org.craftercms.studio.api.v1.exception.security.UserAlreadyExistsException) PasswordDoesNotMatchException(org.craftercms.studio.api.v1.exception.security.PasswordDoesNotMatchException) UserAlreadyExistsException(org.craftercms.studio.api.v1.exception.security.UserAlreadyExistsException) UserNotFoundException(org.craftercms.studio.api.v1.exception.security.UserNotFoundException) UserExternallyManagedException(org.craftercms.studio.api.v1.exception.security.UserExternallyManagedException) PasswordRequirementsFailedException(org.craftercms.studio.api.v2.exception.PasswordRequirementsFailedException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) PasswordRequirementsFailedException(org.craftercms.studio.api.v2.exception.PasswordRequirementsFailedException)

Example 3 with 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);
    }
}
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)3 ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)3 PasswordDoesNotMatchException (org.craftercms.studio.api.v1.exception.security.PasswordDoesNotMatchException)3 UserExternallyManagedException (org.craftercms.studio.api.v1.exception.security.UserExternallyManagedException)3 PasswordRequirementsFailedException (org.craftercms.studio.api.v2.exception.PasswordRequirementsFailedException)3 UserAlreadyExistsException (org.craftercms.studio.api.v1.exception.security.UserAlreadyExistsException)2 UserNotFoundException (org.craftercms.studio.api.v1.exception.security.UserNotFoundException)2 RetryingOperation (org.craftercms.studio.api.v2.annotation.RetryingOperation)2 User (org.craftercms.studio.api.v2.dal.User)2