Search in sources :

Example 6 with MotechRole

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

the class MotechRoleServiceImpl method createRole.

@Override
@Transactional
public void createRole(RoleDto role) {
    if (findByRoleName(role.getRoleName()) == null) {
        LOGGER.info("Creating role: {}", role.getRoleName());
        MotechRole motechRole = new MotechRole(role.getRoleName(), role.getPermissionNames(), role.isDeletable());
        motechRolesDataService.create(motechRole);
        userContextsService.refreshAllUsersContextIfActive();
        LOGGER.info("Created role: {}", role.getRoleName());
    } else {
        LOGGER.info("A role with name {} alredy exists. The role has not been added.", role.getRoleName());
    }
}
Also used : MotechRole(org.motechproject.security.domain.MotechRole) Transactional(org.springframework.transaction.annotation.Transactional)

Example 7 with MotechRole

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

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

the class MotechRoleServiceImpl method deleteRole.

@Override
@Transactional
public void deleteRole(RoleDto role) {
    LOGGER.info("Deleting role: {}", role.getRoleName());
    MotechRole motechRole = findByRoleName(role.getRoleName());
    if (motechRole.isDeletable()) {
        List<MotechUser> users = motechUsersDao.findByRole(role.getRoleName());
        if (!users.isEmpty()) {
            throw new RoleHasUserException("Role cannot be deleted because a user has the role.");
        }
        motechRolesDataService.delete(motechRole);
        userContextsService.refreshAllUsersContextIfActive();
        LOGGER.info("Deleted role: {}", role);
    } else {
        LOGGER.warn("The role {} cant be deleted", role.getRoleName());
    }
}
Also used : MotechUser(org.motechproject.security.domain.MotechUser) RoleHasUserException(org.motechproject.security.exception.RoleHasUserException) MotechRole(org.motechproject.security.domain.MotechRole) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

MotechRole (org.motechproject.security.domain.MotechRole)8 Test (org.junit.Test)4 MotechUser (org.motechproject.security.domain.MotechUser)3 RoleDto (org.motechproject.security.model.RoleDto)3 Transactional (org.springframework.transaction.annotation.Transactional)3 Before (org.junit.Before)1 RoleHasUserException (org.motechproject.security.exception.RoleHasUserException)1 MotechURLSecurityService (org.motechproject.security.service.MotechURLSecurityService)1 AuthenticationManager (org.springframework.security.authentication.AuthenticationManager)1 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)1 UserDetails (org.springframework.security.core.userdetails.UserDetails)1