From e999d42d33fad29b75db33cda98c4ea20f218005 Mon Sep 17 00:00:00 2001 From: Jonathan Treffler Date: Mon, 25 Nov 2024 17:47:45 +0100 Subject: [PATCH] Added organization folder update permissions DAV prop, improved PropFindPlugin performance --- lib/Controller/ResourceController.php | 2 +- lib/Dav/PropFindPlugin.php | 67 +++++++++++++++++++++++---- 2 files changed, 60 insertions(+), 9 deletions(-) diff --git a/lib/Controller/ResourceController.php b/lib/Controller/ResourceController.php index 83ca594..ccbc754 100644 --- a/lib/Controller/ResourceController.php +++ b/lib/Controller/ResourceController.php @@ -41,7 +41,7 @@ class ResourceController extends BaseController { } if($this->shouldInclude(self::SUBRESOURCES_INCLUDE, $includes)) { - $result["subresources"] = $this->getSubResources($resource->getId()); + $result["subResources"] = $this->getSubResources($resource->getId()); } return $result; diff --git a/lib/Dav/PropFindPlugin.php b/lib/Dav/PropFindPlugin.php index 1f08500..4b72e4d 100644 --- a/lib/Dav/PropFindPlugin.php +++ b/lib/Dav/PropFindPlugin.php @@ -13,16 +13,21 @@ use OCA\DAV\Connector\Sabre\Node; use OCA\GroupFolders\Folder\FolderManager; use OCA\GroupFolders\Mount\GroupMountPoint; +use OCA\OrganizationFolders\Db\Resource; +use OCA\OrganizationFolders\Model\OrganizationFolder; +use OCA\OrganizationFolders\Service\OrganizationFolderService; use OCA\OrganizationFolders\Service\ResourceService; use OCA\OrganizationFolders\Security\AuthorizationService; class PropFindPlugin extends ServerPlugin { public const ORGANIZATION_FOLDER_ID_PROPERTYNAME = '{http://verdigado.com/ns}organization-folder-id'; public const ORGANIZATION_FOLDER_RESOURCE_ID_PROPERTYNAME = '{http://verdigado.com/ns}organization-folder-resource-id'; - public const ORGANIZATION_FOLDER_RESOURCE_MANAGER_PERMISSIONS_PROPERTYNAME = '{http://verdigado.com/ns}organization-folder-resource-user-has-manager-permissions'; + public const ORGANIZATION_FOLDER_UPDATE_PERMISSIONS_PROPERTYNAME = '{http://verdigado.com/ns}organization-folder-user-has-update-permissions'; + public const ORGANIZATION_FOLDER_RESOURCE_UPDATE_PERMISSIONS_PROPERTYNAME = '{http://verdigado.com/ns}organization-folder-resource-user-has-update-permissions'; public function __construct( private FolderManager $folderManager, + private OrganizationFolderService $organizationFolderService, private ResourceService $resourceService, private AuthorizationService $authorizationService, ) { @@ -44,26 +49,72 @@ class PropFindPlugin extends ServerPlugin { return; } + /** + * @var ?OrganizationFolder + */ + $organizationFolder = null; - $propFind->handle(self::ORGANIZATION_FOLDER_ID_PROPERTYNAME, function () use ($fileInfo): int { - return $this->folderManager->getFolderByPath($fileInfo->getPath()); - }); + /** + * @var ?Resource + */ + $resource = null; - $propFind->handle(self::ORGANIZATION_FOLDER_RESOURCE_ID_PROPERTYNAME, function () use ($node): ?int { + $propFind->handle(self::ORGANIZATION_FOLDER_ID_PROPERTYNAME, function () use ($fileInfo, $organizationFolder): ?int { try { - return $this->resourceService->findByFileId($node->getId())->getId(); + if(!isset($organizationFolder)) { + $organizationFolder = $this->getOrganizationFolderFromPath($fileInfo->getPath()); + } + + return $organizationFolder->getId(); } catch (\Exception $e) { return null; } }); - $propFind->handle(self::ORGANIZATION_FOLDER_RESOURCE_MANAGER_PERMISSIONS_PROPERTYNAME, function () use ($node) { + $propFind->handle(self::ORGANIZATION_FOLDER_UPDATE_PERMISSIONS_PROPERTYNAME, function () use ($fileInfo, $organizationFolder): ?string { try { - $resource = $this->resourceService->findByFileId($node->getId()); + if(!isset($organizationFolder)) { + $organizationFolder = $this->getOrganizationFolderFromPath($fileInfo->getPath()); + } + + return $this->authorizationService->isGranted(["UPDATE"], $organizationFolder) ? 'true' : 'false'; + } catch (\Exception $e) { + return null; + } + }); + + $propFind->handle(self::ORGANIZATION_FOLDER_RESOURCE_ID_PROPERTYNAME, function () use ($node, $resource): ?int { + try { + if(!isset($resource)) { + $resource = $this->resourceService->findByFileId($node->getId()); + } + + return $resource->getId(); + } catch (\Exception $e) { + return null; + } + }); + + $propFind->handle(self::ORGANIZATION_FOLDER_RESOURCE_UPDATE_PERMISSIONS_PROPERTYNAME, function () use ($node, $resource) { + try { + if(!isset($resource)) { + $resource = $this->resourceService->findByFileId($node->getId()); + } + return $this->authorizationService->isGranted(["UPDATE"], $resource) ? 'true' : 'false'; } catch (\Exception $e) { return null; } }); } + + private function getOrganizationFolderFromPath($path): ?OrganizationFolder { + $organizationFolderId = $this->folderManager->getFolderByPath($path); + + if(isset($organizationFolderId)) { + return $this->organizationFolderService->find($organizationFolderId); + } else { + return null; + } + } } \ No newline at end of file