Search in sources :

Example 26 with MotechUser

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

the class EmailSenderImplTest method prepareUser.

private void prepareUser() {
    user = new MotechUser();
    user.setLastPasswordChange(DateUtil.now().minusDays(DAYS_TO_CHANGE_PASSWORD - DAYS_FOR_REMINDER));
    user.setUserName("FooUsername");
    user.setEmail("fooUser@domain.com");
    user.setLocale(Locale.ENGLISH);
}
Also used : MotechUser(org.motechproject.security.domain.MotechUser)

Example 27 with MotechUser

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

the class MotechUsersDao method getUsers.

/**
 * Returns all MotechUsers that comes from
 * {@link LoginMode#REPOSITORY}
 *
 * @return list that contains users
 */
@Transactional
public List<MotechUser> getUsers() {
    List<MotechUser> users = dataService.retrieveAll();
    Iterator<MotechUser> iterator = users.iterator();
    while (iterator.hasNext()) {
        MotechUser user = iterator.next();
        if (isNotBlank(user.getOpenId())) {
            iterator.remove();
        }
    }
    return users;
}
Also used : MotechUser(org.motechproject.security.domain.MotechUser) Transactional(org.springframework.transaction.annotation.Transactional)

Example 28 with MotechUser

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

the class MotechRoleServiceImpl method updateRole.

@Override
@Transactional
public void updateRole(RoleDto role) {
    LOGGER.info("Updating role: {}", role.getRoleName());
    MotechRole motechRole = findByRoleName(role.getOriginalRoleName());
    motechRole.setRoleName(role.getRoleName());
    motechRole.setPermissionNames(role.getPermissionNames());
    List<MotechUser> users = motechUsersDao.findByRole(role.getOriginalRoleName());
    if (StringUtils.equals(role.getRoleName(), role.getOriginalRoleName())) {
        for (MotechUser user : users) {
            List<String> roleList = user.getRoles();
            roleList.remove(role.getOriginalRoleName());
            roleList.add(role.getRoleName());
            motechUsersDao.update(user);
        }
    }
    motechRolesDataService.update(motechRole);
    userContextsService.refreshAllUsersContextIfActive();
    LOGGER.info("Updated role: {}", role.getRoleName());
}
Also used : MotechUser(org.motechproject.security.domain.MotechUser) MotechRole(org.motechproject.security.domain.MotechRole) Transactional(org.springframework.transaction.annotation.Transactional)

Example 29 with MotechUser

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

the class MotechUserServiceImpl method updateUserDetailsWithPassword.

@Override
@Transactional
public void updateUserDetailsWithPassword(UserDto user) {
    if (!StringUtils.isEmpty(user.getPassword())) {
        validatePassword(user.getPassword());
        MotechUser motechUser = motechUsersDao.findByUserName(user.getUserName());
        motechUser.setEmail(user.getEmail());
        motechUser.setUserStatus(user.getUserStatus());
        motechUser.setPassword(passwordEncoder.encode(user.getPassword()));
        motechUser.setRoles(user.getRoles());
        motechUser.setLocale(user.getLocale());
        motechUsersDao.update(motechUser);
        userContextsService.refreshUserContextIfActive(motechUser.getUserName());
    } else {
        throw new IllegalArgumentException("User password cannot be empty. If you wish to omit changing user password, " + "please call method updateUserDetailsWithoutPassword.");
    }
}
Also used : MotechUser(org.motechproject.security.domain.MotechUser) Transactional(org.springframework.transaction.annotation.Transactional)

Example 30 with MotechUser

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

the class MotechUserServiceImpl method changeExpiredPassword.

@Override
@Transactional
public MotechUserProfile changeExpiredPassword(String userName, String oldPassword, String newPassword) {
    MotechUser motechUser = motechUsersDao.findByUserName(userName);
    validatePassword(newPassword);
    if (motechUser != null && UserStatus.MUST_CHANGE_PASSWORD.equals(motechUser.getUserStatus()) && passwordEncoder.isPasswordValid(motechUser.getPassword(), oldPassword)) {
        // The new password and the old password cannot be the same
        if (passwordEncoder.isPasswordValid(motechUser.getPassword(), newPassword)) {
            return null;
        }
        motechUser.setPassword(passwordEncoder.encodePassword(newPassword));
        motechUser.setUserStatus(UserStatus.ACTIVE);
        motechUsersDao.update(motechUser);
        return new MotechUserProfile(motechUser);
    } else {
        // Wrong password
        incrementFailureLoginCount(motechUser);
    }
    return null;
}
Also used : MotechUser(org.motechproject.security.domain.MotechUser) MotechUserProfile(org.motechproject.security.domain.MotechUserProfile) 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