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