diff --git a/apps/comments/lib/Activity/Listener.php b/apps/comments/lib/Activity/Listener.php index b07cb19afdab2..939f17dcd2e66 100644 --- a/apps/comments/lib/Activity/Listener.php +++ b/apps/comments/lib/Activity/Listener.php @@ -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; @@ -36,24 +35,11 @@ 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']; - } + $users = $cache->getReadablePathByUserForFileId((int)$event->getComment()->getObjectId()); + if (empty($users)) { + return; } $actor = $this->session->getUser(); diff --git a/apps/systemtags/lib/Activity/Listener.php b/apps/systemtags/lib/Activity/Listener.php index 7ab00787a763f..1fdad6f42c9ff 100644 --- a/apps/systemtags/lib/Activity/Listener.php +++ b/apps/systemtags/lib/Activity/Listener.php @@ -11,7 +11,6 @@ use OCP\App\IAppManager; use OCP\Files\Config\IMountProviderCollection; use OCP\Files\IRootFolder; -use OCP\Files\Node; use OCP\IConfig; use OCP\IGroup; use OCP\IGroupManager; @@ -153,22 +152,10 @@ public function mapperEvent(MapperEvent $event) { // Get all mount point owners $cache = $this->mountCollection->getMountCache(); - $mounts = $cache->getMountsForFileId($event->getObjectId()); - if (empty($mounts)) { - return; - } - $users = []; - foreach ($mounts as $mount) { - $owner = $mount->getUser()->getUID(); - $ownerFolder = $this->rootFolder->getUserFolder($owner); - $nodes = $ownerFolder->getById($event->getObjectId()); - if (!empty($nodes)) { - /** @var Node $node */ - $node = array_shift($nodes); - $al = $this->shareHelper->getPathsForAccessList($node); - $users += $al['users']; - } + $users = $cache->getReadablePathByUserForFileId((int)$event->getObjectId()); + if (empty($users)) { + return; } $actor = $this->session->getUser(); diff --git a/core/Command/Info/FileUtils.php b/core/Command/Info/FileUtils.php index df7dba175ba24..47f477c166aba 100644 --- a/core/Command/Info/FileUtils.php +++ b/core/Command/Info/FileUtils.php @@ -44,18 +44,7 @@ public function getFilesByUser(FileInfo $file): array { return []; } - $mounts = $this->userMountCache->getMountsForFileId($id); - $result = []; - foreach ($mounts as $mount) { - if (isset($result[$mount->getUser()->getUID()])) { - continue; - } - - $userFolder = $this->rootFolder->getUserFolder($mount->getUser()->getUID()); - $result[$mount->getUser()->getUID()] = $userFolder->getById($id); - } - - return $result; + return $this->userMountCache->getReadableNodesByUserForFileId($id); } /** diff --git a/lib/private/Files/Config/UserMountCache.php b/lib/private/Files/Config/UserMountCache.php index 94da770b63f06..2445f54c56ad1 100644 --- a/lib/private/Files/Config/UserMountCache.php +++ b/lib/private/Files/Config/UserMountCache.php @@ -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; /** @@ -380,6 +384,61 @@ public function getMountsForFileId($fileId, $user = null) { }, $filteredMounts); } + /** + * Get all nodes that give access to a file + * + * @return array 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); + $result = []; + foreach ($mounts as $mount) { + $uid = $mount->getUser()->getUID(); + if (!isset($result[$uid])) { + $result[$uid] = []; + } + + $userFolder = $rootFolder->getUserFolder($uid); + $result[$uid] = array_merge($result[$uid], $userFolder->getById($fileId)); + } + + return array_filter($result); + } + + /** + * Get all users having read access to a file, with a path they see it as. + * They may see the same file under other paths, + * to get this information use the exhaustive getReadableNodesByUserForFileId + * + * @return array Paths giving access to the given fileId, indexed by user ID + * @since 28.0.0 + */ + public function getReadablePathByUserForFileId(int $fileId): array { + $mounts = $this->getMountsForFileId($fileId); + $rootFolder = Server::get(IRootFolder::class); + $result = []; + foreach ($mounts as $mount) { + $uid = $mount->getUser()->getUID(); + if (isset($result[$uid])) { + continue; + } + + $userFolder = $rootFolder->getUserFolder($uid); + $nodes = $userFolder->getById($fileId); + $node = reset($nodes); + if ($node) { + $path = $userFolder->getRelativePath($node->getPath()); + if ($path !== null) { + $result[$uid] = $path; + } + } + } + + return $result; + } + /** * Remove all cached mounts for a user * diff --git a/lib/public/Files/Config/IUserMountCache.php b/lib/public/Files/Config/IUserMountCache.php index a5b68ded66d15..5c4fcf83f4ae9 100644 --- a/lib/public/Files/Config/IUserMountCache.php +++ b/lib/public/Files/Config/IUserMountCache.php @@ -8,6 +8,7 @@ namespace OCP\Files\Config; use OCP\Files\Mount\IMountPoint; +use OCP\Files\Node; use OCP\Files\NotFoundException; use OCP\IUser; @@ -65,6 +66,23 @@ public function getMountsForRootId($rootFileId); */ public function getMountsForFileId($fileId, $user = null); + /** + * Get all cached nodes that give access to a file + * + * @return array Nodes giving access to the given fileId, indexed by user ID + * @since 28.0.0 + */ + public function getReadableNodesByUserForFileId(int $fileId): array; + + /** + * Get all users having read access to a file, with a path they see it as. + * They may see the same file under other paths, to get this information use exhaustive getReadableNodesByUserForFileId + * + * @return array Paths giving access to the given fileId, indexed by user ID + * @since 28.0.0 + */ + public function getReadablePathByUserForFileId(int $fileId): array; + /** * Remove all cached mounts for a user *