Search in sources :

Example 11 with UserServiceUnexpectedException

use of org.summerb.users.api.exceptions.UserServiceUnexpectedException 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.validation.FieldValidationException) UserServiceUnexpectedException(org.summerb.users.api.exceptions.UserServiceUnexpectedException) FieldRequiredValidationError(org.summerb.validation.errors.FieldRequiredValidationError) Transactional(org.springframework.transaction.annotation.Transactional)

Example 12 with UserServiceUnexpectedException

use of org.summerb.users.api.exceptions.UserServiceUnexpectedException in project summerb by skarpushin.

the class PasswordServiceImpl method isRestorationTokenValid.

@Override
public boolean isRestorationTokenValid(String userUuid, String restorationTokenUuid) throws UserNotFoundException {
    Preconditions.checkArgument(userUuid != null);
    Preconditions.checkArgument(restorationTokenUuid != null);
    assertUserExists(userUuid);
    try {
        Password password = passwordDao.findPasswordByUserUuid(userUuid);
        if (password == null || !restorationTokenUuid.equals(password.getRestorationToken())) {
            return false;
        }
    } catch (Throwable t) {
        String msg = String.format("Failed to check user '%s' restoration token validity", userUuid);
        throw new UserServiceUnexpectedException(msg, t);
    }
    return true;
}
Also used : UserServiceUnexpectedException(org.summerb.users.api.exceptions.UserServiceUnexpectedException) Password(org.summerb.users.impl.dom.Password)

Example 13 with UserServiceUnexpectedException

use of org.summerb.users.api.exceptions.UserServiceUnexpectedException in project summerb by skarpushin.

the class UsersServiceFacadeImpl method registerUser.

@Transactional(rollbackFor = Throwable.class)
@Override
public User registerUser(Registration registration) throws FieldValidationException {
    try {
        Preconditions.checkArgument(registration != null, "Registration param must be not null");
        // Validate display name
        validateRegistration(registration);
        // Validate user status
        UserStatus userStatus = getUserStatusByEmail(registration.getEmail());
        if (userStatus == UserStatus.AwaitingActivation) {
            throw new FieldValidationException(new RegistrationAlreadyRequestedValidationError());
        }
        // Create user
        User user = null;
        if (userStatus == UserStatus.Provisioned) {
            user = userService.getUserByEmail(registration.getEmail());
            user.setDisplayName(registration.getDisplayName());
            user.setLocale(CurrentRequestUtils.getLocale().toString());
            user.setTimeZone(TimeZone.getDefault().getID());
            userService.updateUser(user);
        } else {
            user = new User();
            user.setEmail(registration.getEmail());
            user.setDisplayName(registration.getDisplayName());
            user.setLocale(CurrentRequestUtils.getLocale().toString());
            user.setTimeZone(TimeZone.getDefault().getID());
            user = userService.createUser(user);
        }
        // Create password
        passwordService.setUserPassword(user.getUuid(), registration.getPassword());
        // Create user account permissions
        permissionService.grantPermission(SecurityConstants.DOMAIN, user.getUuid(), null, SecurityConstants.MARKER_AWAITING_ACTIVATION);
        runUserRegisteredHandler(user);
        // 
        return user;
    } catch (UserNotFoundException e) {
        throw new UserServiceUnexpectedException("User was just created, but not found", e);
    } catch (Throwable t) {
        Throwables.throwIfInstanceOf(t, FieldValidationException.class);
        throw new RuntimeException("Unexpected error while registering user", t);
    }
}
Also used : FieldValidationException(org.summerb.validation.FieldValidationException) UserNotFoundException(org.summerb.users.api.exceptions.UserNotFoundException) RegistrationAlreadyRequestedValidationError(org.summerb.webappboilerplate.security.ve.RegistrationAlreadyRequestedValidationError) User(org.summerb.users.api.dto.User) UserServiceUnexpectedException(org.summerb.users.api.exceptions.UserServiceUnexpectedException) UserStatus(org.summerb.webappboilerplate.security.dto.UserStatus) Transactional(org.springframework.transaction.annotation.Transactional)

Example 14 with UserServiceUnexpectedException

use of org.summerb.users.api.exceptions.UserServiceUnexpectedException in project summerb by skarpushin.

the class UsersServiceFacadeImpl method getNewPasswordResetToken.

@Transactional(rollbackFor = Throwable.class)
@Override
public String getNewPasswordResetToken(String email) throws FieldValidationException {
    try {
        validateUserIsEligableForPasswordReset(email);
        User user = userService.getUserByEmail(email);
        String passwordResetToken = passwordService.getNewRestorationTokenForUser(user.getUuid());
        if (passwordResetArmedHandler != null) {
            passwordResetArmedHandler.onPasswordResetRequested(user, passwordResetToken);
        }
        return passwordResetToken;
    } 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) Transactional(org.springframework.transaction.annotation.Transactional)

Example 15 with UserServiceUnexpectedException

use of org.summerb.users.api.exceptions.UserServiceUnexpectedException 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)

Aggregations

UserServiceUnexpectedException (org.summerb.users.api.exceptions.UserServiceUnexpectedException)27 Transactional (org.springframework.transaction.annotation.Transactional)18 User (org.summerb.users.api.dto.User)11 FieldValidationException (org.summerb.validation.FieldValidationException)11 UserNotFoundException (org.summerb.users.api.exceptions.UserNotFoundException)9 DuplicateKeyException (org.springframework.dao.DuplicateKeyException)3 AuthToken (org.summerb.users.api.dto.AuthToken)3 AuthTokenNotFoundException (org.summerb.users.api.exceptions.AuthTokenNotFoundException)3 InvalidPasswordException (org.summerb.users.api.exceptions.InvalidPasswordException)2 DuplicateUserValidationError (org.summerb.users.api.validation.DuplicateUserValidationError)2 Password (org.summerb.users.impl.dom.Password)2 Date (java.util.Date)1 ValidationError (org.summerb.validation.ValidationError)1 FieldRequiredValidationError (org.summerb.validation.errors.FieldRequiredValidationError)1 UserStatus (org.summerb.webappboilerplate.security.dto.UserStatus)1 RegistrationAlreadyRequestedValidationError (org.summerb.webappboilerplate.security.ve.RegistrationAlreadyRequestedValidationError)1