Search in sources :

Example 1 with UserStatus

use of org.summerb.webappboilerplate.security.dto.UserStatus 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 2 with UserStatus

use of org.summerb.webappboilerplate.security.dto.UserStatus 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)

Aggregations

FieldValidationException (org.summerb.validation.FieldValidationException)2 UserStatus (org.summerb.webappboilerplate.security.dto.UserStatus)2 Transactional (org.springframework.transaction.annotation.Transactional)1 User (org.summerb.users.api.dto.User)1 UserNotFoundException (org.summerb.users.api.exceptions.UserNotFoundException)1 UserServiceUnexpectedException (org.summerb.users.api.exceptions.UserServiceUnexpectedException)1 ValidationContext (org.summerb.validation.ValidationContext)1 RegistrationAlreadyRequestedValidationError (org.summerb.webappboilerplate.security.ve.RegistrationAlreadyRequestedValidationError)1 RegistrationRequiredValidationError (org.summerb.webappboilerplate.security.ve.RegistrationRequiredValidationError)1