Skip to content

Commit

Permalink
Add a method to get all users with access to a file in OCP
Browse files Browse the repository at this point in the history
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
  • Loading branch information
come-nc committed Jun 22, 2023
1 parent 56c16b3 commit 5bc9418
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
19 changes: 3 additions & 16 deletions apps/comments/lib/Activity/Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,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 Down Expand Up @@ -67,24 +66,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->getFilesByUserId((int)$event->getComment()->getObjectId());
foreach ($filesPerUser as $user => $files) {
$users[$user] = reset($files)?->getPath();
}

$actor = $this->session->getUser();
Expand Down
23 changes: 23 additions & 0 deletions lib/private/Files/Config/UserMountCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
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\NotFoundException;
use OCP\IDBConnection;
Expand Down Expand Up @@ -383,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
* @since 28.0.0
*/
public function getNodesByUserForFileId(int $fileId): array {
$mounts = $this->getMountsForFileId($id);
$rootFolder = Server::get(IRootFolder::class);
$result = [];
foreach ($mounts as $mount) {
if (isset($result[$mount->getUser()->getUID()])) {
continue;
}

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

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 @@ -25,6 +25,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 @@ -82,6 +83,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[]>
* @since 28.0.0
*/
public function getNodesByUserForFileId(int $fileId): array;

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

0 comments on commit 5bc9418

Please sign in to comment.