Search in sources :

Example 21 with FieldValidationException

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

the class AuthTokenServiceImpl method updateToken.

@Override
@Transactional(rollbackFor = Throwable.class)
public void updateToken(String authTokenUuid, long lastVerifiedAt, String newTokenValue) throws AuthTokenNotFoundException, FieldValidationException {
    Preconditions.checkArgument(authTokenUuid != null);
    Preconditions.checkArgument(StringUtils.hasText(newTokenValue), "TokenValue is mandatory");
    try {
        // First - check token itself
        AuthToken authToken = getAuthTokenByUuid(authTokenUuid);
        if (newTokenValue.equals(authToken.getTokenValue())) {
            throw new FieldValidationException(new ValidationError("validation.newValueExpected", "newTokenValue"));
        }
        // Now we need to update time when token was checked
        authTokenDao.updateToken(authTokenUuid, lastVerifiedAt, newTokenValue);
    } catch (Throwable t) {
        Throwables.throwIfInstanceOf(t, FieldValidationException.class);
        Throwables.throwIfInstanceOf(t, AuthTokenNotFoundException.class);
        String msg = String.format("Failed to update token '%s'", authTokenUuid);
        throw new UserServiceUnexpectedException(msg, t);
    }
}
Also used : FieldValidationException(org.summerb.validation.FieldValidationException) UserServiceUnexpectedException(org.summerb.users.api.exceptions.UserServiceUnexpectedException) AuthToken(org.summerb.users.api.dto.AuthToken) AuthTokenNotFoundException(org.summerb.users.api.exceptions.AuthTokenNotFoundException) ValidationError(org.summerb.validation.ValidationError) Transactional(org.springframework.transaction.annotation.Transactional)

Example 22 with FieldValidationException

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

the class UserDetailsServiceDefaultImpl method loadUserByUsername.

@Override
public UserDetails loadUserByUsername(String userEmail) throws UsernameNotFoundException {
    try {
        User user = userService.getUserByEmail(userEmail);
        List<String> permissions = permissionService.findUserPermissionsForSubject(SecurityConstants.DOMAIN, user.getUuid(), null);
        AuthToken authToken = null;
        UserDetailsImpl ret = new UserDetailsImpl(user, null, permissions, authToken);
        return ret;
    } catch (UserNotFoundException e) {
        throw new UsernameNotFoundException("User not found", e);
    } catch (FieldValidationException e) {
        throw new UsernameNotFoundException("Email provided in invalid format", e);
    } catch (Throwable t) {
        throw new UsernameNotFoundException("Failed to get user by email", t);
    }
}
Also used : UserNotFoundException(org.summerb.users.api.exceptions.UserNotFoundException) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) FieldValidationException(org.summerb.validation.FieldValidationException) User(org.summerb.users.api.dto.User) AuthToken(org.summerb.users.api.dto.AuthToken)

Example 23 with FieldValidationException

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

the class LoginController method processRegisterForm.

@RequestMapping(method = RequestMethod.POST, value = SecurityActionsUrlsProviderDefaultImpl.REGISTER)
public String processRegisterForm(@ModelAttribute(ATTR_REGISTRATION) Registration registration, Model model, HttpServletRequest request) {
    if (!isAutoTestMode) {
        CaptchaController.assertCaptchaTokenValid("register", registration.getCaptcha(), request);
    }
    try {
        // Create user
        User user = usersServiceFacade.registerUser(registration);
        model.addAttribute(ATTR_REGISTERED, true);
        if (isDevMode) {
            String activationAbsoluteLink = absoluteUrlBuilder.buildExternalUrl(securityActionsUrlsProvider.buildRegistrationActivationPath(user, null));
            model.addAttribute(UserAccountChangeHadlersDefaultImpl.ATTR_ACTIVATION_LINK, activationAbsoluteLink);
        }
    } catch (FieldValidationException fve) {
        model.addAttribute(ControllerBase.ATTR_VALIDATION_ERRORS, new ValidationErrorsVm(fve.getErrors()));
    }
    return views.registerForm();
}
Also used : FieldValidationException(org.summerb.validation.FieldValidationException) User(org.summerb.users.api.dto.User) ValidationErrorsVm(org.summerb.webappboilerplate.model.ValidationErrorsVm) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 24 with FieldValidationException

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

the class LoginController method processPasswordChangeForm.

@Secured({ "ROLE_USER" })
@RequestMapping(method = RequestMethod.POST, value = SecurityActionsUrlsProviderDefaultImpl.CHANGE_PASSWORD)
public String processPasswordChangeForm(@ModelAttribute(ATTR_PASSWORD_CHANGE) PasswordChange passwordChange, Model model, HttpServletRequest request) throws UserNotFoundException {
    try {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        usersServiceFacade.changePassword(auth.getName(), passwordChange);
        model.addAttribute(ATTR_PASSWORD_CHANGED, true);
    } catch (FieldValidationException fve) {
        model.addAttribute(ControllerBase.ATTR_VALIDATION_ERRORS, new ValidationErrorsVm(fve.getErrors()));
    }
    return views.changePassword();
}
Also used : FieldValidationException(org.summerb.validation.FieldValidationException) Authentication(org.springframework.security.core.Authentication) ValidationErrorsVm(org.summerb.webappboilerplate.model.ValidationErrorsVm) Secured(org.springframework.security.access.annotation.Secured) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 25 with FieldValidationException

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

the class ExceptionTranslatorFveImpl method buildUserMessage.

@Override
public String buildUserMessage(Throwable t, Locale locale) {
    if (!FieldValidationException.class.equals(t.getClass())) {
        return null;
    }
    FieldValidationException fve = (FieldValidationException) t;
    StringBuilder ret = new StringBuilder();
    ret.append(I18nUtils.buildMessage(fve, messageSource, locale));
    ret.append(": ");
    boolean first = true;
    for (ValidationError ve : fve.getErrors()) {
        if (!first) {
            ret.append(", ");
        }
        ret.append(translateFieldName(ve.getFieldToken(), messageSource, locale));
        ret.append(" - ");
        ret.append(I18nUtils.buildMessage(ve, messageSource, locale));
        first = false;
    }
    return ret.toString();
}
Also used : FieldValidationException(org.summerb.validation.FieldValidationException) ValidationError(org.summerb.validation.ValidationError)

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