Search in sources :

Example 16 with MotechUser

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

the class MotechUserServiceBundleIT method shouldChangePassword.

@Test
public void shouldChangePassword() {
    motechUserService.register("userName", "password", "1234", "", asList("IT_ADMIN", "DB_ADMIN"), Locale.ENGLISH);
    motechUserService.changePassword("userName", "password", "newPassword");
    MotechUser motechUser = usersDataService.findByUserName("userName");
    assertTrue(passwordEncoder.isPasswordValid(motechUser.getPassword(), "newPassword"));
}
Also used : MotechUser(org.motechproject.security.domain.MotechUser) Test(org.junit.Test)

Example 17 with MotechUser

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

the class MotechUserServiceBundleIT method shouldCreateBlockedUser.

@Test
public void shouldCreateBlockedUser() {
    motechUserService.register("userName", "password", "1234", "", asList("IT_ADMIN", "DB_ADMIN"), Locale.ENGLISH, UserStatus.BLOCKED, null);
    MotechUser motechUser = usersDataService.findByUserName("userName");
    assertEquals(UserStatus.BLOCKED, motechUser.getUserStatus());
}
Also used : MotechUser(org.motechproject.security.domain.MotechUser) Test(org.junit.Test)

Example 18 with MotechUser

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

the class MotechUserServiceBundleIT method shouldActivateUser.

@Test
public void shouldActivateUser() {
    motechUserService.register("userName", "password", "1234", "", asList("IT_ADMIN", "DB_ADMIN"), Locale.ENGLISH, UserStatus.BLOCKED, null);
    motechUserService.activateUser("userName");
    MotechUser motechUser = usersDataService.findByUserName("userName");
    assertEquals(UserStatus.ACTIVE, motechUser.getUserStatus());
}
Also used : MotechUser(org.motechproject.security.domain.MotechUser) Test(org.junit.Test)

Example 19 with MotechUser

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

the class MotechUserServiceBundleIT method shouldNotChangePasswordWithoutOldPassword.

@Test
public void shouldNotChangePasswordWithoutOldPassword() {
    motechUserService.register("userName", "password", "1234", "", asList("IT_ADMIN", "DB_ADMIN"), Locale.ENGLISH);
    motechUserService.changePassword("userName", "foo", "newPassword");
    MotechUser motechUser = usersDataService.findByUserName("userName");
    assertTrue(passwordEncoder.isPasswordValid(motechUser.getPassword(), "password"));
}
Also used : MotechUser(org.motechproject.security.domain.MotechUser) Test(org.junit.Test)

Example 20 with MotechUser

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

the class MotechLoginErrorHandler method onAuthenticationFailure.

@Override
@Transactional
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
    // Wrong password or username
    if (exception instanceof BadCredentialsException) {
        MotechUser motechUser = motechUsersDao.findByUserName(exception.getAuthentication().getName());
        int failureLoginLimit = settingService.getFailureLoginLimit();
        if (motechUser != null && failureLoginLimit > 0) {
            int failureLoginCounter = motechUser.getFailureLoginCounter();
            failureLoginCounter++;
            if (failureLoginCounter > failureLoginLimit && motechUser.isActive()) {
                motechUser.setUserStatus(UserStatus.BLOCKED);
                failureLoginCounter = 0;
                LOGGER.debug("User {} has been blocked", motechUser.getUserName());
            }
            motechUser.setFailureLoginCounter(failureLoginCounter);
            motechUsersDao.update(motechUser);
        }
        if (motechUser != null && !motechUser.isActive()) {
            LOGGER.debug("Redirecting to " + userBlockedUrl);
            redirectStrategy.sendRedirect(request, response, userBlockedUrl);
            return;
        }
    }
    super.onAuthenticationFailure(request, response, exception);
}
Also used : MotechUser(org.motechproject.security.domain.MotechUser) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) Transactional(org.springframework.transaction.annotation.Transactional)

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