Search in sources :

Example 1 with User

use of org.summerb.users.api.dto.User 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 2 with User

use of org.summerb.users.api.dto.User in project summerb by skarpushin.

the class UserServiceImplFactory method createUsersServiceImpl.

public static UserServiceImpl createUsersServiceImpl() {
    UserServiceImpl ret = new UserServiceImpl();
    UserDao userDao = Mockito.mock(UserDao.class);
    ret.setUserDao(userDao);
    ret.setEventBus(Mockito.mock(EventBus.class));
    User existingUser = UserFactory.createExistingUser();
    when(userDao.findUserByUuid(UserFactory.EXISTENT_USER)).thenReturn(existingUser);
    when(userDao.findUserByUuid(UserFactory.EXISTENT_USER_2_PROBLEM_WITH_PASSWORD)).thenReturn(UserFactory.createExistingUser2());
    when(userDao.findUserByUuid(UserFactory.EXISTENT_USER_WITH_MISSING_PASSWORD)).thenReturn(UserFactory.createUserWithMissingPassword());
    when(userDao.findUserByUuid(UserFactory.NON_EXISTENT_USER)).thenReturn(null);
    when(userDao.findUserByUuid(UserFactory.USER_RESULT_IN_EXCEPTION)).thenThrow(new IllegalStateException("Simulate unexpected excception"));
    when(userDao.findUserByEmail(UserFactory.EXISTENT_USER_EMAIL)).thenReturn(existingUser);
    when(userDao.findUserByEmail(UserFactory.NON_EXISTENT_USER_EMAIL)).thenReturn(null);
    when(userDao.findUserByEmail(UserFactory.USER_EMAIL_RESULT_IN_EXCEPTION)).thenThrow(new IllegalStateException("Simulate unexpected excception"));
    when(userDao.findUserByDisplayNamePartial(eq(UserFactory.EXISTENT_USER), any(PagerParams.class))).thenThrow(new IllegalStateException("Simulate unexpected excception"));
    when(userDao.findUserByUuid(UserFactory.EXISTENT_USER_WITH_EXPIRED_TOKEN)).thenReturn(UserFactory.createUserWithExpiredToken());
    when(userDao.updateUser(UserFactory.createDuplicateUser())).thenThrow(new DuplicateKeyException("Simulate unexpected excception"));
    return ret;
}
Also used : User(org.summerb.users.api.dto.User) UserDao(org.summerb.users.impl.dao.UserDao) PagerParams(org.summerb.easycrud.api.dto.PagerParams) EventBus(com.google.common.eventbus.EventBus) DuplicateKeyException(org.springframework.dao.DuplicateKeyException)

Example 3 with User

use of org.summerb.users.api.dto.User in project summerb by skarpushin.

the class PasswordDaoImplTest method testSetUserPassword_expectWillBePerformedOk.

@Test
public void testSetUserPassword_expectWillBePerformedOk() throws Exception {
    User createdUser = userService.createUser(UserFactory.createNewUserTemplate());
    String pwd1 = "aaaa";
    passwordService.setUserPassword(createdUser.getUuid(), pwd1);
    boolean result = passwordService.isUserPasswordValid(createdUser.getUuid(), pwd1);
    assertTrue(result);
}
Also used : User(org.summerb.users.api.dto.User) Test(org.junit.Test)

Example 4 with User

use of org.summerb.users.api.dto.User 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 5 with User

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

Aggregations

User (org.summerb.users.api.dto.User)61 Test (org.junit.Test)33 UserNotFoundException (org.summerb.users.api.exceptions.UserNotFoundException)13 AuthToken (org.summerb.users.api.dto.AuthToken)11 UserServiceUnexpectedException (org.summerb.users.api.exceptions.UserServiceUnexpectedException)11 FieldValidationException (org.summerb.validation.FieldValidationException)11 Transactional (org.springframework.transaction.annotation.Transactional)8 Date (java.util.Date)4 PagerParams (org.summerb.easycrud.api.dto.PagerParams)4 DuplicateKeyException (org.springframework.dao.DuplicateKeyException)3 InvalidPasswordException (org.summerb.users.api.exceptions.InvalidPasswordException)3 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 GenericException (org.summerb.utils.exceptions.GenericException)2 ValidationContext (org.summerb.validation.ValidationContext)2 CacheBuilder (com.google.common.cache.CacheBuilder)1 EventBus (com.google.common.eventbus.EventBus)1 Locale (java.util.Locale)1 Secured (org.springframework.security.access.annotation.Secured)1 AuthenticationServiceException (org.springframework.security.authentication.AuthenticationServiceException)1