|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * SPDX-FileCopyrightText: 2025 Robin Appelman <[email protected]> |
| 6 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + */ |
| 8 | + |
| 9 | +namespace OCA\Files_Sharing\Command; |
| 10 | + |
| 11 | +use OC\Core\Command\Base; |
| 12 | +use OCP\Files\Folder; |
| 13 | +use OCP\Files\IRootFolder; |
| 14 | +use OCP\Files\Node; |
| 15 | +use OCP\Files\NotFoundException; |
| 16 | +use OCP\Share\IManager; |
| 17 | +use OCP\Share\IShare; |
| 18 | +use Symfony\Component\Console\Input\InputInterface; |
| 19 | +use Symfony\Component\Console\Input\InputOption; |
| 20 | +use Symfony\Component\Console\Output\OutputInterface; |
| 21 | + |
| 22 | +class ListShares extends Base { |
| 23 | + /** @var array<string, Node> */ |
| 24 | + private array $fileCache = []; |
| 25 | + |
| 26 | + private const SHARE_TYPE_NAMES = [ |
| 27 | + IShare::TYPE_USER => 'user', |
| 28 | + IShare::TYPE_GROUP => 'group', |
| 29 | + IShare::TYPE_LINK => 'link', |
| 30 | + IShare::TYPE_EMAIL => 'email', |
| 31 | + IShare::TYPE_REMOTE => 'remote', |
| 32 | + IShare::TYPE_REMOTE_GROUP => 'group', |
| 33 | + IShare::TYPE_ROOM => 'room', |
| 34 | + IShare::TYPE_DECK => 'deck', |
| 35 | + ]; |
| 36 | + |
| 37 | + public function __construct( |
| 38 | + private readonly IManager $shareManager, |
| 39 | + private readonly IRootFolder $rootFolder, |
| 40 | + ) { |
| 41 | + parent::__construct(); |
| 42 | + } |
| 43 | + |
| 44 | + protected function configure() { |
| 45 | + parent::configure(); |
| 46 | + $this |
| 47 | + ->setName('share:list') |
| 48 | + ->setDescription('List available shares') |
| 49 | + ->addOption('owner', null, InputOption::VALUE_REQUIRED, 'only show shares owned by a specific user') |
| 50 | + ->addOption('recipient', null, InputOption::VALUE_REQUIRED, 'only show shares with a specific recipient') |
| 51 | + ->addOption('by', null, InputOption::VALUE_REQUIRED, 'only show shares with by as specific user') |
| 52 | + ->addOption('file', null, InputOption::VALUE_REQUIRED, 'only show shares of a specific file') |
| 53 | + ->addOption('parent', null, InputOption::VALUE_REQUIRED, 'only show shares of files inside a specific folder') |
| 54 | + ->addOption('recursive', null, InputOption::VALUE_NONE, 'also show shares nested deep inside the specified parent folder') |
| 55 | + ->addOption('type', null, InputOption::VALUE_REQUIRED, 'only show shares of a specific type') |
| 56 | + ->addOption('status', null, InputOption::VALUE_REQUIRED, 'only show shares with a specific status'); |
| 57 | + } |
| 58 | + |
| 59 | + public function execute(InputInterface $input, OutputInterface $output): int { |
| 60 | + if ($input->getOption('recursive') && !$input->getOption('parent')) { |
| 61 | + $output->writeln("<error>recursive option can't be used without parent option</error>"); |
| 62 | + return 1; |
| 63 | + } |
| 64 | + |
| 65 | + // todo: do some pre-filtering instead of first querying all shares |
| 66 | + /** @var \Iterator<IShare> $allShares */ |
| 67 | + $allShares = $this->shareManager->getAllShares(); |
| 68 | + $shares = new \CallbackFilterIterator($allShares, function (IShare $share) use ($input) { |
| 69 | + return $this->shouldShowShare($input, $share); |
| 70 | + }); |
| 71 | + $shares = iterator_to_array($shares); |
| 72 | + $data = array_map(function (IShare $share) { |
| 73 | + return [ |
| 74 | + 'id' => $share->getId(), |
| 75 | + 'file' => $share->getNodeId(), |
| 76 | + 'target-path' => $share->getTarget(), |
| 77 | + 'source-path' => $share->getNode()->getPath(), |
| 78 | + 'owner' => $share->getShareOwner(), |
| 79 | + 'recipient' => $share->getSharedWith(), |
| 80 | + 'by' => $share->getSharedBy(), |
| 81 | + 'type' => self::SHARE_TYPE_NAMES[$share->getShareType()] ?? 'unknown', |
| 82 | + ]; |
| 83 | + }, $shares); |
| 84 | + |
| 85 | + $this->writeTableInOutputFormat($input, $output, $data); |
| 86 | + return 0; |
| 87 | + } |
| 88 | + |
| 89 | + private function getFileId(string $file): int { |
| 90 | + if (is_numeric($file)) { |
| 91 | + return (int)$file; |
| 92 | + } |
| 93 | + return $this->getFile($file)->getId(); |
| 94 | + } |
| 95 | + |
| 96 | + private function getFile(string $file): Node { |
| 97 | + if (isset($this->fileCache[$file])) { |
| 98 | + return $this->fileCache[$file]; |
| 99 | + } |
| 100 | + |
| 101 | + if (is_numeric($file)) { |
| 102 | + $node = $this->rootFolder->getFirstNodeById((int)$file); |
| 103 | + if (!$node) { |
| 104 | + throw new NotFoundException("File with id $file not found"); |
| 105 | + } |
| 106 | + } else { |
| 107 | + $node = $this->rootFolder->get($file); |
| 108 | + } |
| 109 | + $this->fileCache[$file] = $node; |
| 110 | + return $node; |
| 111 | + } |
| 112 | + |
| 113 | + private function getShareType(string $type): int { |
| 114 | + foreach (self::SHARE_TYPE_NAMES as $shareType => $shareTypeName) { |
| 115 | + if ($shareTypeName === $type) { |
| 116 | + return $shareType; |
| 117 | + } |
| 118 | + } |
| 119 | + throw new \Exception("Unknown share type $type"); |
| 120 | + } |
| 121 | + |
| 122 | + private function shouldShowShare(InputInterface $input, IShare $share): bool { |
| 123 | + if ($input->getOption('owner') && $share->getShareOwner() !== $input->getOption('owner')) { |
| 124 | + return false; |
| 125 | + } |
| 126 | + if ($input->getOption('recipient') && $share->getSharedWith() !== $input->getOption('recipient')) { |
| 127 | + return false; |
| 128 | + } |
| 129 | + if ($input->getOption('by') && $share->getSharedBy() !== $input->getOption('by')) { |
| 130 | + return false; |
| 131 | + } |
| 132 | + if ($input->getOption('file') && $share->getNodeId() !== $this->getFileId($input->getOption('file'))) { |
| 133 | + return false; |
| 134 | + } |
| 135 | + if ($input->getOption('parent')) { |
| 136 | + $parent = $this->getFile($input->getOption('parent')); |
| 137 | + if (!$parent instanceof Folder) { |
| 138 | + throw new \Exception("Parent {$parent->getPath()} is not a folder"); |
| 139 | + } |
| 140 | + $recursive = $input->getOption('recursive'); |
| 141 | + if (!$recursive) { |
| 142 | + $shareCacheEntry = $share->getNodeCacheEntry(); |
| 143 | + if (!$shareCacheEntry) { |
| 144 | + $shareCacheEntry = $share->getNode(); |
| 145 | + } |
| 146 | + if ($shareCacheEntry->getParentId() !== $parent->getId()) { |
| 147 | + return false; |
| 148 | + } |
| 149 | + } else { |
| 150 | + $shareNode = $share->getNode(); |
| 151 | + if ($parent->getRelativePath($shareNode->getPath()) === null) { |
| 152 | + return false; |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + if ($input->getOption('type') && $share->getShareType() !== $this->getShareType($input->getOption('type'))) { |
| 157 | + return false; |
| 158 | + } |
| 159 | + return true; |
| 160 | + } |
| 161 | +} |
0 commit comments