Search in sources :

Example 1 with RolePermission

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

the class ConsumerRolePermissionService method consumerHasPermission.

/**
 * Check whether user has the permission
 */
public boolean consumerHasPermission(long consumerId, String permissionType, String targetId) {
    Permission permission = permissionRepository.findTopByPermissionTypeAndTargetId(permissionType, targetId);
    if (permission == null) {
        return false;
    }
    List<ConsumerRole> consumerRoles = consumerRoleRepository.findByConsumerId(consumerId);
    if (CollectionUtils.isEmpty(consumerRoles)) {
        return false;
    }
    Set<Long> roleIds = consumerRoles.stream().map(ConsumerRole::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 : ConsumerRole(com.ctrip.framework.apollo.openapi.entity.ConsumerRole) RolePermission(com.ctrip.framework.apollo.portal.entity.po.RolePermission) Permission(com.ctrip.framework.apollo.portal.entity.po.Permission) RolePermission(com.ctrip.framework.apollo.portal.entity.po.RolePermission)

Example 2 with RolePermission

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

the class RolePermissionServiceTest method testCreateRoleWithPermissions.

@Test
@Sql(scripts = "/sql/permission/insert-test-permissions.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testCreateRoleWithPermissions() throws Exception {
    String someRoleName = "someRoleName";
    Role role = assembleRole(someRoleName);
    Set<Long> permissionIds = Sets.newHashSet(990L, 991L);
    Role created = rolePermissionService.createRoleWithPermissions(role, permissionIds);
    Role createdFromDB = roleRepository.findById(created.getId()).orElse(null);
    List<RolePermission> rolePermissions = rolePermissionRepository.findByRoleIdIn(Sets.newHashSet(createdFromDB.getId()));
    Set<Long> rolePermissionIds = rolePermissions.stream().map(RolePermission::getPermissionId).collect(Collectors.toSet());
    assertEquals(someRoleName, createdFromDB.getRoleName());
    assertEquals(2, rolePermissionIds.size());
    assertTrue(rolePermissionIds.containsAll(permissionIds));
}
Also used : UserRole(com.ctrip.framework.apollo.portal.entity.po.UserRole) Role(com.ctrip.framework.apollo.portal.entity.po.Role) RolePermission(com.ctrip.framework.apollo.portal.entity.po.RolePermission) AbstractIntegrationTest(com.ctrip.framework.apollo.portal.AbstractIntegrationTest) Test(org.junit.Test) Sql(org.springframework.test.context.jdbc.Sql)

Example 3 with RolePermission

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

the class DefaultRolePermissionService method createRoleWithPermissions.

/**
 * Create role with permissions, note that role name should be unique
 */
@Transactional
public Role createRoleWithPermissions(Role role, Set<Long> permissionIds) {
    Role current = findRoleByRoleName(role.getRoleName());
    Preconditions.checkState(current == null, "Role %s already exists!", role.getRoleName());
    Role createdRole = roleRepository.save(role);
    if (!CollectionUtils.isEmpty(permissionIds)) {
        Iterable<RolePermission> rolePermissions = permissionIds.stream().map(permissionId -> {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(createdRole.getId());
            rolePermission.setPermissionId(permissionId);
            rolePermission.setDataChangeCreatedBy(createdRole.getDataChangeCreatedBy());
            rolePermission.setDataChangeLastModifiedBy(createdRole.getDataChangeLastModifiedBy());
            return rolePermission;
        }).collect(Collectors.toList());
        rolePermissionRepository.saveAll(rolePermissions);
    }
    return createdRole;
}
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) RolePermission(com.ctrip.framework.apollo.portal.entity.po.RolePermission) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with RolePermission

use of com.ctrip.framework.apollo.portal.entity.po.RolePermission 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

RolePermission (com.ctrip.framework.apollo.portal.entity.po.RolePermission)4 Permission (com.ctrip.framework.apollo.portal.entity.po.Permission)3 UserRole (com.ctrip.framework.apollo.portal.entity.po.UserRole)3 Role (com.ctrip.framework.apollo.portal.entity.po.Role)2 ConsumerRole (com.ctrip.framework.apollo.openapi.entity.ConsumerRole)1 ConsumerRoleRepository (com.ctrip.framework.apollo.openapi.repository.ConsumerRoleRepository)1 AbstractIntegrationTest (com.ctrip.framework.apollo.portal.AbstractIntegrationTest)1 PortalConfig (com.ctrip.framework.apollo.portal.component.config.PortalConfig)1 UserInfo (com.ctrip.framework.apollo.portal.entity.bo.UserInfo)1 PermissionRepository (com.ctrip.framework.apollo.portal.repository.PermissionRepository)1 RolePermissionRepository (com.ctrip.framework.apollo.portal.repository.RolePermissionRepository)1 RoleRepository (com.ctrip.framework.apollo.portal.repository.RoleRepository)1 UserRoleRepository (com.ctrip.framework.apollo.portal.repository.UserRoleRepository)1 RolePermissionService (com.ctrip.framework.apollo.portal.service.RolePermissionService)1 Preconditions (com.google.common.base.Preconditions)1 HashMultimap (com.google.common.collect.HashMultimap)1 Lists (com.google.common.collect.Lists)1 Multimap (com.google.common.collect.Multimap)1 Sets (com.google.common.collect.Sets)1 Collection (java.util.Collection)1