Search in sources :

Example 6 with UserDto

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);
}
Also used : MotechUser(org.motechproject.security.domain.MotechUser) User(org.springframework.security.core.userdetails.User) MotechUser(org.motechproject.security.domain.MotechUser) Authentication(org.springframework.security.core.Authentication) UserDto(org.motechproject.security.model.UserDto) Transactional(org.springframework.transaction.annotation.Transactional)

Example 7 with UserDto

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"));
}
Also used : UserDto(org.motechproject.security.model.UserDto) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 8 with UserDto

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);
}
Also used : UserDto(org.motechproject.security.model.UserDto)

Example 9 with UserDto

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));
}
Also used : MotechUser(org.motechproject.security.domain.MotechUser) UserDto(org.motechproject.security.model.UserDto) Transactional(org.springframework.transaction.annotation.Transactional)

Example 10 with UserDto

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");
}
Also used : UserDto(org.motechproject.security.model.UserDto) List(java.util.List) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Test(org.junit.Test)

Aggregations

UserDto (org.motechproject.security.model.UserDto)15 Test (org.junit.Test)12 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)3 ArrayList (java.util.ArrayList)2 Locale (java.util.Locale)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 MotechUser (org.motechproject.security.domain.MotechUser)2 MotechUserProfile (org.motechproject.security.domain.MotechUserProfile)2 PasswordValidatorException (org.motechproject.security.exception.PasswordValidatorException)2 User (org.springframework.security.core.userdetails.User)2 Transactional (org.springframework.transaction.annotation.Transactional)2 List (java.util.List)1 HttpClient (org.apache.http.client.HttpClient)1 HttpGet (org.apache.http.client.methods.HttpGet)1 Contains (org.mockito.internal.matchers.Contains)1 PasswordTooShortException (org.motechproject.security.exception.PasswordTooShortException)1 RoleDto (org.motechproject.security.model.RoleDto)1 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)1 Authentication (org.springframework.security.core.Authentication)1 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)1