Search in sources :

Example 1 with FieldRequiredValidationError

use of org.summerb.approaches.validation.errors.FieldRequiredValidationError in project summerb by skarpushin.

the class PasswordServiceImpl method setUserPassword.

@Override
@Transactional(rollbackFor = Throwable.class)
public void setUserPassword(String userUuid, String newPasswordPlain) throws UserNotFoundException, FieldValidationException {
    Preconditions.checkArgument(userUuid != null);
    Preconditions.checkArgument(newPasswordPlain != null);
    assertUserExists(userUuid);
    if (!StringUtils.hasText(newPasswordPlain)) {
        throw new FieldValidationException(new FieldRequiredValidationError(FN_PASSWORD));
    }
    String newPasswordHash = null;
    try {
        newPasswordHash = encodePassword(newPasswordPlain);
        // sanity check
        if (!isPasswordMatch(newPasswordPlain, newPasswordHash)) {
            throw new RuntimeException("Password doesn't match just created hash");
        }
        // set user password
        int updateResult = passwordDao.updateUserPassword(userUuid, newPasswordHash);
        if (updateResult < 1) {
            throw new RuntimeException("updateUserPassword returned unexpected result = " + updateResult);
        }
    } catch (Throwable t) {
        String msg = String.format("Failed to set user '%s' passwordHash '%s'", userUuid, newPasswordHash);
        throw new UserServiceUnexpectedException(msg, t);
    }
}
Also used : FieldValidationException(org.summerb.approaches.validation.FieldValidationException) UserServiceUnexpectedException(org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException) FieldRequiredValidationError(org.summerb.approaches.validation.errors.FieldRequiredValidationError) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Transactional (org.springframework.transaction.annotation.Transactional)1 FieldValidationException (org.summerb.approaches.validation.FieldValidationException)1 FieldRequiredValidationError (org.summerb.approaches.validation.errors.FieldRequiredValidationError)1 UserServiceUnexpectedException (org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException)1