-
Notifications
You must be signed in to change notification settings - Fork 0
Homogenize permissions checks #215
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
Changes from 10 commits
9b2b559
53dc492
543dac3
b9ea629
49b9bbb
6b84bbb
e8d5488
b3bf66a
0af499b
d4ef8f0
32bea87
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 |
|---|---|---|
|
|
@@ -248,11 +248,13 @@ public void createElementInDirectoryPath(String directoryPath, ElementAttributes | |
| insertElement(elementAttributes, parentDirectoryUuid); | ||
| } | ||
|
|
||
| private Map<UUID, Long> getSubDirectoriesCounts(List<UUID> subDirectories, List<String> types) { | ||
| List<DirectoryElementRepository.SubDirectoryCount> subdirectoriesCountsList = repositoryService.getSubDirectoriesCounts(subDirectories, types); | ||
| Map<UUID, Long> subdirectoriesCountsMap = new HashMap<>(); | ||
| subdirectoriesCountsList.forEach(e -> subdirectoriesCountsMap.put(e.getId(), e.getCount())); | ||
| return subdirectoriesCountsMap; | ||
| private Map<UUID, Long> getSubDirectoriesCounts(List<UUID> subDirectories, List<String> types, String userId) { | ||
| return repositoryService.findAllByParentIdInAndTypeIn(subDirectories, types).stream() | ||
| .filter(child -> hasReadPermissions(userId, List.of(child.getId()))) | ||
| .collect(Collectors.groupingBy( | ||
| DirectoryElementRepository.ElementParentage::getParentId, | ||
| Collectors.counting() | ||
| )); | ||
| } | ||
|
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. I think we can do differently: rather than finding all children in N directories (each child will be checked by checking its parent directory), we could first eliminate the non-readable dirs among the N, and then retrieve the children ? 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. In our current uses cases it might be a bit overkill because we already check directories permissions in functions calling getSubDirectoriesCounts but I reckon it's still worth because by adding it here too we're avoiding future permissions mistakes related to it if it gets involved in new use cases |
||
|
|
||
| public List<ElementAttributes> getDirectoryElements(UUID directoryUuid, List<String> types, Boolean recursive, String userId) { | ||
|
|
@@ -270,25 +272,25 @@ public List<ElementAttributes> getDirectoryElements(UUID directoryUuid, List<Str | |
| List<DirectoryElementEntity> descendents = repositoryService.findAllDescendants(directoryUuid).stream().toList(); | ||
| return descendents | ||
| .stream() | ||
| .filter(e -> types.isEmpty() || types.contains(e.getType())) | ||
| .filter(e -> (types.isEmpty() || types.contains(e.getType())) && hasReadPermissions(userId, List.of(e.getId()))) | ||
| .map(ElementAttributes::toElementAttributes) | ||
| .toList(); | ||
| } else { | ||
| return getAllDirectoryElementsStream(directoryUuid, types).toList(); | ||
| return getAllDirectoryElementsStream(directoryUuid, types, userId).toList(); | ||
| } | ||
| } | ||
|
|
||
| private Stream<ElementAttributes> getOnlyElementsStream(UUID directoryUuid, List<String> types) { | ||
| return getAllDirectoryElementsStream(directoryUuid, types) | ||
| private Stream<ElementAttributes> getOnlyElementsStream(UUID directoryUuid, List<String> types, String userId) { | ||
| return getAllDirectoryElementsStream(directoryUuid, types, userId) | ||
| .filter(elementAttributes -> !elementAttributes.getType().equals(DIRECTORY)); | ||
| } | ||
|
|
||
| private Stream<ElementAttributes> getAllDirectoryElementsStream(UUID directoryUuid, List<String> types) { | ||
| private Stream<ElementAttributes> getAllDirectoryElementsStream(UUID directoryUuid, List<String> types, String userId) { | ||
| List<DirectoryElementEntity> directoryElements = repositoryService.findAllByParentId(directoryUuid); | ||
| Map<UUID, Long> subdirectoriesCountsMap = getSubDirectoriesCountsMap(types, directoryElements); | ||
| Map<UUID, Long> subdirectoriesCountsMap = getSubDirectoriesCountsMap(types, directoryElements, userId); | ||
| return directoryElements | ||
| .stream() | ||
| .filter(e -> e.getType().equals(DIRECTORY) || types.isEmpty() || types.contains(e.getType())) | ||
| .filter(e -> (e.getType().equals(DIRECTORY) || types.isEmpty() || types.contains(e.getType())) && hasReadPermissions(userId, List.of(e.getId()))) | ||
| .map(e -> toElementAttributes(e, subdirectoriesCountsMap.getOrDefault(e.getId(), 0L))); | ||
| } | ||
|
|
||
|
|
@@ -299,21 +301,21 @@ public List<ElementAttributes> getRootDirectories(List<String> types, String use | |
| if (!roleService.isUserExploreAdmin()) { | ||
| directoryElements = directoryElements.stream().filter(directoryElementEntity -> hasReadPermissions(userId, List.of(directoryElementEntity.getId()))).toList(); | ||
| } | ||
| Map<UUID, Long> subdirectoriesCountsMap = getSubDirectoriesCountsMap(types, directoryElements); | ||
| Map<UUID, Long> subdirectoriesCountsMap = getSubDirectoriesCountsMap(types, directoryElements, userId); | ||
| return directoryElements.stream() | ||
| .map(e -> toElementAttributes(e, subdirectoriesCountsMap.getOrDefault(e.getId(), 0L))) | ||
| .toList(); | ||
| } | ||
|
|
||
| private Map<UUID, Long> getSubDirectoriesCountsMap(List<String> types, List<DirectoryElementEntity> directoryElements) { | ||
| return getSubDirectoriesCounts(directoryElements.stream().map(DirectoryElementEntity::getId).toList(), types); | ||
| private Map<UUID, Long> getSubDirectoriesCountsMap(List<String> types, List<DirectoryElementEntity> directoryElements, String userId) { | ||
| return getSubDirectoriesCounts(directoryElements.stream().map(DirectoryElementEntity::getId).toList(), types, userId); | ||
| } | ||
|
|
||
| public void updateElement(UUID elementUuid, ElementAttributes newElementAttributes, String userId) { | ||
| DirectoryElementEntity directoryElement = getDirectoryElementEntity(elementUuid); | ||
| if (!directoryElement.isAttributesUpdatable(newElementAttributes, userId) || | ||
| !directoryElement.getName().equals(newElementAttributes.getElementName()) && | ||
| directoryHasElementOfNameAndType(directoryElement.getParentId(), newElementAttributes.getElementName(), directoryElement.getType())) { | ||
| directoryHasElementOfNameAndType(directoryElement.getParentId(), newElementAttributes.getElementName(), directoryElement.getType(), userId)) { | ||
| throw new DirectoryException(NOT_ALLOWED); | ||
| } | ||
|
|
||
|
|
@@ -350,7 +352,7 @@ private void moveElementDirectory(DirectoryElementEntity element, UUID newDirect | |
| List<DirectoryElementEntity> descendents = isDirectory ? repositoryService.findAllDescendants(element.getId()).stream().toList() : List.of(); | ||
|
|
||
| // validate move elements | ||
| validateElementForMove(element, newDirectoryUuid, descendents.stream().map(DirectoryElementEntity::getId).collect(Collectors.toSet())); | ||
| validateElementForMove(element, newDirectoryUuid, descendents.stream().map(DirectoryElementEntity::getId).collect(Collectors.toSet()), userId); | ||
|
|
||
| // we update the parent of the moving element | ||
| updateElementParentDirectory(element, newDirectoryUuid); | ||
|
|
@@ -369,12 +371,12 @@ private void moveElementDirectory(DirectoryElementEntity element, UUID newDirect | |
|
|
||
| } | ||
|
|
||
| private void validateElementForMove(DirectoryElementEntity element, UUID newDirectoryUuid, Set<UUID> descendentsUuids) { | ||
| private void validateElementForMove(DirectoryElementEntity element, UUID newDirectoryUuid, Set<UUID> descendentsUuids, String userId) { | ||
| if (newDirectoryUuid == element.getId() || descendentsUuids.contains(newDirectoryUuid)) { | ||
| throw new DirectoryException(MOVE_IN_DESCENDANT_NOT_ALLOWED); | ||
| } | ||
|
|
||
| if (directoryHasElementOfNameAndType(newDirectoryUuid, element.getName(), element.getType())) { | ||
| if (directoryHasElementOfNameAndType(newDirectoryUuid, element.getName(), element.getType(), userId)) { | ||
| throw DirectoryException.createElementNameAlreadyExists(element.getName()); | ||
| } | ||
| } | ||
|
|
@@ -393,8 +395,8 @@ private void validateNewDirectory(UUID newDirectoryUuid) { | |
| } | ||
| } | ||
|
|
||
| private boolean directoryHasElementOfNameAndType(UUID directoryUUID, String elementName, String elementType) { | ||
| return getOnlyElementsStream(directoryUUID, List.of(elementType)) | ||
| private boolean directoryHasElementOfNameAndType(UUID directoryUUID, String elementName, String elementType, String userId) { | ||
| return getOnlyElementsStream(directoryUUID, List.of(elementType), userId) | ||
| .anyMatch( | ||
| e -> e.getElementName().equals(elementName) | ||
| ); | ||
|
|
@@ -429,7 +431,7 @@ private void deleteElement(ElementAttributes elementAttributes, String userId) { | |
| } | ||
|
|
||
| private void deleteSubElements(UUID elementUuid, String userId) { | ||
| getAllDirectoryElementsStream(elementUuid, List.of()).forEach(elementAttributes -> deleteElement(elementAttributes, userId)); | ||
| getAllDirectoryElementsStream(elementUuid, List.of(), userId).forEach(elementAttributes -> deleteElement(elementAttributes, userId)); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -502,15 +504,15 @@ public List<ElementAttributes> getElements(List<UUID> ids, boolean strictMode, L | |
| //if the user is not an admin we filter out elements he doesn't have the permission on | ||
| if (!roleService.isUserExploreAdmin()) { | ||
| elementEntities = elementEntities.stream().filter(directoryElementEntity -> | ||
| hasReadPermissions(userId, List.of(directoryElementEntity.getId())) | ||
| ).toList(); | ||
| hasReadPermissions(userId, List.of(directoryElementEntity.getId())) | ||
| ).toList(); | ||
| } | ||
|
|
||
| if (strictMode && elementEntities.size() != ids.stream().distinct().count()) { | ||
| throw new DirectoryException(NOT_FOUND); | ||
| } | ||
|
|
||
| Map<UUID, Long> subElementsCount = getSubDirectoriesCounts(elementEntities.stream().map(DirectoryElementEntity::getId).toList(), types); | ||
| Map<UUID, Long> subElementsCount = getSubDirectoriesCounts(elementEntities.stream().map(DirectoryElementEntity::getId).toList(), types, userId); | ||
|
|
||
| return elementEntities.stream() | ||
| .map(attribute -> toElementAttributes(attribute, subElementsCount.getOrDefault(attribute.getId(), 0L))) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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.
All children are in readable dirs, so we dont need to check (their parent dir) anymore.
Or maybe we have to filter/check in case the child is a sub-dir ?
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.
Ye a subdirectory can be read protected while its parent isn't so I don't think we can bypass this check