Search in sources :

Example 1 with UserRole

use of com.ctrip.framework.apollo.portal.entity.po.UserRole in project apollo by ctripcorp.

the class DefaultRolePermissionService method removeRoleFromUsers.

/**
 * Remove role from users
 */
@Transactional
public void removeRoleFromUsers(String roleName, Set<String> userIds, String operatorUserId) {
    Role role = findRoleByRoleName(roleName);
    Preconditions.checkState(role != null, "Role %s doesn't exist!", roleName);
    List<UserRole> existedUserRoles = userRoleRepository.findByUserIdInAndRoleId(userIds, role.getId());
    for (UserRole userRole : existedUserRoles) {
        userRole.setDeleted(true);
        userRole.setDataChangeLastModifiedTime(new Date());
        userRole.setDataChangeLastModifiedBy(operatorUserId);
    }
    userRoleRepository.saveAll(existedUserRoles);
}
Also used : Role(com.ctrip.framework.apollo.portal.entity.po.Role) UserRole(com.ctrip.framework.apollo.portal.entity.po.UserRole) UserRole(com.ctrip.framework.apollo.portal.entity.po.UserRole) Date(java.util.Date) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with UserRole

use of com.ctrip.framework.apollo.portal.entity.po.UserRole in project apollo by ctripcorp.

the class RolePermissionServiceTest method testAssignRoleToUsersWithUserRolesExisted.

@Test
@Sql(scripts = "/sql/permission/insert-test-roles.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/permission/insert-test-userroles.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testAssignRoleToUsersWithUserRolesExisted() throws Exception {
    String someRoleName = "someRoleName";
    String someUser = "someUser";
    String anotherUser = "anotherUser";
    String operator = "operator";
    Set<String> users = Sets.newHashSet(someUser, anotherUser);
    rolePermissionService.assignRoleToUsers(someRoleName, users, operator);
    List<UserRole> userRoles = userRoleRepository.findByRoleId(990);
    Set<String> usersWithRole = Sets.newHashSet();
    for (UserRole userRole : userRoles) {
        assertEquals("someOperator", userRole.getDataChangeCreatedBy());
        assertEquals("someOperator", userRole.getDataChangeLastModifiedBy());
        usersWithRole.add(userRole.getUserId());
    }
    assertEquals(2, usersWithRole.size());
    assertTrue(usersWithRole.containsAll(users));
}
Also used : UserRole(com.ctrip.framework.apollo.portal.entity.po.UserRole) AbstractIntegrationTest(com.ctrip.framework.apollo.portal.AbstractIntegrationTest) Test(org.junit.Test) Sql(org.springframework.test.context.jdbc.Sql)

Example 3 with UserRole

use of com.ctrip.framework.apollo.portal.entity.po.UserRole in project apollo by ctripcorp.

the class RolePermissionServiceTest method testAssignRoleToUsers.

@Test
@Sql(scripts = "/sql/permission/insert-test-roles.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testAssignRoleToUsers() throws Exception {
    String someRoleName = "someRoleName";
    String someUser = "someUser";
    String anotherUser = "anotherUser";
    String operator = "operator";
    Set<String> users = Sets.newHashSet(someUser, anotherUser);
    rolePermissionService.assignRoleToUsers(someRoleName, users, operator);
    List<UserRole> userRoles = userRoleRepository.findByRoleId(990);
    Set<String> usersWithRole = Sets.newHashSet();
    for (UserRole userRole : userRoles) {
        assertEquals(operator, userRole.getDataChangeCreatedBy());
        assertEquals(operator, userRole.getDataChangeLastModifiedBy());
        usersWithRole.add(userRole.getUserId());
    }
    assertEquals(2, usersWithRole.size());
    assertTrue(usersWithRole.containsAll(users));
}
Also used : UserRole(com.ctrip.framework.apollo.portal.entity.po.UserRole) AbstractIntegrationTest(com.ctrip.framework.apollo.portal.AbstractIntegrationTest) Test(org.junit.Test) Sql(org.springframework.test.context.jdbc.Sql)

Example 4 with UserRole

use of com.ctrip.framework.apollo.portal.entity.po.UserRole in project apollo by ctripcorp.

the class DefaultRolePermissionService method assignRoleToUsers.

/**
 * Assign role to users
 *
 * @return the users assigned roles
 */
