Search in sources :

Example 1 with UserNotFoundException

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

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

the class UsersServiceFacadeImpl method getUserStatusByEmail.

@Override
public UserStatus getUserStatusByEmail(String email) throws FieldValidationException {
    ValidationContext ctx = new ValidationContext();
    ctx.validateNotEmpty(email, LoginParams.FN_EMAIL);
    ctx.throwIfHasErrors();
    // Check if user have record
    User user = null;
    try {
        user = userService.getUserByEmail(email);
    } catch (UserNotFoundException nfe) {
        return UserStatus.NotExists;
    }
    // Check if user has ROLE_USER
    List<String> permissions = permissionService.findUserPermissionsForSubject(SecurityConstants.DOMAIN, user.getUuid(), null);
    if (permissions.contains(SecurityConstants.ROLE_USER)) {
        return UserStatus.NormalUser;
    }
    if (permissions.contains(SecurityConstants.MARKER_AWAITING_ACTIVATION)) {
        return UserStatus.AwaitingActivation;
    }
    return UserStatus.Provisioned;
}
Also used : UserNotFoundException(org.summerb.microservices.users.api.exceptions.UserNotFoundException) User(org.summerb.microservices.users.api.dto.User) ValidationContext(org.summerb.approaches.validation.ValidationContext)

Example 3 with UserNotFoundException

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

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

the class AuthTokenServiceImpl method isAuthTokenValid.

@Override
@Transactional(rollbackFor = Throwable.class)
public AuthToken isAuthTokenValid(String userUuid, String authTokenUuid, String tokenValue) throws UserNotFoundException {
    Preconditions.checkArgument(userUuid != null);
    Preconditions.checkArgument(authTokenUuid != null);
    Preconditions.checkArgument(StringUtils.hasText(tokenValue), "TokenValue is mandatory");
    try {
        // First - check token itself
        AuthToken authToken = getAuthTokenByUuid(authTokenUuid);
        if (authToken.getExpiresAt() < getNow()) {
            authTokenDao.deleteAuthToken(authTokenUuid);
            return null;
        }
        if (!tokenValue.equals(authToken.getTokenValue())) {
            return null;
        }
        // Check reference to user
        User user = userService.getUserByUuid(userUuid);
        if (!authToken.getUserUuid().equals(user.getUuid())) {
            return null;
        }
        // Now we need to update time when token was checked
        authToken.setTokenValue(UUID.randomUUID().toString());
        authToken.setLastVerifiedAt(getNow());
        authTokenDao.updateToken(authTokenUuid, authToken.getLastVerifiedAt(), authToken.getTokenValue());
        return authToken;
    } catch (AuthTokenNotFoundException nfe) {
        return null;
    } catch (Throwable t) {
        Throwables.throwIfInstanceOf(t, UserNotFoundException.class);
        String msg = String.format("Failed to check auth token '%s' validity for user '%s'", authTokenUuid, userUuid);
        throw new UserServiceUnexpectedException(msg, t);
    }
}
Also used : UserNotFoundException(org.summerb.microservices.users.api.exceptions.UserNotFoundException) User(org.summerb.microservices.users.api.dto.User) UserServiceUnexpectedException(org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException) AuthToken(org.summerb.microservices.users.api.dto.AuthToken) AuthTokenNotFoundException(org.summerb.microservices.users.api.exceptions.AuthTokenNotFoundException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with UserNotFoundException

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

the class UserServiceImpl method getUserByEmail.

@Override
public User getUserByEmail(String userEmail) throws FieldValidationException, UserNotFoundException {
    Assert.notNull(userEmail, "user email must be provided");
    validateEmail(userEmail);
    User foundUser;
    try {
        foundUser = userDao.findUserByEmail(userEmail);
    } catch (Throwable t) {
        String msg = String.format("Failed to find user by email '%s'", userEmail);
        throw new UserServiceUnexpectedException(msg, t);
    }
    if (foundUser == null) {
        throw new UserNotFoundException(userEmail);
    }
    return foundUser;
}
Also used : UserNotFoundException(org.summerb.microservices.users.api.exceptions.UserNotFoundException) User(org.summerb.microservices.users.api.dto.User) UserServiceUnexpectedException(org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException)

Aggregations

UserNotFoundException (org.summerb.microservices.users.api.exceptions.UserNotFoundException)14 User (org.summerb.microservices.users.api.dto.User)13 UserServiceUnexpectedException (org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException)9 FieldValidationException (org.summerb.approaches.validation.FieldValidationException)7 Transactional (org.springframework.transaction.annotation.Transactional)6 AuthToken (org.summerb.microservices.users.api.dto.AuthToken)3 InvalidPasswordException (org.summerb.microservices.users.api.exceptions.InvalidPasswordException)3 Test (org.junit.Test)2 UserDetailsImpl (org.summerb.approaches.springmvc.security.dto.UserDetailsImpl)2 DuplicateKeyException (org.springframework.dao.DuplicateKeyException)1 AuthenticationServiceException (org.springframework.security.authentication.AuthenticationServiceException)1 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)1 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)1 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)1 UserStatus (org.summerb.approaches.springmvc.security.dto.UserStatus)1 PasswordInvalidValidationError (org.summerb.approaches.springmvc.security.ve.PasswordInvalidValidationError)1 RegistrationAlreadyRequestedValidationError (org.summerb.approaches.springmvc.security.ve.RegistrationAlreadyRequestedValidationError)1 UserNotFoundValidationError (org.summerb.approaches.springmvc.security.ve.UserNotFoundValidationError)1 ValidationContext (org.summerb.approaches.validation.ValidationContext)1 AuthTokenNotFoundException (org.summerb.microservices.users.api.exceptions.AuthTokenNotFoundException)1