Search in sources :

Example 1 with Permission

use of com.ctrip.framework.apollo.portal.entity.po.Permission 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 = FluentIterable.from(consumerRoles).transform(consumerRole -> consumerRole.getRoleId()).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 : List(java.util.List) FluentIterable(com.google.common.collect.FluentIterable) RolePermissionRepository(com.ctrip.framework.apollo.portal.repository.RolePermissionRepository) PermissionRepository(com.ctrip.framework.apollo.portal.repository.PermissionRepository) Service(org.springframework.stereotype.Service) CollectionUtils(org.springframework.util.CollectionUtils) RolePermission(com.ctrip.framework.apollo.portal.entity.po.RolePermission) Autowired(org.springframework.beans.factory.annotation.Autowired) Set(java.util.Set) ConsumerRoleRepository(com.ctrip.framework.apollo.openapi.repository.ConsumerRoleRepository) ConsumerRole(com.ctrip.framework.apollo.openapi.entity.ConsumerRole) Permission(com.ctrip.framework.apollo.portal.entity.po.Permission) 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 Permission

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

the class RolePermissionServiceTest method testCreatePermissionWithPermissionExisted.

@Test(expected = IllegalStateException.class)
@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 testCreatePermissionWithPermissionExisted() throws Exception {
    String someTargetId = "someTargetId";
    String somePermissionType = "somePermissionType";
    Permission somePermission = assemblePermission(somePermissionType, someTargetId);
    rolePermissionService.createPermission(somePermission);
}
Also used : RolePermission(com.ctrip.framework.apollo.portal.entity.po.RolePermission) Permission(com.ctrip.framework.apollo.portal.entity.po.Permission) AbstractIntegrationTest(com.ctrip.framework.apollo.portal.AbstractIntegrationTest) Test(org.junit.Test) Sql(org.springframework.test.context.jdbc.Sql)

Example 3 with Permission

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

the class RolePermissionServiceTest method testCreatePermissionsWithPermissionsExisted.

@Test(expected = IllegalStateException.class)
@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 testCreatePermissionsWithPermissionsExisted() throws Exception {
    String someTargetId = "someTargetId";
    String anotherTargetId = "anotherTargetId";
    String somePermissionType = "somePermissionType";
    String anotherPermissionType = "anotherPermissionType";
    Permission somePermission = assemblePermission(somePermissionType, someTargetId);
    Permission anotherPermission = assemblePermission(anotherPermissionType, anotherTargetId);
    rolePermissionService.createPermissions(Sets.newHashSet(somePermission, anotherPermission));
}
Also used : RolePermission(com.ctrip.framework.apollo.portal.entity.po.RolePermission) Permission(com.ctrip.framework.apollo.portal.entity.po.Permission) AbstractIntegrationTest(com.ctrip.framework.apollo.portal.AbstractIntegrationTest) Test(org.junit.Test) Sql(org.springframework.test.context.jdbc.Sql)

Example 4 with Permission

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

the class RolePermissionServiceTest method assemblePermission.

private Permission assemblePermission(String permissionType, String targetId) {
    Permission permission = new Permission();
    permission.setPermissionType(permissionType);
    permission.setTargetId(targetId);
    permission.setDataChangeCreatedBy(someCreatedBy);
    permission.setDataChangeLastModifiedBy(someCreatedBy);
    return permission;
}
Also used : RolePermission(com.ctrip.framework.apollo.portal.entity.po.RolePermission) Permission(com.ctrip.framework.apollo.portal.entity.po.Permission)

Example 5 with Permission

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

the class RolePermissionServiceTest method testCreatePermissions.

@Test
@Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testCreatePermissions() throws Exception {
    String someTargetId = "someTargetId";
    String anotherTargetId = "anotherTargetId";
    String somePermissionType = "somePermissionType";
    String anotherPermissionType = "anotherPermissionType";
    Permission somePermission = assemblePermission(somePermissionType, someTargetId);
    Permission anotherPermission = assemblePermission(anotherPermissionType, anotherTargetId);
    Set<Permission> created = rolePermissionService.createPermissions(Sets.newHashSet(somePermission, anotherPermission));
    Set<Long> permissionIds = FluentIterable.from(created).transform(BaseEntity::getId).toSet();
    Iterable<Permission> permissionsFromDB = permissionRepository.findAll(permissionIds);
    Set<String> targetIds = Sets.newHashSet();
    Set<String> permissionTypes = Sets.newHashSet();
    for (Permission permission : permissionsFromDB) {
        targetIds.add(permission.getTargetId());
        permissionTypes.add(permission.getPermissionType());
    }
    assertEquals(2, targetIds.size());
    assertEquals(2, permissionTypes.size());
    assertTrue(targetIds.containsAll(Sets.newHashSet(someTargetId, anotherTargetId)));
    assertTrue(permissionTypes.containsAll(Sets.newHashSet(somePermissionType, anotherPermissionType)));
}
Also used : RolePermission(com.ctrip.framework.apollo.portal.entity.po.RolePermission) Permission(com.ctrip.framework.apollo.portal.entity.po.Permission) AbstractIntegrationTest(com.ctrip.framework.apollo.portal.AbstractIntegrationTest) Test(org.junit.Test) Sql(org.springframework.test.context.jdbc.Sql)

Aggregations

Permission (com.ctrip.framework.apollo.portal.entity.po.Permission)13 RolePermission (com.ctrip.framework.apollo.portal.entity.po.RolePermission)9 AbstractIntegrationTest (com.ctrip.framework.apollo.portal.AbstractIntegrationTest)4 Test (org.junit.Test)4 Sql (org.springframework.test.context.jdbc.Sql)4 Transactional (org.springframework.transaction.annotation.Transactional)4 Role (com.ctrip.framework.apollo.portal.entity.po.Role)3 FluentIterable (com.google.common.collect.FluentIterable)3 Autowired (org.springframework.beans.factory.annotation.Autowired)3 PermissionRepository (com.ctrip.framework.apollo.portal.repository.PermissionRepository)2 RolePermissionRepository (com.ctrip.framework.apollo.portal.repository.RolePermissionRepository)2 RolePermissionService (com.ctrip.framework.apollo.portal.service.RolePermissionService)2 Sets (com.google.common.collect.Sets)2 Set (java.util.Set)2 CollectionUtils (org.springframework.util.CollectionUtils)2 App (com.ctrip.framework.apollo.common.entity.App)1 ConfigConsts (com.ctrip.framework.apollo.core.ConfigConsts)1 ConsumerRole (com.ctrip.framework.apollo.openapi.entity.ConsumerRole)1 ConsumerRoleRepository (com.ctrip.framework.apollo.openapi.repository.ConsumerRoleRepository)1 PortalConfig (com.ctrip.framework.apollo.portal.component.config.PortalConfig)1