Search in sources :

Example 6 with UserNotFoundException

use of org.summerb.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.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 7 with UserNotFoundException

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

Example 8 with UserNotFoundException

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

the class UserServiceImpl method updateUser.

@Override
@Transactional(rollbackFor = Throwable.class)
public void updateUser(User user) throws FieldValidationException, UserNotFoundException {
    Preconditions.checkArgument(user != null, "User reference required");
    Preconditions.checkArgument(StringUtils.hasText(user.getUuid()), "User uuid must be provided");
    validateUser(user);
    boolean isUpdatedSuccessfully;
    try {
        isUpdatedSuccessfully = userDao.updateUser(user);
        eventBus.post(EntityChangedEvent.updated(user));
    } catch (DuplicateKeyException dke) {
        throw new FieldValidationException(new DuplicateUserValidationError(User.FN_EMAIL));
    } catch (Throwable t) {
        String msg = String.format("Failed to update user '%s'", user.getUuid());
        throw new UserServiceUnexpectedException(msg, t);
    }
    if (!isUpdatedSuccessfully) {
        throw new UserNotFoundException(user.getUuid());
    }
}
Also used : FieldValidationException(org.summerb.validation.FieldValidationException) UserNotFoundException(org.summerb.users.api.exceptions.UserNotFoundException) UserServiceUnexpectedException(org.summerb.users.api.exceptions.UserServiceUnexpectedException) DuplicateKeyException(org.springframework.dao.DuplicateKeyException) DuplicateUserValidationError(org.summerb.users.api.validation.DuplicateUserValidationError) Transactional(org.springframework.transaction.annotation.Transactional)

Example 9 with UserNotFoundException

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

the class UserServiceImpl method getUserByUuid.

@Override
public User getUserByUuid(String userUuid) throws UserNotFoundException {
    Assert.hasText(userUuid, "userUuid must be provided");
    User foundUser;
    try {
        foundUser = userDao.findUserByUuid(userUuid);
    } catch (Throwable t) {
        String msg = String.format("Failed to find user '%s'", userUuid);
        throw new UserServiceUnexpectedException(msg, t);
    }
    if (foundUser == null) {
        throw new UserNotFoundException(userUuid);
    }
    return foundUser;
}
Also used : UserNotFoundException(org.summerb.users.api.exceptions.UserNotFoundException) User(org.summerb.users.api.dto.User) UserServiceUnexpectedException(org.summerb.users.api.exceptions.UserServiceUnexpectedException)

Example 10 with UserNotFoundException

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

the class UserServiceImpl method deleteUserByUuid.

@Override
@Transactional(rollbackFor = Throwable.class)
public void deleteUserByUuid(String userUuid) throws UserNotFoundException {
    Preconditions.checkArgument(userUuid != null, "User uuid required");
    Preconditions.checkArgument(StringUtils.hasText(userUuid), "User uuid must be provided");
    boolean isDeletedSucceessfully = false;
    try {
        User userToDelete = userDao.findUserByUuid(userUuid);
        if (userToDelete != null) {
            isDeletedSucceessfully = userDao.deleteUser(userUuid);
            // NOTE: Assumed, that all related stuff will be deleted
            // automatically using CASCADE DELETE in the database
            eventBus.post(EntityChangedEvent.removedObject(userToDelete));
        }
    } catch (Throwable t) {
        String msg = String.format("Failed to delete user '%s'", userUuid);
        throw new UserServiceUnexpectedException(msg, t);
    }
    if (!isDeletedSucceessfully) {
        throw new UserNotFoundException(userUuid);
    }
}
Also used : UserNotFoundException(org.summerb.users.api.exceptions.UserNotFoundException) User(org.summerb.users.api.dto.User) UserServiceUnexpectedException(org.summerb.users.api.exceptions.UserServiceUnexpectedException) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

UserNotFoundException (org.summerb.users.api.exceptions.UserNotFoundException)14 User (org.summerb.users.api.dto.User)13 UserServiceUnexpectedException (org.summerb.users.api.exceptions.UserServiceUnexpectedException)9 FieldValidationException (org.summerb.validation.FieldValidationException)7 Transactional (org.springframework.transaction.annotation.Transactional)6 AuthToken (org.summerb.users.api.dto.AuthToken)3 InvalidPasswordException (org.summerb.users.api.exceptions.InvalidPasswordException)3 Test (org.junit.Test)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 AuthTokenNotFoundException (org.summerb.users.api.exceptions.AuthTokenNotFoundException)1 DuplicateUserValidationError (org.summerb.users.api.validation.DuplicateUserValidationError)1 ValidationContext (org.summerb.validation.ValidationContext)1 UserStatus (org.summerb.webappboilerplate.security.dto.UserStatus)1 PasswordInvalidValidationError (org.summerb.webappboilerplate.security.ve.PasswordInvalidValidationError)1 RegistrationAlreadyRequestedValidationError (org.summerb.webappboilerplate.security.ve.RegistrationAlreadyRequestedValidationError)1 UserNotFoundValidationError (org.summerb.webappboilerplate.security.ve.UserNotFoundValidationError)1