Skip to content

Commit

Permalink
fix(FileListener): Try to account for groupfolders
Browse files Browse the repository at this point in the history
Signed-off-by: Marcel Klehr <[email protected]>
  • Loading branch information
marcelklehr committed Jun 18, 2024
1 parent f7e773d commit d894e8a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 32 deletions.
41 changes: 41 additions & 0 deletions lib/Hooks/FileListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -58,6 +62,7 @@ public function __construct(
private IRootFolder $rootFolder,
private IUserMountCache $userMountCache,
private IManager $shareManager,
private IGroupManager $groupManager,
) {
$this->movingFromIgnoredTerritory = null;
$this->sourceUserIds = [];
Expand All @@ -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/<groupfolder_id>/..."
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));
}

Expand Down
32 changes: 0 additions & 32 deletions tests/ClassifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = [];
Expand Down

0 comments on commit d894e8a

Please sign in to comment.