ConsumerRolePermissionService { 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; } ConsumerRolePermissionService(
      final PermissionRepository permissionRepository,
      final ConsumerRoleRepository consumerRoleRepository,
      final RolePermissionRepository rolePermissionRepository); boolean consumerHasPermission(long consumerId, String permissionType, String targetId);  }
@Test @Sql(scripts = "/sql/permission/insert-test-roles.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) @Sql(scripts = "/sql/permission/insert-test-permissions.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) @Sql(scripts = "/sql/permission/insert-test-consumerroles.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) @Sql(scripts = "/sql/permission/insert-test-rolepermissions.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) @Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) public void testConsumerHasPermission() throws Exception { String someTargetId = "someTargetId"; String anotherTargetId = "anotherTargetId"; String somePermissionType = "somePermissionType"; String anotherPermissionType = "anotherPermissionType"; long someConsumerId = 1; long anotherConsumerId = 2; long someConsumerWithNoPermission = 3; assertTrue(consumerRolePermissionService.consumerHasPermission(someConsumerId, somePermissionType, someTargetId)); assertTrue(consumerRolePermissionService.consumerHasPermission(someConsumerId, anotherPermissionType, anotherTargetId)); assertTrue(consumerRolePermissionService.consumerHasPermission(anotherConsumerId, somePermissionType, someTargetId)); assertTrue(consumerRolePermissionService.consumerHasPermission(anotherConsumerId, anotherPermissionType, anotherTargetId)); assertFalse(consumerRolePermissionService.consumerHasPermission(someConsumerWithNoPermission, somePermissionType, someTargetId)); assertFalse(consumerRolePermissionService.consumerHasPermission(someConsumerWithNoPermission, anotherPermissionType, anotherTargetId)); }