@Transactional
public Set<String> assignRoleToUsers(String roleName, Set<String> userIds, String operatorUserId) {
    Role role = findRoleByRoleName(roleName);
    Preconditions.checkState(role != null, "Role %s doesn't exist!", roleName);
    List<UserRole> existedUserRoles = userRoleRepository.findByUserIdInAndRoleId(userIds, role.getId());
    Set<String> existedUserIds = existedUserRoles.stream().map(UserRole::getUserId).collect(Collectors.toSet());
    Set<String> toAssignUserIds = Sets.difference(userIds, existedUserIds);
    Iterable<UserRole> toCreate = toAssignUserIds.stream().map(userId -> {
        UserRole userRole = new UserRole();
        userRole.setRoleId(role.getId());
        userRole.setUserId(userId);
        userRole.setDataChangeCreatedBy(operatorUserId);
        userRole.setDataChangeLastModifiedBy(operatorUserId);
        return userRole;
    }).collect(Collectors.toList());
    userRoleRepository.saveAll(toCreate);
    return toAssignUserIds;
}
Also used : Role(com.ctrip.framework.apollo.portal.entity.po.Role) UserRole(com.ctrip.framework.apollo.portal.entity.po.UserRole) Date(java.util.Date) Role(com.ctrip.framework.apollo.portal.entity.po.Role) Autowired(org.springframework.beans.factory.annotation.Autowired) Multimap(com.google.common.collect.Multimap) Permission(com.ctrip.framework.apollo.portal.entity.po.Permission) UserInfo(com.ctrip.framework.apollo.portal.entity.bo.UserInfo) HashMultimap(com.google.common.collect.HashMultimap) Lists(com.google.common.collect.Lists) StreamSupport(java.util.stream.StreamSupport) RolePermissionService(com.ctrip.framework.apollo.portal.service.RolePermissionService) UserRole(com.ctrip.framework.apollo.portal.entity.po.UserRole) PortalConfig(com.ctrip.framework.apollo.portal.component.config.PortalConfig) RolePermission(com.ctrip.framework.apollo.portal.entity.po.RolePermission) Collection(java.util.Collection) Set(java.util.Set) ConsumerRoleRepository(com.ctrip.framework.apollo.openapi.repository.ConsumerRoleRepository) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) List(java.util.List) UserRoleRepository(com.ctrip.framework.apollo.portal.repository.UserRoleRepository) RolePermissionRepository(com.ctrip.framework.apollo.portal.repository.RolePermissionRepository) PermissionRepository(com.ctrip.framework.apollo.portal.repository.PermissionRepository) CollectionUtils(org.springframework.util.CollectionUtils) Preconditions(com.google.common.base.Preconditions) RoleRepository(com.ctrip.framework.apollo.portal.repository.RoleRepository) Collections(java.util.Collections) Transactional(org.springframework.transaction.annotation.Transactional) UserRole(com.ctrip.framework.apollo.portal.entity.po.UserRole) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with UserRole

use of com.ctrip.framework.apollo.portal.entity.po.UserRole in project apollo by ctripcorp.

the class DefaultRolePermissionService method userHasPermission.

/**
 * Check whether user has the permission
 */
public boolean userHasPermission(String userId, String permissionType, String targetId) {
    Permission permission = permissionRepository.findTopByPermissionTypeAndTargetId(permissionType, targetId);
    if (permission == null) {
        return false;
    }
    if (isSuperAdmin(userId)) {
        return true;
    }
    List<UserRole> userRoles = userRoleRepository.findByUserId(userId);
    if (CollectionUtils.isEmpty(userRoles)) {
        return false;
    }
    Set<Long> roleIds = userRoles.stream().map(UserRole::getRoleId).collect(Collectors.toSet());
    List<RolePermission> rolePermissions = rolePermissionRepository.findByRoleIdIn(roleIds);
    if (CollectionUtils.isEmpty(rolePermissions)) {
        return false;
    }
    for (RolePermission rolePermission : rolePermissions) {
        if (rolePermission.getPermissionId() == permission.getId()) {
            return true;
        }
    }
    return false;
}
Also used : UserRole(com.ctrip.framework.apollo.portal.entity.po.UserRole) Permission(com.ctrip.framework.apollo.portal.entity.po.Permission) RolePermission(com.ctrip.framework.apollo.portal.entity.po.RolePermission) RolePermission(com.ctrip.framework.apollo.portal.entity.po.RolePermission)

Aggregations

UserRole (com.ctrip.framework.apollo.portal.entity.po.UserRole)6 Permission (com.ctrip.framework.apollo.portal.entity.po.Permission)3 Role (com.ctrip.framework.apollo.portal.entity.po.Role)3 RolePermission (com.ctrip.framework.apollo.portal.entity.po.RolePermission)3 Date (java.util.Date)3 Transactional (org.springframework.transaction.annotation.Transactional)3 ConsumerRoleRepository (com.ctrip.framework.apollo.openapi.repository.ConsumerRoleRepository)2 AbstractIntegrationTest (com.ctrip.framework.apollo.portal.AbstractIntegrationTest)2 PortalConfig (com.ctrip.framework.apollo.portal.component.config.PortalConfig)2 UserInfo (com.ctrip.framework.apollo.portal.entity.bo.UserInfo)2 PermissionRepository (com.ctrip.framework.apollo.portal.repository.PermissionRepository)2 RolePermissionRepository (com.ctrip.framework.apollo.portal.repository.RolePermissionRepository)2 RoleRepository (com.ctrip.framework.apollo.portal.repository.RoleRepository)2 UserRoleRepository (com.ctrip.framework.apollo.portal.repository.UserRoleRepository)2 RolePermissionService (com.ctrip.framework.apollo.portal.service.RolePermissionService)2 Preconditions (com.google.common.base.Preconditions)2 HashMultimap (com.google.common.collect.HashMultimap)2 Lists (com.google.common.collect.Lists)2 Multimap (com.google.common.collect.Multimap)2 Sets (com.google.common.collect.Sets)2