Search in sources :

Example 1 with UserServiceUnexpectedException

use of org.summerb.microservices.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.approaches.validation.FieldValidationException) UserNotFoundException(org.summerb.microservices.users.api.exceptions.UserNotFoundException) RegistrationAlreadyRequestedValidationError(org.summerb.approaches.springmvc.security.ve.RegistrationAlreadyRequestedValidationError) User(org.summerb.microservices.users.api.dto.User) UserServiceUnexpectedException(org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException) UserStatus(org.summerb.approaches.springmvc.security.dto.UserStatus) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with UserServiceUnexpectedException

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

Example 3 with UserServiceUnexpectedException

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

the class AuthTokenServiceImpl method authenticate.

@Override
@Transactional(rollbackFor = Throwable.class)
public AuthToken authenticate(String userEmail, String passwordPlain, String clientIp) throws UserNotFoundException, FieldValidationException, InvalidPasswordException {
    Preconditions.checkArgument(userEmail != null);
    Preconditions.checkArgument(passwordPlain != null);
    Preconditions.checkArgument(clientIp != null);
    try {
        User user = validateAndGetUser(userEmail, passwordPlain);
        return createAuthToken(user.getEmail(), clientIp, UUID.randomUUID().toString(), UUID.randomUUID().toString());
    } catch (Throwable t) {
        Throwables.throwIfInstanceOf(t, UserNotFoundException.class);
        Throwables.throwIfInstanceOf(t, FieldValidationException.class);
        Throwables.throwIfInstanceOf(t, InvalidPasswordException.class);
        String msg = String.format("Failed to create auth otken for user '%s'", userEmail);
        throw new UserServiceUnexpectedException(msg, t);
    }
}
Also used : UserNotFoundException(org.summerb.microservices.users.api.exceptions.UserNotFoundException) FieldValidationException(org.summerb.approaches.validation.FieldValidationException) User(org.summerb.microservices.users.api.dto.User) UserServiceUnexpectedException(org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException) InvalidPasswordException(org.summerb.microservices.users.api.exceptions.InvalidPasswordException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with UserServiceUnexpectedException

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

the class AuthTokenServiceImpl method deleteAuthToken.

@Override
@Transactional(rollbackFor = Throwable.class)
public void deleteAuthToken(String authTokenUuid) throws AuthTokenNotFoundException {
    Preconditions.checkArgument(authTokenUuid != null);
    try {
        getAuthTokenByUuid(authTokenUuid);
        authTokenDao.deleteAuthToken(authTokenUuid);
    } catch (AuthTokenNotFoundException nfe) {
        // it's ok
        return;
    } catch (Throwable t) {
        String msg = String.format("Failed to delete auth token '%s'", authTokenUuid);
        throw new UserServiceUnexpectedException(msg, t);
    }
}
Also used : UserServiceUnexpectedException(org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException) AuthTokenNotFoundException(org.summerb.microservices.users.api.exceptions.AuthTokenNotFoundException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with UserServiceUnexpectedException

use of org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException 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.approaches.validation.FieldValidationException) UserServiceUnexpectedException(org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException) AuthToken(org.summerb.microservices.users.api.dto.AuthToken) AuthTokenNotFoundException(org.summerb.microservices.users.api.exceptions.AuthTokenNotFoundException) ValidationError(org.summerb.approaches.validation.ValidationError) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

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