Search in sources :

Example 21 with FieldValidationException

use of org.summerb.approaches.validation.FieldValidationException in project summerb by skarpushin.

the class UsersServiceFacadeImpl method assertPasswordResetOperationValid.

protected String assertPasswordResetOperationValid(String email, String passwordResetToken, PasswordReset resetPasswordRequest) throws FieldValidationException, UserNotFoundException, GenericException {
    validatePasswordReset(resetPasswordRequest);
    try {
        validateUserIsEligableForPasswordReset(email);
    } catch (FieldValidationException fve) {
        throw new GenericException(CommonMessageCodes.ERROR_UNEXPECTED, fve);
    }
    User user = userService.getUserByEmail(email);
    String userUuid = user.getUuid();
    boolean isValid = passwordService.isRestorationTokenValid(userUuid, passwordResetToken);
    if (!isValid) {
        throw new GenericException(SecurityMessageCodes.INVALID_PASSWORD_RESET_TOKEN);
    }
    return userUuid;
}
Also used : FieldValidationException(org.summerb.approaches.validation.FieldValidationException) User(org.summerb.microservices.users.api.dto.User) GenericException(org.summerb.utils.exceptions.GenericException)

Example 22 with FieldValidationException

use of org.summerb.approaches.validation.FieldValidationException in project summerb by skarpushin.

the class UsersServiceFacadeImpl method changePassword.

@Override
public void changePassword(String email, PasswordChange passwordChange) throws UserNotFoundException, FieldValidationException {
    try {
        User user = validatePasswordChangeRequestValid(email, passwordChange);
        passwordService.setUserPassword(user.getUuid(), passwordChange.getPassword());
    } catch (Throwable e) {
        Throwables.throwIfInstanceOf(e, FieldValidationException.class);
        throw new UserServiceUnexpectedException("Failed to arrange password reset", e);
    }
}
Also used : FieldValidationException(org.summerb.approaches.validation.FieldValidationException) User(org.summerb.microservices.users.api.dto.User) UserServiceUnexpectedException(org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException)

Example 23 with FieldValidationException

use of org.summerb.approaches.validation.FieldValidationException in project summerb by skarpushin.

the class AuthTokenServiceImpl method createAuthToken.

@Override
@Transactional(rollbackFor = Throwable.class)
public AuthToken createAuthToken(String userEmail, String clientIp, String tokenUuid, String tokenValueUuid) throws UserNotFoundException, FieldValidationException {
    Preconditions.checkArgument(userEmail != null);
    Preconditions.checkArgument(clientIp != null);
    Preconditions.checkArgument(StringUtils.hasText(tokenUuid));
    Preconditions.checkArgument(StringUtils.hasText(tokenValueUuid));
    try {
        User user = userService.getUserByEmail(userEmail);
        AuthToken authToken = buildNewAuthToken(user, clientIp, tokenUuid, tokenValueUuid);
        authTokenDao.createAuthToken(authToken);
        return authToken;
    } catch (Throwable t) {
        Throwables.throwIfInstanceOf(t, UserNotFoundException.class);
        Throwables.throwIfInstanceOf(t, FieldValidationException.class);
        String msg = String.format("Failed to create auth otken for user '%s'", userEmail);
        throw new UserServiceUnexpectedException(msg, t);
    }
}
Also used : UserNotFoundException(org.summerb.microservices.users.api.exceptions.UserNotFoundException) FieldValidationException(org.summerb.approaches.validation.FieldValidationException) User(org.summerb.microservices.users.api.dto.User) UserServiceUnexpectedException(org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException) AuthToken(org.summerb.microservices.users.api.dto.AuthToken) Transactional(org.springframework.transaction.annotation.Transactional)

Example 24 with FieldValidationException

use of org.summerb.approaches.validation.FieldValidationException in project summerb by skarpushin.

the class AuthTokenServiceImpl method validateAndGetUser.

private User validateAndGetUser(String userEmail, String passwordPlain) throws UserNotFoundException, FieldValidationException, InvalidPasswordException {
    try {
        User user = userService.getUserByEmail(userEmail);
        boolean isPasswordValid = passwordService.isUserPasswordValid(user.getUuid(), passwordPlain);
        if (!isPasswordValid) {
            throw new InvalidPasswordException();
        }
        return user;
    } catch (Throwable t) {
        Throwables.throwIfInstanceOf(t, UserNotFoundException.class);
        Throwables.throwIfInstanceOf(t, FieldValidationException.class);
        Throwables.throwIfInstanceOf(t, InvalidPasswordException.class);
        String msg = String.format("Failed to validate user '%s' and password '%s'", userEmail, passwordPlain);
        throw new UserServiceUnexpectedException(msg, t);
    }
}
Also used : UserNotFoundException(org.summerb.microservices.users.api.exceptions.UserNotFoundException) FieldValidationException(org.summerb.approaches.validation.FieldValidationException) User(org.summerb.microservices.users.api.dto.User) UserServiceUnexpectedException(org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException) InvalidPasswordException(org.summerb.microservices.users.api.exceptions.InvalidPasswordException)

Example 25 with FieldValidationException

use of org.summerb.approaches.validation.FieldValidationException in project summerb by skarpushin.

the class UserServiceImpl method validateUser.

private void validateUser(User user) throws FieldValidationException {
    ValidationContext ctx = new ValidationContext();
    validateEmail(user.getEmail(), ctx);
    ctx.validateDataLengthLessOrEqual(user.getDisplayName(), User.FN_DISPLAY_NAME_SIZE, User.FN_DISPLAY_NAME);
    if (ctx.getHasErrors()) {
        throw new FieldValidationException(ctx.getErrors());
    }
}
Also used : FieldValidationException(org.summerb.approaches.validation.FieldValidationException) ValidationContext(org.summerb.approaches.validation.ValidationContext)

Aggregations

FieldValidationException (org.summerb.approaches.validation.FieldValidationException)29 User (org.summerb.microservices.users.api.dto.User)11 UserServiceUnexpectedException (org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException)11 Transactional (org.springframework.transaction.annotation.Transactional)9 UserNotFoundException (org.summerb.microservices.users.api.exceptions.UserNotFoundException)8 ValidationContext (org.summerb.approaches.validation.ValidationContext)5 DuplicateKeyException (org.springframework.dao.DuplicateKeyException)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 ValidationErrorsVm (org.summerb.approaches.springmvc.model.ValidationErrorsVm)3 ValidationError (org.summerb.approaches.validation.ValidationError)3 AuthToken (org.summerb.microservices.users.api.dto.AuthToken)3 InvalidPasswordException (org.summerb.microservices.users.api.exceptions.InvalidPasswordException)3 AccessDeniedException (org.springframework.security.access.AccessDeniedException)2 NotAuthorizedResult (org.summerb.approaches.security.api.dto.NotAuthorizedResult)2 NotAuthorizedException (org.summerb.approaches.security.api.exceptions.NotAuthorizedException)2 UserDetailsImpl (org.summerb.approaches.springmvc.security.dto.UserDetailsImpl)2 UserStatus (org.summerb.approaches.springmvc.security.dto.UserStatus)2 DuplicateUserValidationError (org.summerb.microservices.users.api.validation.DuplicateUserValidationError)2 GenericException (org.summerb.utils.exceptions.GenericException)2 ExceptionInfo (org.summerb.utils.exceptions.dto.ExceptionInfo)2