Search in sources :

Example 1 with RoleHasUserException

use of org.motechproject.security.exception.RoleHasUserException 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)1 MotechUser (org.motechproject.security.domain.MotechUser)1 RoleHasUserException (org.motechproject.security.exception.RoleHasUserException)1 Transactional (org.springframework.transaction.annotation.Transactional)1