0
0
Fork 0
mirror of https://github.com/verdigado/organization_folders.git synced 2024-11-22 04:38:09 +01:00

Added dav plugin to be able to query organizationId and resourceId of filesystem node from frontend

This commit is contained in:
Jonathan Treffler 2024-11-05 17:40:32 +01:00
parent dc10742476
commit f7b3b2f255
5 changed files with 116 additions and 2 deletions

View file

@ -3,11 +3,25 @@
namespace OCA\OrganizationFolders\AppInfo; namespace OCA\OrganizationFolders\AppInfo;
use OCP\AppFramework\App; 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 const APP_ID = 'organization_folders';
public function __construct() { public function __construct() {
parent::__construct(self::APP_ID); parent::__construct(self::APP_ID);
} }
public function register(IRegistrationContext $context): void {
$context->registerEventListener(SabrePluginAddEvent::class, SabrePluginAddListener::class);
}
public function boot(IBootContext $context): void {
}
} }

View file

@ -0,0 +1,56 @@
<?php
declare(strict_types=1);
namespace OCA\OrganizationFolders\Dav;
use Sabre\DAV\Server;
use Sabre\DAV\ServerPlugin;
use Sabre\DAV\INode;
use Sabre\DAV\PropFind;
use OCA\DAV\Connector\Sabre\Node;
use OCA\GroupFolders\Folder\FolderManager;
use OCA\GroupFolders\Mount\GroupMountPoint;
use OCA\OrganizationFolders\Service\ResourceService;
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 function __construct(
private FolderManager $folderManager,
private ResourceService $resourceService,
) {
}
public function initialize(Server $server): void {
$server->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;
}
});
}
}

View file

@ -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') $qb->select('resource.*', 'folder.members_acl_permission', 'folder.managers_acl_permission', 'folder.inherited_acl_permission', 'folder.file_id')
->from(self::RESOURCES_TABLE, "resource") ->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'),); $qb->leftJoin('resource', self::FOLDER_RESOURCES_TABLE, 'folder', $qb->expr()->eq('resource.id', 'folder.resource_id'),);
return $this->findEntity($qb); 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 $organizationFolderId
* @param int $parentResourceId * @param int $parentResourceId

View file

@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
namespace OCA\OrganizationFolders\Listener;
use OCA\DAV\Events\SabrePluginAddEvent;
use OCA\OrganizationFolders\Dav\PropFindPlugin;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use Psr\Container\ContainerInterface;
class SabrePluginAddListener implements IEventListener {
public function __construct(private ContainerInterface $container) {}
public function handle(Event $event): void {
if ($event instanceof SabrePluginAddEvent) {
$propFindPlugin = $this->container->get(PropFindPlugin::class);
$event->getServer()->addPlugin($propFindPlugin);
}
}
}

View file

@ -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 */ /* Use named arguments to call this function */
public function create( public function create(
string $type, string $type,