use of org.motechproject.security.model.UserDto in project motech by motech.
the class MotechUserServiceImpl method getCurrentUser.
@Override
@Transactional
public UserDto getCurrentUser() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
User userInSession = (authentication == null) ? null : (User) authentication.getPrincipal();
if (userInSession == null) {
return null;
}
String currentUserName = userInSession.getUsername();
MotechUser user = motechUsersDao.findByUserName(currentUserName);
return new UserDto(user);
}
use of org.motechproject.security.model.UserDto in project motech by motech.
the class PersistedUserValidatorTest method shouldRejectEmailIfInUse.
@Test
public void shouldRejectEmailIfInUse() {
PersistedUserValidator persistedUserValidator = new PersistedUserValidator(userService);
when(userService.hasUser("admin")).thenReturn(false);
UserDto user = new UserDto();
user.setUserName("john");
when(userService.hasEmail("admin@motech.org")).thenReturn(true);
List<String> errors = new ArrayList<>();
persistedUserValidator.validate(getExampleStartupForm(), errors, ConfigSource.FILE);
assertTrue(errors.contains("server.error.email.exist"));
}
use of org.motechproject.security.model.UserDto in project motech by motech.
the class MotechUserServiceImpl method changeEmail.
@Override
public void changeEmail(String email) {
UserDto currentUser = getCurrentUser();
currentUser.setEmail(email);
updateUserDetailsWithoutPassword(currentUser);
}
use of org.motechproject.security.model.UserDto in project motech by motech.
the class MotechUserServiceImpl method setLocale.
@Override
@Transactional
public void setLocale(Locale locale) {
UserDto currentUser = getCurrentUser();
MotechUser user = motechUsersDao.findByUserName(currentUser.getUserName());
user.setLocale(locale);
updateUserDetailsWithoutPassword(new UserDto(user));
}
use of org.motechproject.security.model.UserDto in project motech by motech.
the class UserControllerTest method shouldGeneratePasswordRespectingMinLength.
@Test
public void shouldGeneratePasswordRespectingMinLength() throws Exception {
when(settingService.getMinPasswordLength()).thenReturn(20);
UserDto userDto = new UserDto();
userDto.setGeneratePassword(true);
userDto.setUserName("john");
userDto.setEmail("john@email.com");
userDto.setLocale(Locale.CANADA_FRENCH);
mockMvc.perform(post("/users/create").body(new ObjectMapper().writeValueAsBytes(userDto)).contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk());
ArgumentCaptor<String> passwordCaptor = ArgumentCaptor.forClass(String.class);
verify(userService).register(eq("john"), passwordCaptor.capture(), eq("john@email.com"), eq(""), eq((List<String>) null), eq(Locale.CANADA_FRENCH));
assertNotNull(passwordCaptor.getValue());
assertTrue(passwordCaptor.getValue().length() >= 20);
verify(userService).sendLoginInformation("john");
}
Aggregations