Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: Add a method to get all users with access to a file in OCP
Signed-off-by: Côme Chilliet <[email protected]>
  • Loading branch information
come-nc committed Sep 14, 2024
commit cb2495a1c27aa014139f675ad3ec49e9aced164f
19 changes: 3 additions & 16 deletions apps/comments/lib/Activity/Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use OCP\Comments\CommentsEvent;
use OCP\Files\Config\IMountProviderCollection;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\IUser;
use OCP\IUserSession;
use OCP\Share\IShareHelper;
Expand All @@ -36,24 +35,12 @@ public function commentEvent(CommentsEvent $event): void {
return;
}

// Get all mount point owners
$cache = $this->mountCollection->getMountCache();
$mounts = $cache->getMountsForFileId((int)$event->getComment()->getObjectId());
if (empty($mounts)) {
return;
}

$users = [];
foreach ($mounts as $mount) {
$owner = $mount->getUser()->getUID();
$ownerFolder = $this->rootFolder->getUserFolder($owner);
$nodes = $ownerFolder->getById((int)$event->getComment()->getObjectId());
if (!empty($nodes)) {
/** @var Node $node */
$node = array_shift($nodes);
$al = $this->shareHelper->getPathsForAccessList($node);
$users += $al['users'];
}
$filesPerUser = $cache->getReadableNodesByUserForFileId((int)$event->getComment()->getObjectId());
foreach ($filesPerUser as $user => $files) {
$users[$user] = reset($files)?->getPath();
}

$actor = $this->session->getUser();
Expand Down
26 changes: 26 additions & 0 deletions lib/private/Files/Config/UserMountCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@
use OCP\Files\Config\ICachedMountFileInfo;
use OCP\Files\Config\ICachedMountInfo;
use OCP\Files\Config\IUserMountCache;
use OCP\Files\IRootFolder;
use OCP\Files\Mount\IMountPoint;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\IDBConnection;
use OCP\IUser;
use OCP\IUserManager;
use OCP\Server;
use Psr\Log\LoggerInterface;

/**
Expand Down Expand Up @@ -380,6 +384,28 @@ public function getMountsForFileId($fileId, $user = null) {
}, $filteredMounts);
}

/**
* Get all nodes that give access to a file
*
* @return array<string, Node[]> Nodes giving access to the given fileId, indexed by user ID
* @since 28.0.0
*/
public function getReadableNodesByUserForFileId(int $fileId): array {
$mounts = $this->getMountsForFileId($fileId);
$rootFolder = Server::get(IRootFolder::class);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Can this be added to the DI or is there a problem with that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might make more sense to move this method to the RootFolder instead

$result = [];
foreach ($mounts as $mount) {
if (isset($result[$mount->getUser()->getUID()])) {
continue;
}

$userFolder = $rootFolder->getUserFolder($mount->getUser()->getUID());
$result[$mount->getUser()->getUID()] = $userFolder->getById($fileId);
}

return $result;
}

/**
* Remove all cached mounts for a user
*
Expand Down
9 changes: 9 additions & 0 deletions lib/public/Files/Config/IUserMountCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace OCP\Files\Config;

use OCP\Files\Mount\IMountPoint;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\IUser;

Expand Down Expand Up @@ -65,6 +66,14 @@ public function getMountsForRootId($rootFileId);
*/
public function getMountsForFileId($fileId, $user = null);

/**
* Get all cached nodes that give access to a file
*
* @return array<string, Node[]> Nodes giving access to the given fileId, indexed by user ID
* @since 28.0.0
*/
public function getReadableNodesByUserForFileId(int $fileId): array;

/**
* Remove all cached mounts for a user
*
Expand Down