Search in sources :

Example 31 with MotechUser

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

the class MotechUserServiceImpl method updateUserDetailsWithoutPassword.

@Override
@Transactional
public void updateUserDetailsWithoutPassword(UserDto user) {
    MotechUser motechUser = motechUsersDao.findByUserName(user.getUserName());
    motechUser.setEmail(user.getEmail());
    motechUser.setUserStatus(user.getUserStatus());
    motechUser.setRoles(user.getRoles());
    motechUser.setLocale(user.getLocale());
    motechUsersDao.update(motechUser);
    userContextsService.refreshUserContextIfActive(motechUser.getUserName());
}
Also used : MotechUser(org.motechproject.security.domain.MotechUser) Transactional(org.springframework.transaction.annotation.Transactional)

Example 32 with MotechUser

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

the class MotechUserServiceImpl method register.

@Override
@Transactional
public // NO CHECKSTYLE More than 7 parameters (found 8).
void register(// NO CHECKSTYLE More than 7 parameters (found 8).
String username, // NO CHECKSTYLE More than 7 parameters (found 8).
String password, String email, String externalId, List<String> roles, Locale locale, UserStatus userStatus, String openId) {
    LOGGER.info("Registering new user: {}", username);
    if (isBlank(username) || isBlank(password)) {
        throw new IllegalArgumentException("Username or password cannot be empty");
    }
    validatePassword(password);
    String encodePassword = passwordEncoder.encodePassword(password);
    MotechUser user = new MotechUser(username, encodePassword, email, externalId, roles, openId, locale);
    user.setUserStatus(userStatus);
    motechUsersDao.add(user);
    LOGGER.info("Registered new user: {}", username);
}
Also used : MotechUser(org.motechproject.security.domain.MotechUser) Transactional(org.springframework.transaction.annotation.Transactional)

Example 33 with MotechUser

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

the class MotechUserServiceImpl method activateUser.

@Override
@Transactional
public void activateUser(String username) {
    LOGGER.info("Activating user: {}", username);
    MotechUser motechUser = motechUsersDao.findByUserName(username);
    if (motechUser != null) {
        motechUser.setUserStatus(UserStatus.ACTIVE);
        motechUsersDao.update(motechUser);
    }
    LOGGER.info("Activated user: {}", username);
}
Also used : MotechUser(org.motechproject.security.domain.MotechUser) Transactional(org.springframework.transaction.annotation.Transactional)

Example 34 with MotechUser

use of org.motechproject.security.domain.MotechUser 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 35 with MotechUser

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

the class MotechUsersDao method update.

/**
 * Updates given MotechUser as long as his email is not used by another user
 *
 * @param motechUser to be updated
 */
@Transactional
public void update(MotechUser motechUser) {
    String email = motechUser.getEmail();
    MotechUser otherWithSameEmail = findUserByEmail(email);
    if (null != otherWithSameEmail && !otherWithSameEmail.getUserName().equals(motechUser.getUserName())) {
        throw new EmailExistsException("User with email " + email + " already exists");
    }
    dataService.update(motechUser);
}
Also used : MotechUser(org.motechproject.security.domain.MotechUser) EmailExistsException(org.motechproject.security.exception.EmailExistsException) 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