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;
}
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;
}
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);
}
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);
}
}
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);
}
}
Aggregations