-
Notifications
You must be signed in to change notification settings - Fork 0
Add an endpoint filtering the elements a user can access #267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -236,6 +236,17 @@ public ResponseEntity<Void> areElementsAccessible(@RequestParam("ids") List<UUID | |
| return ResponseEntity.ok().build(); | ||
| } | ||
|
|
||
| @GetMapping(value = "/elements/permission", produces = MediaType.APPLICATION_JSON_VALUE) | ||
| @Operation(summary = "Get, among the given elements, the ones the user can access with the given permission") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Filter the given elements to the ones the user has the given permission on |
||
| @ApiResponses(value = { | ||
| @ApiResponse(responseCode = "200", description = "The uuids of the accessible elements"), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The uuids of the given elements the user has the permission on |
||
| }) | ||
| public ResponseEntity<List<UUID>> getAccessibleElements(@RequestParam("ids") List<UUID> elementUuids, | ||
| @RequestParam(value = "accessType") PermissionType permissionType, | ||
| @RequestHeader("userId") String userId) { | ||
| return ResponseEntity.ok().body(permissionService.filterAccessibleElements(userId, elementUuids, permissionType)); | ||
| } | ||
|
|
||
| @GetMapping(value = "/directories/{directoryUuid}/permissions", produces = MediaType.APPLICATION_JSON_VALUE) | ||
| @Operation(summary = "Get permissions for the directory") | ||
| @ApiResponses(value = { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
| import org.gridsuite.directory.server.repository.PermissionRepository; | ||
| import org.springframework.stereotype.Service; | ||
| import java.util.*; | ||
| import java.util.function.Supplier; | ||
| import java.util.stream.Collectors; | ||
| import static org.gridsuite.directory.server.DirectoryService.DIRECTORY; | ||
| import static org.gridsuite.directory.server.dto.PermissionType.MANAGE; | ||
|
|
@@ -70,6 +71,29 @@ public void checkDirectoriesPermission(String userId, List<UUID> elementUuids, U | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Tells which of the given elements the user may access. | ||
| * | ||
| * @param userId User ID checking permissions for | ||
| * @param elementUuids List of element UUIDs to check permissions on | ||
| * @param permissionType Type of permission to check (READ, WRITE, MANAGE) | ||
| * @return the uuids of the accessible elements, in no particular order | ||
| */ | ||
| public List<UUID> filterAccessibleElements(String userId, List<UUID> elementUuids, PermissionType permissionType) { | ||
| boolean isExploreAdmin = roleService.isUserExploreAdmin(); | ||
| //Resolved once for the whole batch: hasElementPermission would otherwise query user-admin-server for | ||
| //every single element. | ||
| List<UUID> userGroupIds = isExploreAdmin ? List.of() : getUserGroupIds(userId); | ||
| return directoryElementRepository.findAllByIdIn(elementUuids).stream() | ||
| //If it's a directory we check its own permission else we check the permission on its parent directory | ||
| .filter(element -> isExploreAdmin || hasElementPermission(userId, | ||
| element.getType().equals(DIRECTORY) ? element.getId() : element.getParentId(), | ||
| permissionType, | ||
| () -> userGroupIds)) | ||
| .map(DirectoryElementEntity::getId) | ||
| .toList(); | ||
| } | ||
|
|
||
| public boolean hasReadPermissions(String userId, List<UUID> elementUuids) { | ||
| return roleService.isUserExploreAdmin() || directoryElementRepository.findAllByIdIn(elementUuids).stream().allMatch(element -> | ||
| //If it's a directory we check its own write permission else we check the permission on the element parent directory | ||
|
|
@@ -204,6 +228,10 @@ private boolean checkPermission(String userId, List<UUID> elementUuids, Permissi | |
| } | ||
|
|
||
| private boolean hasElementPermission(String userId, UUID uuid, PermissionType permissionType) { | ||
| return hasElementPermission(userId, uuid, permissionType, () -> getUserGroupIds(userId)); | ||
| } | ||
|
|
||
| private boolean hasElementPermission(String userId, UUID uuid, PermissionType permissionType, Supplier<List<UUID>> userGroupIds) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. userGroupIdsSupplier |
||
| //Check global permission first | ||
| boolean globalPermission = checkPermission(permissionRepository.findById(new PermissionId(uuid, ALL_USERS, "")), permissionType); | ||
| if (globalPermission) { | ||
|
|
@@ -217,14 +245,17 @@ private boolean hasElementPermission(String userId, UUID uuid, PermissionType pe | |
| } | ||
|
|
||
| //Finally check group permission | ||
| return userAdminService.getUserGroups(userId) | ||
| return userGroupIds.get() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. userGroupIdsSupplier.get() |
||
| .stream() | ||
| .map(UserGroupDTO::id) | ||
| .anyMatch(groupId -> | ||
| checkPermission(permissionRepository.findById(new PermissionId(uuid, "", groupId.toString())), permissionType) | ||
| ); | ||
| } | ||
|
|
||
| private List<UUID> getUserGroupIds(String userId) { | ||
| return userAdminService.getUserGroups(userId).stream().map(UserGroupDTO::id).toList(); | ||
| } | ||
|
|
||
| private boolean checkPermission(Optional<PermissionEntity> permissionEntity, PermissionType permissionType) { | ||
| return permissionEntity | ||
| .map(p -> switch (permissionType) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@GetMapping(value = "/elements/accessible", produces = MediaType.APPLICATION_JSON_VALUE)