From c0c5a19c26b38cb4cc0cae5a0a6372cd98471849 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 18 Oct 2022 15:22:11 +0200 Subject: [PATCH] allow filtering occ files_external:list to storages applicable for a user Signed-off-by: Robin Appelman --- apps/files_external/lib/Command/Create.php | 17 ++++++---- apps/files_external/lib/Command/Delete.php | 15 ++++++-- apps/files_external/lib/Command/Export.php | 2 +- apps/files_external/lib/Command/Import.php | 22 +++++++----- .../lib/Command/ListCommand.php | 34 +++++++++++++++---- .../tests/Command/ListCommandTest.php | 5 ++- 6 files changed, 70 insertions(+), 25 deletions(-) diff --git a/apps/files_external/lib/Command/Create.php b/apps/files_external/lib/Command/Create.php index 17e4731a2d695..06db82dff89c0 100644 --- a/apps/files_external/lib/Command/Create.php +++ b/apps/files_external/lib/Command/Create.php @@ -33,6 +33,7 @@ use OCA\Files_External\Lib\StorageConfig; use OCA\Files_External\Service\BackendService; use OCA\Files_External\Service\GlobalStoragesService; +use OCA\Files_External\Service\UserGlobalStoragesService; use OCA\Files_External\Service\UserStoragesService; use OCA\Files_External\Service\StoragesService; use OCP\IUserManager; @@ -46,19 +47,23 @@ class Create extends Base { private GlobalStoragesService $globalService; private UserStoragesService $userService; + private UserGlobalStoragesService $userGlobalService; private IUserManager $userManager; private BackendService $backendService; private IUserSession $userSession; - public function __construct(GlobalStoragesService $globalService, - UserStoragesService $userService, - IUserManager $userManager, - IUserSession $userSession, - BackendService $backendService + public function __construct( + GlobalStoragesService $globalService, + UserStoragesService $userService, + UserGlobalStoragesService $userGlobalService, + IUserManager $userManager, + IUserSession $userSession, + BackendService $backendService ) { parent::__construct(); $this->globalService = $globalService; $this->userService = $userService; + $this->userGlobalService = $userGlobalService; $this->userManager = $userManager; $this->userSession = $userSession; $this->backendService = $backendService; @@ -188,7 +193,7 @@ private function validateParam(string $key, &$value, Backend $storageBackend, Au } private function showMount(string $user, StorageConfig $mount, InputInterface $input, OutputInterface $output): void { - $listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager); + $listCommand = new ListCommand($this->globalService, $this->userService, $this->userGlobalService, $this->userSession, $this->userManager); $listInput = new ArrayInput([], $listCommand->getDefinition()); $listInput->setOption('output', $input->getOption('output')); $listInput->setOption('show-password', true); diff --git a/apps/files_external/lib/Command/Delete.php b/apps/files_external/lib/Command/Delete.php index cf09e3907b72c..214d2062a9790 100644 --- a/apps/files_external/lib/Command/Delete.php +++ b/apps/files_external/lib/Command/Delete.php @@ -26,6 +26,7 @@ use OC\Core\Command\Base; use OCA\Files_External\NotFoundException; use OCA\Files_External\Service\GlobalStoragesService; +use OCA\Files_External\Service\UserGlobalStoragesService; use OCA\Files_External\Service\UserStoragesService; use OCP\IUserManager; use OCP\IUserSession; @@ -39,13 +40,21 @@ class Delete extends Base { protected GlobalStoragesService $globalService; protected UserStoragesService $userService; + protected UserGlobalStoragesService $userGlobalService; protected IUserSession $userSession; protected IUserManager $userManager; - public function __construct(GlobalStoragesService $globalService, UserStoragesService $userService, IUserSession $userSession, IUserManager $userManager) { + public function __construct( + GlobalStoragesService $globalService, + UserStoragesService $userService, + UserGlobalStoragesService $userGlobalService, + IUserSession $userSession, + IUserManager $userManager + ) { parent::__construct(); $this->globalService = $globalService; $this->userService = $userService; + $this->userGlobalService = $userGlobalService; $this->userSession = $userSession; $this->userManager = $userManager; } @@ -79,10 +88,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int $noConfirm = $input->getOption('yes'); if (!$noConfirm) { - $listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager); + $listCommand = new ListCommand($this->globalService, $this->userService, $this->userGlobalService, $this->userSession, $this->userManager); $listInput = new ArrayInput([], $listCommand->getDefinition()); $listInput->setOption('output', $input->getOption('output')); - $listCommand->listMounts(null, [$mount], $listInput, $output); + $listCommand->listMounts("", [$mount], $listInput, $output); $questionHelper = $this->getHelper('question'); $question = new ConfirmationQuestion('Delete this mount? [y/N] ', false); diff --git a/apps/files_external/lib/Command/Export.php b/apps/files_external/lib/Command/Export.php index 98cfb68306021..8d53f9f88937c 100644 --- a/apps/files_external/lib/Command/Export.php +++ b/apps/files_external/lib/Command/Export.php @@ -46,7 +46,7 @@ protected function configure(): void { } protected function execute(InputInterface $input, OutputInterface $output): int { - $listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager); + $listCommand = new ListCommand($this->globalService, $this->userService, $this->userGlobalService, $this->userSession, $this->userManager); $listInput = new ArrayInput([], $listCommand->getDefinition()); $listInput->setArgument('user_id', $input->getArgument('user_id')); $listInput->setOption('all', $input->getOption('all')); diff --git a/apps/files_external/lib/Command/Import.php b/apps/files_external/lib/Command/Import.php index eab7dd9a6bedb..0af651841efb5 100644 --- a/apps/files_external/lib/Command/Import.php +++ b/apps/files_external/lib/Command/Import.php @@ -22,6 +22,7 @@ * along with this program. If not, see * */ + namespace OCA\Files_External\Command; use OC\Core\Command\Base; @@ -30,6 +31,7 @@ use OCA\Files_External\Service\BackendService; use OCA\Files_External\Service\GlobalStoragesService; use OCA\Files_External\Service\ImportLegacyStoragesService; +use OCA\Files_External\Service\UserGlobalStoragesService; use OCA\Files_External\Service\UserStoragesService; use OCP\IUserManager; use OCP\IUserSession; @@ -42,21 +44,25 @@ class Import extends Base { private GlobalStoragesService $globalService; private UserStoragesService $userService; + private UserGlobalStoragesService $userGlobalService; private IUserSession $userSession; private IUserManager $userManager; private ImportLegacyStoragesService $importLegacyStorageService; private BackendService $backendService; - public function __construct(GlobalStoragesService $globalService, - UserStoragesService $userService, - IUserSession $userSession, - IUserManager $userManager, - ImportLegacyStoragesService $importLegacyStorageService, - BackendService $backendService + public function __construct( + GlobalStoragesService $globalService, + UserStoragesService $userService, + UserGlobalStoragesService $userGlobalService, + IUserSession $userSession, + IUserManager $userManager, + ImportLegacyStoragesService $importLegacyStorageService, + BackendService $backendService ) { parent::__construct(); $this->globalService = $globalService; $this->userService = $userService; + $this->userGlobalService = $userGlobalService; $this->userSession = $userSession; $this->userManager = $userManager; $this->importLegacyStorageService = $importLegacyStorageService; @@ -88,7 +94,7 @@ protected function configure(): void { } protected function execute(InputInterface $input, OutputInterface $output): int { - $user = (string) $input->getOption('user'); + $user = (string)$input->getOption('user'); $path = $input->getArgument('path'); if ($path === '-') { $json = file_get_contents('php://stdin'); @@ -157,7 +163,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $output->writeln('No mounts to be imported'); return 1; } - $listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager); + $listCommand = new ListCommand($this->globalService, $this->userService, $this->userGlobalService, $this->userSession, $this->userManager); $listInput = new ArrayInput([], $listCommand->getDefinition()); $listInput->setOption('output', $input->getOption('output')); $listInput->setOption('show-password', true); diff --git a/apps/files_external/lib/Command/ListCommand.php b/apps/files_external/lib/Command/ListCommand.php index b2a4baf366b8a..6d70fdea2adb0 100644 --- a/apps/files_external/lib/Command/ListCommand.php +++ b/apps/files_external/lib/Command/ListCommand.php @@ -29,6 +29,7 @@ use OC\User\NoUserException; use OCA\Files_External\Lib\StorageConfig; use OCA\Files_External\Service\GlobalStoragesService; +use OCA\Files_External\Service\UserGlobalStoragesService; use OCA\Files_External\Service\UserStoragesService; use OCP\IUserManager; use OCP\IUserSession; @@ -41,15 +42,23 @@ class ListCommand extends Base { protected GlobalStoragesService $globalService; protected UserStoragesService $userService; + protected UserGlobalStoragesService $userGlobalService; protected IUserSession $userSession; protected IUserManager $userManager; - public const ALL = -1; + public const ALL = "__ALL__USERS__"; - public function __construct(GlobalStoragesService $globalService, UserStoragesService $userService, IUserSession $userSession, IUserManager $userManager) { + public function __construct( + GlobalStoragesService $globalService, + UserStoragesService $userService, + UserGlobalStoragesService $userGlobalService, + IUserSession $userSession, + IUserManager $userManager + ) { parent::__construct(); $this->globalService = $globalService; $this->userService = $userService; + $this->userGlobalService = $userGlobalService; $this->userSession = $userSession; $this->userManager = $userManager; } @@ -77,13 +86,26 @@ protected function configure(): void { 'a', InputOption::VALUE_NONE, 'show both system wide mounts and all personal mounts' + )->addOption( + 'for', + null, + InputOption::VALUE_REQUIRED, + 'show only mounts applicable for a specific user' ); parent::configure(); } protected function execute(InputInterface $input, OutputInterface $output): int { /** @var StorageConfig[] $mounts */ - if ($input->getOption('all')) { + if ($for = $input->getOption('for')) { + $forUser = $this->userManager->get($for); + if (!$forUser) { + $output->writeln("User $for not found"); + return 1; + } + $mounts = $this->userGlobalService->getAllStoragesForUser($forUser); + $userId = self::ALL; + } else if ($input->getOption('all')) { $mounts = $this->globalService->getStorageForAllUsers(); $userId = self::ALL; } else { @@ -97,10 +119,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int } /** - * @param ?string|ListCommand::ALL $userId + * @param string $userId * @param StorageConfig[] $mounts */ - public function listMounts($userId, array $mounts, InputInterface $input, OutputInterface $output): void { + public function listMounts(string $userId, array $mounts, InputInterface $input, OutputInterface $output): void { $outputType = $input->getOption('output'); if (count($mounts) === 0) { if ($outputType === self::OUTPUT_FORMAT_JSON || $outputType === self::OUTPUT_FORMAT_JSON_PRETTY) { @@ -245,7 +267,7 @@ public function listMounts($userId, array $mounts, InputInterface $input, Output } } - protected function getStorageService($userId) { + protected function getStorageService(string $userId) { if (!empty($userId)) { $user = $this->userManager->get($userId); if (is_null($user)) { diff --git a/apps/files_external/tests/Command/ListCommandTest.php b/apps/files_external/tests/Command/ListCommandTest.php index e9cd3496d71db..36f52d25f743b 100644 --- a/apps/files_external/tests/Command/ListCommandTest.php +++ b/apps/files_external/tests/Command/ListCommandTest.php @@ -33,6 +33,7 @@ use OCA\Files_External\Lib\Backend\Local; use OCA\Files_External\Lib\StorageConfig; use OCA\Files_External\Service\GlobalStoragesService; +use OCA\Files_External\Service\UserGlobalStoragesService; use OCA\Files_External\Service\UserStoragesService; use OCP\Authentication\LoginCredentials\IStore; use OCP\IL10N; @@ -51,12 +52,14 @@ private function getInstance() { $globalService = $this->createMock(GlobalStoragesService::class); /** @var UserStoragesService|\PHPUnit\Framework\MockObject\MockObject $userService */ $userService = $this->createMock(UserStoragesService::class); + /** @var UserStoragesService|\PHPUnit\Framework\MockObject\MockObject $userService */ + $userGlobalService = $this->createMock(UserGlobalStoragesService::class); /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject $userManager */ $userManager = $this->createMock(IUserManager::class); /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject $userSession */ $userSession = $this->createMock(IUserSession::class); - return new ListCommand($globalService, $userService, $userSession, $userManager); + return new ListCommand($globalService, $userService, $userGlobalService, $userSession, $userManager); } public function testListAuthIdentifier() {