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;
}
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);
}
}
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);
}
}
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);
}
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);
}
Aggregations