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;
}
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);
}
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));
}
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;
}
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)));
}
Aggregations