-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
add command to list shares #51399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
add command to list shares #51399
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2e9222a
feat: add getParentId to ICacheEntry
icewind1991 c573867
feat: add command to list shares
icewind1991 9626c08
fix: set share status for federated shares
icewind1991 b9723ea
feat: remove share status from share:list as it doesn't seem to conta…
icewind1991 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
| /** | ||
| * SPDX-FileCopyrightText: 2025 Robin Appelman <[email protected]> | ||
| * 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\IRootFolder; | ||
| use OCP\Files\Node; | ||
| use OCP\Files\NotFoundException; | ||
| 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 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', | ||
| ]; | ||
|
|
||
| public function __construct( | ||
| private readonly IManager $shareManager, | ||
| private readonly IRootFolder $rootFolder, | ||
| ) { | ||
| parent::__construct(); | ||
| } | ||
|
|
||
| protected function configure() { | ||
| parent::configure(); | ||
| $this | ||
| ->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('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 { | ||
| if ($input->getOption('recursive') && !$input->getOption('parent')) { | ||
| $output->writeln("<error>recursive option can't be used without parent option</error>"); | ||
| return 1; | ||
| } | ||
|
|
||
| // todo: do some pre-filtering instead of first querying all shares | ||
| /** @var \Iterator<IShare> $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', | ||
| ]; | ||
| }, $shares); | ||
|
|
||
| $this->writeTableInOutputFormat($input, $output, $data); | ||
| return 0; | ||
| } | ||
|
|
||
| private function getFileId(string $file): int { | ||
| if (is_numeric($file)) { | ||
| return (int)$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((int)$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 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 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) { | ||
| $shareCacheEntry = $share->getNodeCacheEntry(); | ||
| if (!$shareCacheEntry) { | ||
| $shareCacheEntry = $share->getNode(); | ||
| } | ||
| if ($shareCacheEntry->getParentId() !== $parent->getId()) { | ||
| return false; | ||
| } | ||
| } else { | ||
| $shareNode = $share->getNode(); | ||
| if ($parent->getRelativePath($shareNode->getPath()) === null) { | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
| if ($input->getOption('type') && $share->getShareType() !== $this->getShareType($input->getOption('type'))) { | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.