From d894e8a78ddde74f0f1924ff13bd99e46617b266 Mon Sep 17 00:00:00 2001 From: Marcel Klehr Date: Tue, 18 Jun 2024 11:13:20 +0200 Subject: [PATCH] fix(FileListener): Try to account for groupfolders Signed-off-by: Marcel Klehr --- lib/Hooks/FileListener.php | 41 ++++++++++++++++++++++++++++++++++++++ tests/ClassifierTest.php | 32 ----------------------------- 2 files changed, 41 insertions(+), 32 deletions(-) diff --git a/lib/Hooks/FileListener.php b/lib/Hooks/FileListener.php index 43d7d91e..fc1dd13c 100644 --- a/lib/Hooks/FileListener.php +++ b/lib/Hooks/FileListener.php @@ -34,10 +34,14 @@ use OCP\Files\IRootFolder; use OCP\Files\Node; use OCP\Files\NotFoundException; +use OCP\IGroupManager; +use OCP\IUser; use OCP\Share\Events\ShareAcceptedEvent; use OCP\Share\Events\ShareCreatedEvent; use OCP\Share\Events\ShareDeletedEvent; use OCP\Share\IManager; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; use Psr\Log\LoggerInterface; /** @@ -58,6 +62,7 @@ public function __construct( private IRootFolder $rootFolder, private IUserMountCache $userMountCache, private IManager $shareManager, + private IGroupManager $groupManager, ) { $this->movingFromIgnoredTerritory = null; $this->sourceUserIds = []; @@ -80,6 +85,42 @@ private function getUsersWithFileAccess(Node $node): array { return $mountInfo->getUser()->getUID(); }, $mountInfos); + $userIds += $this->getGroupFolderParticipants($node); + + return array_values(array_unique($userIds)); + } + + private function getGroupFolderParticipants(Node $node) { + $filePath = $node->getPath(); + + // Now we need to find the group folder ID associated with this path + // Assuming the group folders are stored in a specific path, e.g., "/__groupfolders//..." + preg_match('/\/__groupfolders\/(\d+)\//', $filePath, $matches); + + if (!isset($matches[1])) { + return []; + } + + $groupFolderId = (int) $matches[1]; + + try { + $groupFolderManager = \OCP\Server::get(OCA\GroupFolders\Folder\FolderManager::class); + } catch (NotFoundExceptionInterface|ContainerExceptionInterface $e) { + return []; + } + + $folder = $groupFolderManager->getFolder($groupFolderId); + + $userIds = []; + // Fetch groups + if (!empty($folder['groups'])) { + foreach ($folder['groups'] as $groupId => $permissions) { + $group = $this->groupManager->get($groupId); + if ($group) { + $userIds += array_map(fn (IUser $user) => $user->getUID(), $group->getUsers()); + } + } + } return array_values(array_unique($userIds)); } diff --git a/tests/ClassifierTest.php b/tests/ClassifierTest.php index 0c2476cc..d10600dd 100644 --- a/tests/ClassifierTest.php +++ b/tests/ClassifierTest.php @@ -22,7 +22,6 @@ use OCA\Recognize\Service\QueueService; use OCP\AppFramework\Services\IAppConfig; use OCP\BackgroundJob\IJobList; -use OCP\Files\Config\IUserMountCache; use OCP\Files\Folder; use OCP\Files\IRootFolder; use OCP\SystemTag\ISystemTagObjectMapper; @@ -392,37 +391,6 @@ public function testLandmarksPipeline() : void { ); } - public function testSharing() : void { - $testFile = $this->userFolder->newFile('/jumpingjack.gif', file_get_contents(__DIR__.'/res/jumpingjack.gif')); - - $userMountCache = \OCP\Server::get(IUserMountCache::class); - $userMountCache->clear(); - $mountInfos = $userMountCache->getMountsForFileId($testFile->getId()); - $users = array_map(static function (\OCP\Files\Config\ICachedMountInfo $mountInfo) { - return $mountInfo->getUser()->getUID(); - }, $mountInfos); - self::assertEquals([self::TEST_USER1], $users); - - $sharedFolder = $this->userFolder->newFolder('/shared/'); - $share = $this->shareManager->newShare(); - $share->setSharedBy(self::TEST_USER1); - $share->setSharedWith(self::TEST_USER2); - $share->setShareType(\OCP\Share\IShare::TYPE_USER); - $share->setNode($sharedFolder); - $share->setPermissions(\OCP\Constants::PERMISSION_ALL); - $this->shareManager->createShare($share); - $this->shareManager->acceptShare($share, self::TEST_USER2); - - $testFile->move($sharedFolder->getPath().'/'.$testFile->getName()); - - $userMountCache->clear(); - $mountInfos = $userMountCache->getMountsForFileId($testFile->getId()); - $users = array_map(static function (\OCP\Files\Config\ICachedMountInfo $mountInfo) { - return $mountInfo->getUser()->getUID(); - }, $mountInfos); - self::assertEquals([self::TEST_USER1, self::TEST_USER2], $users); - } - public function testFacesPipeline() : void { // Upload FaceID files $testFiles = [];