From f7b3b2f255b9a983b3c7d46325bc65fccc98d9de Mon Sep 17 00:00:00 2001 From: Jonathan Treffler Date: Tue, 5 Nov 2024 17:40:32 +0100 Subject: [PATCH] Added dav plugin to be able to query organizationId and resourceId of filesystem node from frontend --- lib/AppInfo/Application.php | 16 ++++++- lib/Dav/PropFindPlugin.php | 56 +++++++++++++++++++++++++ lib/Db/ResourceMapper.php | 16 ++++++- lib/Listener/SabrePluginAddListener.php | 25 +++++++++++ lib/Service/ResourceService.php | 5 +++ 5 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 lib/Dav/PropFindPlugin.php create mode 100644 lib/Listener/SabrePluginAddListener.php diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index a78dbbe..1235b87 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -3,11 +3,25 @@ namespace OCA\OrganizationFolders\AppInfo; use OCP\AppFramework\App; +use OCP\AppFramework\Bootstrap\IBootstrap; +use OCP\AppFramework\Bootstrap\IRegistrationContext; +use OCP\AppFramework\Bootstrap\IBootContext; -class Application extends App { +use OCA\DAV\Events\SabrePluginAddEvent; + +use OCA\OrganizationFolders\Listener\SabrePluginAddListener; + +class Application extends App implements IBootstrap { public const APP_ID = 'organization_folders'; public function __construct() { parent::__construct(self::APP_ID); } + + public function register(IRegistrationContext $context): void { + $context->registerEventListener(SabrePluginAddEvent::class, SabrePluginAddListener::class); + } + + public function boot(IBootContext $context): void { + } } \ No newline at end of file diff --git a/lib/Dav/PropFindPlugin.php b/lib/Dav/PropFindPlugin.php new file mode 100644 index 0000000..390b01d --- /dev/null +++ b/lib/Dav/PropFindPlugin.php @@ -0,0 +1,56 @@ +on('propFind', $this->propFind(...)); + } + + public function propFind(PropFind $propFind, INode $node): void { + if (!$node instanceof Node) { + return; + } + + $fileInfo = $node->getFileInfo(); + $mount = $fileInfo->getMountPoint(); + + if (!$mount instanceof GroupMountPoint) { + return; + } + + $propFind->handle(self::ORGANIZATION_FOLDER_ID_PROPERTYNAME, function () use ($fileInfo): int { + return $this->folderManager->getFolderByPath($fileInfo->getPath()); + }); + + $propFind->handle(self::ORGANIZATION_FOLDER_RESOURCE_ID_PROPERTYNAME, function () use ($node): ?int { + try { + return $this->resourceService->findByFileId($node->getId())->getId(); + } catch (\Exception $e) { + return null; + } + }); + } +} \ No newline at end of file diff --git a/lib/Db/ResourceMapper.php b/lib/Db/ResourceMapper.php index 3dc8ded..1ca5f36 100644 --- a/lib/Db/ResourceMapper.php +++ b/lib/Db/ResourceMapper.php @@ -44,13 +44,27 @@ class ResourceMapper extends QBMapper { $qb->select('resource.*', 'folder.members_acl_permission', 'folder.managers_acl_permission', 'folder.inherited_acl_permission', 'folder.file_id') ->from(self::RESOURCES_TABLE, "resource") - ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))); + ->where($qb->expr()->eq('resource.id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))); $qb->leftJoin('resource', self::FOLDER_RESOURCES_TABLE, 'folder', $qb->expr()->eq('resource.id', 'folder.resource_id'),); return $this->findEntity($qb); } + public function findByFileId(int $fileId): FolderResource { + /* @var $qb IQueryBuilder */ + $qb = $this->db->getQueryBuilder(); + + $qb->select('resource.*', 'folder.members_acl_permission', 'folder.managers_acl_permission', 'folder.inherited_acl_permission', 'folder.file_id') + ->from(self::RESOURCES_TABLE, "resource"); + + $qb->innerJoin('resource', self::FOLDER_RESOURCES_TABLE, 'folder', $qb->expr()->eq('resource.id', 'folder.resource_id'),); + + $qb->where($qb->expr()->eq('folder.file_id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))); + + return $this->findEntity($qb); + } + /** * @param int $organizationFolderId * @param int $parentResourceId diff --git a/lib/Listener/SabrePluginAddListener.php b/lib/Listener/SabrePluginAddListener.php new file mode 100644 index 0000000..df87284 --- /dev/null +++ b/lib/Listener/SabrePluginAddListener.php @@ -0,0 +1,25 @@ +container->get(PropFindPlugin::class); + + $event->getServer()->addPlugin($propFindPlugin); + } + } +} \ No newline at end of file diff --git a/lib/Service/ResourceService.php b/lib/Service/ResourceService.php index d3779b3..a9ad341 100644 --- a/lib/Service/ResourceService.php +++ b/lib/Service/ResourceService.php @@ -55,6 +55,11 @@ class ResourceService { } } + public function findByFileId(int $fileId): FolderResource { + // TODO: improve error handling + return $this->mapper->findByFileId($fileId); + } + /* Use named arguments to call this function */ public function create( string $type,