From 2e9222a29ae3430725f8f20346ec1308e0de3263 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 11 Mar 2025 16:31:39 +0100 Subject: [PATCH 1/4] feat: add getParentId to ICacheEntry Signed-off-by: Robin Appelman --- apps/files_sharing/lib/Command/ListShares.php | 125 ++++++++++++++++++ lib/private/Files/Cache/CacheEntry.php | 4 + lib/public/Files/Cache/ICacheEntry.php | 8 ++ 3 files changed, 137 insertions(+) create mode 100644 apps/files_sharing/lib/Command/ListShares.php diff --git a/apps/files_sharing/lib/Command/ListShares.php b/apps/files_sharing/lib/Command/ListShares.php new file mode 100644 index 0000000000000..40a4e8ed539b2 --- /dev/null +++ b/apps/files_sharing/lib/Command/ListShares.php @@ -0,0 +1,125 @@ + + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace OCA\Files_Sharing\Command; + +use OC\Core\Command\Base; +use OCP\Files\Folder; +use OCP\Files\Node; +use OCP\Files\IRootFolder; +use OCP\Files\NotFoundException; +use OCP\IDBConnection; +use OCP\Share\IManager; +use OCP\Share\IShare; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +class ListShares extends Base { + /** @var array{string, Node} */ + private $fileCache = []; + + public function __construct( + private readonly IDBConnection $connection, + private readonly IManager $shareManager, + private readonly IRootFolder $rootFolder, + ) { + parent::__construct(); + } + + protected function configure() { + parent::configure(); + $this + ->setName('sharing:list') + ->setDescription('List available shares') + ->addOption('owner', null, InputOption::VALUE_REQUIRED, 'only show shares owned by a specific user') + ->addOption('recipient', null, InputOption::VALUE_REQUIRED, 'only show shares with a specific recipient') + ->addOption('by', null, InputOption::VALUE_REQUIRED, 'only show shares with by as specific user') + ->addOption('file', null, InputOption::VALUE_REQUIRED, 'only show shares of a specific file') + ->addOption('parent', null, InputOption::VALUE_REQUIRED, 'only show shares of files inside a specific folder') + ->addOption('recursive', null, InputOption::VALUE_NONE, 'also show shares nested deep inside the specified parent folder'); + } + + public function execute(InputInterface $input, OutputInterface $output): int { + $shares = iterator_to_array($this->shareManager->getAllShares()); + $shares = array_filter($shares, function (IShare $share) use ($input) { + return $this->shouldShowShare($input, $share); + }); + $data = array_map(function (IShare $share) { + return [ + 'id' => $share->getId(), + 'file' => $share->getNodeId(), + 'owner' => $share->getShareOwner(), + 'recipient' => $share->getSharedWith(), + 'by' => $share->getSharedBy(), + ]; + }, $shares); + if ($input->getOption('output') === self::OUTPUT_FORMAT_PLAIN) { + $this->writeTableInOutputFormat($input, $output, $data); + } else { + $this->writeArrayInOutputFormat($input, $output, $data); + } + } + + private function getFileId(string $file): int { + if (is_numeric($file)) { + return $file; + } + return $this->getFile($file)->getId(); + } + + private function getFile(string $file): Node { + if (isset($this->fileCache[$file])) { + return $this->fileCache[$file]; + } + + if (is_numeric($file)) { + $node = $this->rootFolder->getFirstNodeById($file); + if (!$node) { + throw new NotFoundException("File with id $file not found"); + } + } else { + $node = $this->rootFolder->get($file); + } + $this->fileCache[$file] = $node; + return $node; + } + + private function shouldShowShare(InputInterface $input, IShare $share): bool { + if ($input->getOption('owner') && $share->getShareOwner() !== $input->getOption('owner')) { + return false; + } + if ($input->getOption('recipient') && $share->getSharedWith() !== $input->getOption('recipient')) { + return false; + } + if ($input->getOption('by') && $share->getSharedBy() !== $input->getOption('by')) { + return false; + } + if ($input->getOption('file') && $share->getNodeId() !== $this->getFileId($input->getOption('file'))) { + return false; + } + if ($input->getOption('parent')) { + $parent = $this->getFile($input->getOption('parent')); + if (!$parent instanceof Folder) { + throw new \Exception("Parent {$parent->getPath()} is not a folder"); + } + $recursive = $input->getOption('recursive'); + if (!$recursive) { + if ($share->getNodeCacheEntry()->getParent() !== $parent->getId()) { + return false; + } + } else { + $shareNode = $share->getNode(); + if ($parent->getRelativePath($shareNode->getPath()) === null) { + return false; + } + } + } + return true; + } +} diff --git a/lib/private/Files/Cache/CacheEntry.php b/lib/private/Files/Cache/CacheEntry.php index e9417c8012a3f..ab5bae316f455 100644 --- a/lib/private/Files/Cache/CacheEntry.php +++ b/lib/private/Files/Cache/CacheEntry.php @@ -110,6 +110,10 @@ public function getUploadTime(): ?int { return $this->data['upload_time'] ?? null; } + public function getParentId(): int { + return $this->data['parent']; + } + public function getData() { return $this->data; } diff --git a/lib/public/Files/Cache/ICacheEntry.php b/lib/public/Files/Cache/ICacheEntry.php index 11d91e741056c..28e673071fd4c 100644 --- a/lib/public/Files/Cache/ICacheEntry.php +++ b/lib/public/Files/Cache/ICacheEntry.php @@ -161,4 +161,12 @@ public function getUploadTime(): ?int; * @since 25.0.0 */ public function getUnencryptedSize(): int; + + /** + * Get the file id of the parent folder + * + * @return int + * @since 32.0.0 + */ + public function getParentId(): int; } From c573867fff64308ca2d3247fe76f7d800a0207e3 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 11 Mar 2025 16:32:19 +0100 Subject: [PATCH 2/4] feat: add command to list shares Signed-off-by: Robin Appelman --- apps/files_sharing/appinfo/info.xml | 1 + .../composer/composer/autoload_classmap.php | 1 + .../composer/composer/autoload_static.php | 1 + apps/files_sharing/lib/Command/ListShares.php | 89 +++++++++++++++---- 4 files changed, 75 insertions(+), 17 deletions(-) diff --git a/apps/files_sharing/appinfo/info.xml b/apps/files_sharing/appinfo/info.xml index c92559635ea08..03078b5404064 100644 --- a/apps/files_sharing/appinfo/info.xml +++ b/apps/files_sharing/appinfo/info.xml @@ -50,6 +50,7 @@ Turning the feature off removes shared files and folders on the server for all s OCA\Files_Sharing\Command\ExiprationNotification OCA\Files_Sharing\Command\DeleteOrphanShares OCA\Files_Sharing\Command\FixShareOwners + OCA\Files_Sharing\Command\ListShares diff --git a/apps/files_sharing/composer/composer/autoload_classmap.php b/apps/files_sharing/composer/composer/autoload_classmap.php index 400df9c977187..8682fd5f2385f 100644 --- a/apps/files_sharing/composer/composer/autoload_classmap.php +++ b/apps/files_sharing/composer/composer/autoload_classmap.php @@ -28,6 +28,7 @@ 'OCA\\Files_Sharing\\Command\\DeleteOrphanShares' => $baseDir . '/../lib/Command/DeleteOrphanShares.php', 'OCA\\Files_Sharing\\Command\\ExiprationNotification' => $baseDir . '/../lib/Command/ExiprationNotification.php', 'OCA\\Files_Sharing\\Command\\FixShareOwners' => $baseDir . '/../lib/Command/FixShareOwners.php', + 'OCA\\Files_Sharing\\Command\\ListShares' => $baseDir . '/../lib/Command/ListShares.php', 'OCA\\Files_Sharing\\Controller\\AcceptController' => $baseDir . '/../lib/Controller/AcceptController.php', 'OCA\\Files_Sharing\\Controller\\DeletedShareAPIController' => $baseDir . '/../lib/Controller/DeletedShareAPIController.php', 'OCA\\Files_Sharing\\Controller\\ExternalSharesController' => $baseDir . '/../lib/Controller/ExternalSharesController.php', diff --git a/apps/files_sharing/composer/composer/autoload_static.php b/apps/files_sharing/composer/composer/autoload_static.php index 02bb6d08bb580..3bf5bcdffda04 100644 --- a/apps/files_sharing/composer/composer/autoload_static.php +++ b/apps/files_sharing/composer/composer/autoload_static.php @@ -43,6 +43,7 @@ class ComposerStaticInitFiles_Sharing 'OCA\\Files_Sharing\\Command\\DeleteOrphanShares' => __DIR__ . '/..' . '/../lib/Command/DeleteOrphanShares.php', 'OCA\\Files_Sharing\\Command\\ExiprationNotification' => __DIR__ . '/..' . '/../lib/Command/ExiprationNotification.php', 'OCA\\Files_Sharing\\Command\\FixShareOwners' => __DIR__ . '/..' . '/../lib/Command/FixShareOwners.php', + 'OCA\\Files_Sharing\\Command\\ListShares' => __DIR__ . '/..' . '/../lib/Command/ListShares.php', 'OCA\\Files_Sharing\\Controller\\AcceptController' => __DIR__ . '/..' . '/../lib/Controller/AcceptController.php', 'OCA\\Files_Sharing\\Controller\\DeletedShareAPIController' => __DIR__ . '/..' . '/../lib/Controller/DeletedShareAPIController.php', 'OCA\\Files_Sharing\\Controller\\ExternalSharesController' => __DIR__ . '/..' . '/../lib/Controller/ExternalSharesController.php', diff --git a/apps/files_sharing/lib/Command/ListShares.php b/apps/files_sharing/lib/Command/ListShares.php index 40a4e8ed539b2..98bfc721ec818 100644 --- a/apps/files_sharing/lib/Command/ListShares.php +++ b/apps/files_sharing/lib/Command/ListShares.php @@ -10,10 +10,9 @@ use OC\Core\Command\Base; use OCP\Files\Folder; -use OCP\Files\Node; use OCP\Files\IRootFolder; +use OCP\Files\Node; use OCP\Files\NotFoundException; -use OCP\IDBConnection; use OCP\Share\IManager; use OCP\Share\IShare; use Symfony\Component\Console\Input\InputInterface; @@ -21,11 +20,27 @@ use Symfony\Component\Console\Output\OutputInterface; class ListShares extends Base { - /** @var array{string, Node} */ - private $fileCache = []; + /** @var array */ + private array $fileCache = []; + + private const SHARE_TYPE_NAMES = [ + IShare::TYPE_USER => 'user', + IShare::TYPE_GROUP => 'group', + IShare::TYPE_LINK => 'link', + IShare::TYPE_EMAIL => 'email', + IShare::TYPE_REMOTE => 'remote', + IShare::TYPE_REMOTE_GROUP => 'group', + IShare::TYPE_ROOM => 'room', + IShare::TYPE_DECK => 'deck', + ]; + + private const SHARE_STATUS_NAMES = [ + IShare::STATUS_PENDING => 'pending', + IShare::STATUS_ACCEPTED => 'accepted', + IShare::STATUS_REJECTED => 'rejected', + ]; public function __construct( - private readonly IDBConnection $connection, private readonly IManager $shareManager, private readonly IRootFolder $rootFolder, ) { @@ -35,40 +50,52 @@ public function __construct( protected function configure() { parent::configure(); $this - ->setName('sharing:list') + ->setName('share:list') ->setDescription('List available shares') ->addOption('owner', null, InputOption::VALUE_REQUIRED, 'only show shares owned by a specific user') ->addOption('recipient', null, InputOption::VALUE_REQUIRED, 'only show shares with a specific recipient') ->addOption('by', null, InputOption::VALUE_REQUIRED, 'only show shares with by as specific user') ->addOption('file', null, InputOption::VALUE_REQUIRED, 'only show shares of a specific file') ->addOption('parent', null, InputOption::VALUE_REQUIRED, 'only show shares of files inside a specific folder') - ->addOption('recursive', null, InputOption::VALUE_NONE, 'also show shares nested deep inside the specified parent folder'); + ->addOption('recursive', null, InputOption::VALUE_NONE, 'also show shares nested deep inside the specified parent folder') + ->addOption('type', null, InputOption::VALUE_REQUIRED, 'only show shares of a specific type') + ->addOption('status', null, InputOption::VALUE_REQUIRED, 'only show shares with a specific status'); } public function execute(InputInterface $input, OutputInterface $output): int { - $shares = iterator_to_array($this->shareManager->getAllShares()); - $shares = array_filter($shares, function (IShare $share) use ($input) { + if ($input->getOption('recursive') && !$input->getOption('parent')) { + $output->writeln("recursive option can't be used without parent option"); + return 1; + } + + // todo: do some pre-filtering instead of first querying all shares + /** @var \Iterator $allShares */ + $allShares = $this->shareManager->getAllShares(); + $shares = new \CallbackFilterIterator($allShares, function (IShare $share) use ($input) { return $this->shouldShowShare($input, $share); }); + $shares = iterator_to_array($shares); $data = array_map(function (IShare $share) { return [ 'id' => $share->getId(), 'file' => $share->getNodeId(), + 'target-path' => $share->getTarget(), + 'source-path' => $share->getNode()->getPath(), 'owner' => $share->getShareOwner(), 'recipient' => $share->getSharedWith(), 'by' => $share->getSharedBy(), + 'type' => self::SHARE_TYPE_NAMES[$share->getShareType()] ?? 'unknown', + 'status' => self::SHARE_STATUS_NAMES[$share->getStatus()] ?? 'unknown', ]; }, $shares); - if ($input->getOption('output') === self::OUTPUT_FORMAT_PLAIN) { - $this->writeTableInOutputFormat($input, $output, $data); - } else { - $this->writeArrayInOutputFormat($input, $output, $data); - } + + $this->writeTableInOutputFormat($input, $output, $data); + return 0; } private function getFileId(string $file): int { if (is_numeric($file)) { - return $file; + return (int)$file; } return $this->getFile($file)->getId(); } @@ -79,7 +106,7 @@ private function getFile(string $file): Node { } if (is_numeric($file)) { - $node = $this->rootFolder->getFirstNodeById($file); + $node = $this->rootFolder->getFirstNodeById((int)$file); if (!$node) { throw new NotFoundException("File with id $file not found"); } @@ -90,6 +117,24 @@ private function getFile(string $file): Node { return $node; } + private function getShareType(string $type): int { + foreach (self::SHARE_TYPE_NAMES as $shareType => $shareTypeName) { + if ($shareTypeName === $type) { + return $shareType; + } + } + throw new \Exception("Unknown share type $type"); + } + + private function getShareStatus(string $status): int { + foreach (self::SHARE_STATUS_NAMES as $shareStatus => $shareStatusName) { + if ($shareStatusName === $status) { + return $shareStatus; + } + } + throw new \Exception("Unknown share status $status"); + } + private function shouldShowShare(InputInterface $input, IShare $share): bool { if ($input->getOption('owner') && $share->getShareOwner() !== $input->getOption('owner')) { return false; @@ -110,7 +155,11 @@ private function shouldShowShare(InputInterface $input, IShare $share): bool { } $recursive = $input->getOption('recursive'); if (!$recursive) { - if ($share->getNodeCacheEntry()->getParent() !== $parent->getId()) { + $shareCacheEntry = $share->getNodeCacheEntry(); + if (!$shareCacheEntry) { + $shareCacheEntry = $share->getNode(); + } + if ($shareCacheEntry->getParentId() !== $parent->getId()) { return false; } } else { @@ -120,6 +169,12 @@ private function shouldShowShare(InputInterface $input, IShare $share): bool { } } } + if ($input->getOption('type') && $share->getShareType() !== $this->getShareType($input->getOption('type'))) { + return false; + } + if ($input->getOption('status') && $share->getStatus() !== $this->getShareStatus($input->getOption('status'))) { + return false; + } return true; } } From 9626c08786cd0f41200793f4a1fe05b06f73e64c Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Wed, 19 Mar 2025 17:01:26 +0100 Subject: [PATCH 3/4] fix: set share status for federated shares Signed-off-by: Robin Appelman --- apps/federatedfilesharing/lib/FederatedShareProvider.php | 1 + 1 file changed, 1 insertion(+) diff --git a/apps/federatedfilesharing/lib/FederatedShareProvider.php b/apps/federatedfilesharing/lib/FederatedShareProvider.php index 379cd7931164e..d993b35845cc1 100644 --- a/apps/federatedfilesharing/lib/FederatedShareProvider.php +++ b/apps/federatedfilesharing/lib/FederatedShareProvider.php @@ -812,6 +812,7 @@ private function createShareObject($data) { ->setPermissions((int)$data['permissions']) ->setTarget($data['file_target']) ->setMailSend((bool)$data['mail_send']) + ->setStatus((int)$data['accepted']) ->setToken($data['token']); $shareTime = new \DateTime(); From b9723eaa2253663a9f8965dd600a5227e37b83d3 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Wed, 19 Mar 2025 17:02:40 +0100 Subject: [PATCH 4/4] feat: remove share status from share:list as it doesn't seem to contain usefull info at the moment Signed-off-by: Robin Appelman --- apps/files_sharing/lib/Command/ListShares.php | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/apps/files_sharing/lib/Command/ListShares.php b/apps/files_sharing/lib/Command/ListShares.php index 98bfc721ec818..2d5cdbf78129b 100644 --- a/apps/files_sharing/lib/Command/ListShares.php +++ b/apps/files_sharing/lib/Command/ListShares.php @@ -34,12 +34,6 @@ class ListShares extends Base { IShare::TYPE_DECK => 'deck', ]; - private const SHARE_STATUS_NAMES = [ - IShare::STATUS_PENDING => 'pending', - IShare::STATUS_ACCEPTED => 'accepted', - IShare::STATUS_REJECTED => 'rejected', - ]; - public function __construct( private readonly IManager $shareManager, private readonly IRootFolder $rootFolder, @@ -85,7 +79,6 @@ public function execute(InputInterface $input, OutputInterface $output): int { 'recipient' => $share->getSharedWith(), 'by' => $share->getSharedBy(), 'type' => self::SHARE_TYPE_NAMES[$share->getShareType()] ?? 'unknown', - 'status' => self::SHARE_STATUS_NAMES[$share->getStatus()] ?? 'unknown', ]; }, $shares); @@ -126,15 +119,6 @@ private function getShareType(string $type): int { throw new \Exception("Unknown share type $type"); } - private function getShareStatus(string $status): int { - foreach (self::SHARE_STATUS_NAMES as $shareStatus => $shareStatusName) { - if ($shareStatusName === $status) { - return $shareStatus; - } - } - throw new \Exception("Unknown share status $status"); - } - private function shouldShowShare(InputInterface $input, IShare $share): bool { if ($input->getOption('owner') && $share->getShareOwner() !== $input->getOption('owner')) { return false; @@ -172,9 +156,6 @@ private function shouldShowShare(InputInterface $input, IShare $share): bool { if ($input->getOption('type') && $share->getShareType() !== $this->getShareType($input->getOption('type'))) { return false; } - if ($input->getOption('status') && $share->getStatus() !== $this->getShareStatus($input->getOption('status'))) { - return false; - } return true; } }