Search in sources :

Example 11 with FieldValidationException

use of org.summerb.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.validation.FieldValidationException) User(org.summerb.users.api.dto.User) GenericException(org.summerb.utils.exceptions.GenericException)

Example 12 with FieldValidationException

use of org.summerb.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.validation.FieldValidationException) User(org.summerb.users.api.dto.User) UserServiceUnexpectedException(org.summerb.users.api.exceptions.UserServiceUnexpectedException)

Example 13 with FieldValidationException

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

the class UsersServiceFacadeImpl method validateUserIsEligableForPasswordReset.

protected void validateUserIsEligableForPasswordReset(String email) throws FieldValidationException {
    ValidationContext ctx = new ValidationContext();
    if (!ctx.validateEmailFormat(email, User.FN_EMAIL)) {
        throw new FieldValidationException(ctx.getErrors());
    }
    // now see if this user exists
    UserStatus userStatus = getUserStatusByEmail(email);
    if (userStatus == UserStatus.NotExists || userStatus == UserStatus.Provisioned) {
        throw new FieldValidationException(new RegistrationRequiredValidationError());
    }
    // Sanity check
    if (userStatus != UserStatus.NormalUser && userStatus != UserStatus.AwaitingActivation) {
        throw new RuntimeException("Password Reset scenario is not supported for user whose status is: " + userStatus);
    }
}
Also used : FieldValidationException(org.summerb.validation.FieldValidationException) RegistrationRequiredValidationError(org.summerb.webappboilerplate.security.ve.RegistrationRequiredValidationError) UserStatus(org.summerb.webappboilerplate.security.dto.UserStatus) ValidationContext(org.summerb.validation.ValidationContext)

Example 14 with FieldValidationException

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

the class ControllerExceptionHandlerStrategyLegacyImpl method buildJsonError.

/**
 * This peace of crap needs to be removed. Because in case of JSON it's rest
 * API, there is no place for {@link ModelAndView}. Response should be pure JSON
 * content.
 *
 * So instead of implementing it here it's better to just re-throw exception and
 * let {@link RestExceptionTranslator} handle it and gracefully convert it into
 * json description of error happened
 */
protected ModelAndView buildJsonError(Throwable ex, HttpServletRequest req, HttpServletResponse res) {
    String msg = exceptionTranslator.buildUserMessage(ex, LocaleContextHolder.getLocale());
    NotAuthorizedException nae;
    FieldValidationException fve;
    AccessDeniedException ade;
    boolean translateAuthExc = Boolean.TRUE.equals(Boolean.valueOf(req.getHeader(RestExceptionTranslator.X_TRANSLATE_AUTHORIZATION_ERRORS)));
    if ((nae = ExceptionUtils.findExceptionOfType(ex, NotAuthorizedException.class)) != null) {
        NotAuthorizedResult naeResult = nae.getResult();
        res.setStatus(isAnonymous() ? HttpServletResponse.SC_UNAUTHORIZED : HttpServletResponse.SC_FORBIDDEN);
        if (translateAuthExc) {
            return new ModelAndView(jsonView, ControllerBase.ATTR_EXCEPTION, msg);
        } else {
            respondWithJson(naeResult, res);
            return null;
        }
    } else if ((ade = ExceptionUtils.findExceptionOfType(ex, AccessDeniedException.class)) != null) {
        res.setStatus(isAnonymous() ? HttpServletResponse.SC_UNAUTHORIZED : HttpServletResponse.SC_FORBIDDEN);
        if (translateAuthExc) {
            return new ModelAndView(jsonView, ControllerBase.ATTR_EXCEPTION, msg);
        } else {
            respondWithJson(new NotAuthorizedResult(getCurrentUser(), SecurityMessageCodes.ACCESS_DENIED), res);
            return null;
        }
    } else if ((fve = ExceptionUtils.findExceptionOfType(ex, FieldValidationException.class)) != null) {
        res.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        ValidationErrorsVm vepm = new ValidationErrorsVm(fve.getErrors());
        return new ModelAndView(jsonView, ControllerBase.ATTR_VALIDATION_ERRORS, vepm.getMsg());
    }
    log.warn("Failed to process request", ex);
    res.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    return new ModelAndView(jsonView, ControllerBase.ATTR_EXCEPTION, msg);
}
Also used : FieldValidationException(org.summerb.validation.FieldValidationException) AccessDeniedException(org.springframework.security.access.AccessDeniedException) ValidationErrorsVm(org.summerb.webappboilerplate.model.ValidationErrorsVm) ModelAndView(org.springframework.web.servlet.ModelAndView) NotAuthorizedResult(org.summerb.security.api.dto.NotAuthorizedResult) NotAuthorizedException(org.summerb.security.api.exceptions.NotAuthorizedException)

Example 15 with FieldValidationException

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

the class RestAuthenticationFailureHandler method onAuthenticationFailure.

@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
    FieldValidationException fve = ExceptionUtils.findExceptionOfType(exception, FieldValidationException.class);
    if (fve != null) {
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        jsonResponseHelper.writeResponseBody(new ValidationErrors(fve.getErrors()), response);
        return;
    }
    response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    GenericServerErrorResult responseBody = new GenericServerErrorResult(exceptionTranslator.buildUserMessage(exception, LocaleContextHolder.getLocale()), new ExceptionInfo(exception));
    jsonResponseHelper.writeResponseBody(responseBody, response);
}
Also used : FieldValidationException(org.summerb.validation.FieldValidationException) ValidationErrors(org.summerb.validation.ValidationErrors) GenericServerErrorResult(org.summerb.utils.exceptions.dto.GenericServerErrorResult) ExceptionInfo(org.summerb.utils.exceptions.dto.ExceptionInfo)

Aggregations

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