Search in sources :

Example 6 with UserServiceUnexpectedException

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

the class UserServiceImpl method createUser.

@Override
@Transactional(rollbackFor = Throwable.class)
public User createUser(User userTemplate) throws FieldValidationException {
    Preconditions.checkArgument(userTemplate != null, "userTemplate is required");
    validateUser(userTemplate);
    User userToCreate = new User();
    userToCreate.setDisplayName(userTemplate.getDisplayName());
    userToCreate.setEmail(userTemplate.getEmail());
    userToCreate.setIntegrationData(userTemplate.getIntegrationData());
    userToCreate.setIsBlocked(userTemplate.getIsBlocked());
    userToCreate.setLocale(userTemplate.getLocale());
    userToCreate.setRegisteredAt(userTemplate.getRegisteredAt());
    userToCreate.setTimeZone(userTemplate.getTimeZone());
    // Patch data
    if (userTemplate.getUuid() != null) {
        Preconditions.checkArgument(stringIdGenerator.isValidId(userTemplate.getUuid()), "User id is invalid");
        userToCreate.setUuid(userTemplate.getUuid());
    } else {
        userToCreate.setUuid(stringIdGenerator.generateNewId(userTemplate));
    }
    if (userToCreate.getRegisteredAt() == 0) {
        userToCreate.setRegisteredAt(new Date().getTime());
    }
    if (userToCreate.getLocale() == null) {
        userToCreate.setLocale(LocaleContextHolder.getLocale().toString());
    }
    if (userToCreate.getTimeZone() == null) {
        userToCreate.setTimeZone(Calendar.getInstance().getTimeZone().getID());
    }
    // Create
    try {
        userDao.createUser(userToCreate);
        eventBus.post(EntityChangedEvent.added(userToCreate));
    } catch (DuplicateKeyException dke) {
        throw new FieldValidationException(new DuplicateUserValidationError(User.FN_EMAIL));
    } catch (Throwable t) {
        String msg = String.format("Failed to create user with email '%s'", userToCreate.getEmail());
        throw new UserServiceUnexpectedException(msg, t);
    }
    return userToCreate;
}
Also used : FieldValidationException(org.summerb.validation.FieldValidationException) User(org.summerb.users.api.dto.User) UserServiceUnexpectedException(org.summerb.users.api.exceptions.UserServiceUnexpectedException) Date(java.util.Date) DuplicateKeyException(org.springframework.dao.DuplicateKeyException) DuplicateUserValidationError(org.summerb.users.api.validation.DuplicateUserValidationError) Transactional(org.springframework.transaction.annotation.Transactional)

Example 7 with UserServiceUnexpectedException

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

Example 8 with UserServiceUnexpectedException

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

the class AuthTokenServiceImpl method createAuthToken.

@Override
@Transactional(rollbackFor = Throwable.class)
public AuthToken createAuthToken(String userEmail, String clientIp, String tokenUuid, String tokenValueUuid) throws UserNotFoundException, FieldValidationException {
    Preconditions.checkArgument(userEmail != null);
    Preconditions.checkArgument(clientIp != null);
    Preconditions.checkArgument(StringUtils.hasText(tokenUuid));
    Preconditions.checkArgument(StringUtils.hasText(tokenValueUuid));
    try {
        User user = userService.getUserByEmail(userEmail);
        AuthToken authToken = buildNewAuthToken(user, clientIp, tokenUuid, tokenValueUuid);
        authTokenDao.createAuthToken(authToken);
        return authToken;
    } catch (Throwable t) {
        Throwables.throwIfInstanceOf(t, UserNotFoundException.class);
        Throwables.throwIfInstanceOf(t, FieldValidationException.class);
        String msg = String.format("Failed to create auth otken for user '%s'", userEmail);
        throw new UserServiceUnexpectedException(msg, t);
    }
}
Also used : UserNotFoundException(org.summerb.users.api.exceptions.UserNotFoundException) FieldValidationException(org.summerb.validation.FieldValidationException) User(org.summerb.users.api.dto.User) UserServiceUnexpectedException(org.summerb.users.api.exceptions.UserServiceUnexpectedException) AuthToken(org.summerb.users.api.dto.AuthToken) Transactional(org.springframework.transaction.annotation.Transactional)

Example 9 with UserServiceUnexpectedException

use of org.summerb.users.api.exceptions.UserServiceUnexpectedException 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.users.api.exceptions.UserNotFoundException) User(org.summerb.users.api.dto.User) UserServiceUnexpectedException(org.summerb.users.api.exceptions.UserServiceUnexpectedException) AuthToken(org.summerb.users.api.dto.AuthToken) AuthTokenNotFoundException(org.summerb.users.api.exceptions.AuthTokenNotFoundException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 10 with UserServiceUnexpectedException

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

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