Search in sources :

Example 46 with MotechUser

use of org.motechproject.security.domain.MotechUser in project motech by motech.

the class PasswordRecoveryServiceBundleIT method setUp.

@Before
@Override
public void setUp() throws Exception {
    super.setUp();
    recoveriesDataService.deleteAll();
    usersDataService.create(new MotechUser(USERNAME, "test", EMAIL, null, null, null, Locale.ENGLISH));
    usersDataService.create(new MotechUser(OTHER_USERNAME, "test", OTHER_EMAIL, null, null, null, Locale.ENGLISH));
    // Fake time
    DateTimeUtils.setCurrentMillisFixed(DateTime.now().minusHours(1).getMillis());
}
Also used : MotechUser(org.motechproject.security.domain.MotechUser) Before(org.junit.Before)

Example 47 with MotechUser

use of org.motechproject.security.domain.MotechUser in project motech by motech.

the class MotechOpenIdUserDetailsService method loadUserDetails.

/**
 * Adds user for given OpenId to {@link MotechUsersDao}
 * and return his {@link org.springframework.security.core.userdetails.UserDetails}
 *
 * @param token for OpenId
 * @return details of added user
 */
@Override
@Transactional
public UserDetails loadUserDetails(OpenIDAuthenticationToken token) {
    MotechUser user = motechUsersDao.findUserByOpenId(token.getName());
    if (user == null) {
        List<String> roles = new ArrayList<>();
        if (motechUsersDao.getOpenIdUsers().isEmpty()) {
            for (RoleDto role : motechRoleService.getRoles()) {
                roles.add(role.getRoleName());
            }
        }
        user = new MotechUser(getAttribute(token.getAttributes(), "Email"), "", getAttribute(token.getAttributes(), "Email"), "", roles, token.getName(), Locale.getDefault());
        motechUsersDao.addOpenIdUser(user);
    }
    return new User(user.getUserName(), user.getPassword(), user.isActive(), true, !UserStatus.MUST_CHANGE_PASSWORD.equals(user.getUserStatus()), !UserStatus.BLOCKED.equals(user.getUserStatus()), authoritiesService.authoritiesFor(user));
}
Also used : RoleDto(org.motechproject.security.model.RoleDto) MotechUser(org.motechproject.security.domain.MotechUser) User(org.springframework.security.core.userdetails.User) MotechUser(org.motechproject.security.domain.MotechUser) ArrayList(java.util.ArrayList) Transactional(org.springframework.transaction.annotation.Transactional)

Example 48 with MotechUser

use of org.motechproject.security.domain.MotechUser in project motech by motech.

the class PasswordRecoveryServiceImpl method passwordRecoveryRequest.

@Override
@Transactional
public String passwordRecoveryRequest(String email, DateTime expiration, boolean notify) throws UserNotFoundException {
    MotechUser user = motechUsersDao.findUserByEmail(email);
    DateTime expirationDate = expiration;
    if (expirationDate == null) {
        expirationDate = DateTime.now().plusHours(DEFAULT_EXPIRATION_HOURS);
    } else if (expirationDate.isBefore(DateTime.now())) {
        throw new IllegalArgumentException("The expiration date shouldn't be a past date!");
    }
    if (user == null) {
        throw new UserNotFoundException("User with email not found: " + email);
    }
    String token = RandomStringUtils.randomAlphanumeric(TOKEN_LENGTH);
    PasswordRecovery recovery = createRecovery(user.getUserName(), user.getEmail(), token, expirationDate, user.getLocale());
    if (notify) {
        emailSender.sendRecoveryEmail(recovery);
    }
    LOGGER.info("Created a password recovery for user " + user.getUserName());
    return token;
}
Also used : UserNotFoundException(org.motechproject.security.exception.UserNotFoundException) MotechUser(org.motechproject.security.domain.MotechUser) PasswordRecovery(org.motechproject.security.domain.PasswordRecovery) DateTime(org.joda.time.DateTime) Transactional(org.springframework.transaction.annotation.Transactional)

Example 49 with MotechUser

use of org.motechproject.security.domain.MotechUser in project motech by motech.

the class MotechUsersDaoTest method shouldNotAllowDuplicateEmails.

@Test(expected = EmailExistsException.class)
public void shouldNotAllowDuplicateEmails() {
    MotechUser motechUser = new MotechUser("testuser", "testpassword", "test@test.com", "id", asList("ADMIN"), "", Locale.ENGLISH);
    when(usersDataService.findByUserName("testuser")).thenReturn(null);
    when(usersDataService.findByEmail("test@test.com")).thenReturn(motechUser);
    try {
        motechUsersDao.add(motechUser);
    } finally {
        verify(usersDataService).findByUserName("testuser");
        verify(usersDataService).findByEmail("test@test.com");
        verify(usersDataService, never()).create(motechUser);
    }
}
Also used : MotechUser(org.motechproject.security.domain.MotechUser) Test(org.junit.Test)

Example 50 with MotechUser

use of org.motechproject.security.domain.MotechUser in project motech by motech.

the class MotechUsersDaoTest method shouldCreateANewUser.

@Test
public void shouldCreateANewUser() {
    MotechUser motechUser = new MotechUser("testuser", "testpassword", "test@test.com", "id", asList("ADMIN"), "", Locale.ENGLISH);
    when(usersDataService.findByUserName("testuser")).thenReturn(null);
    when(usersDataService.findByEmail("test@test.com")).thenReturn(null);
    motechUsersDao.add(motechUser);
    verify(usersDataService).findByUserName("testuser");
    verify(usersDataService).findByEmail("test@test.com");
    verify(usersDataService).create(motechUser);
}
Also used : MotechUser(org.motechproject.security.domain.MotechUser) Test(org.junit.Test)

Aggregations

MotechUser (org.motechproject.security.domain.MotechUser)61 Test (org.junit.Test)27 Transactional (org.springframework.transaction.annotation.Transactional)24 MotechUserProfile (org.motechproject.security.domain.MotechUserProfile)8 ArrayList (java.util.ArrayList)6 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)6 DateTime (org.joda.time.DateTime)4 PasswordRecovery (org.motechproject.security.domain.PasswordRecovery)4 Authentication (org.springframework.security.core.Authentication)4 User (org.springframework.security.core.userdetails.User)4 HttpSession (javax.servlet.http.HttpSession)3 MotechRole (org.motechproject.security.domain.MotechRole)3 RoleDto (org.motechproject.security.model.RoleDto)3 ConfigAttribute (org.springframework.security.access.ConfigAttribute)3 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)3 AuthenticationException (org.springframework.security.core.AuthenticationException)3 UserNotFoundException (org.motechproject.security.exception.UserNotFoundException)2 UserDto (org.motechproject.security.model.UserDto)2 SecurityConfig (org.springframework.security.access.SecurityConfig)2 AbstractAuthenticationToken (org.springframework.security.authentication.AbstractAuthenticationToken)2