Skip to content

Commit

Permalink
allow filtering occ files_external:list to storages applicable for a …
Browse files Browse the repository at this point in the history
…user

Signed-off-by: Robin Appelman <robin@icewind.nl>
  • Loading branch information
icewind1991 committed Apr 4, 2023
1 parent a1aa449 commit 8497e4e
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 24 deletions.
17 changes: 11 additions & 6 deletions apps/files_external/lib/Command/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
15 changes: 12 additions & 3 deletions apps/files_external/lib/Command/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion apps/files_external/lib/Command/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
22 changes: 14 additions & 8 deletions apps/files_external/lib/Command/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace OCA\Files_External\Command;

use OC\Core\Command\Base;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -157,7 +163,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$output->writeln('<error>No mounts to be imported</error>');
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);
Expand Down
34 changes: 28 additions & 6 deletions apps/files_external/lib/Command/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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("<error>User $for not found</error>");
return 1;
}
$mounts = $this->userGlobalService->getAllStoragesForUser($forUser);
$userId = self::ALL;
} else if ($input->getOption('all')) {
$mounts = $this->globalService->getStorageForAllUsers();
$userId = self::ALL;
} else {
Expand All @@ -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) {
Expand Down Expand Up @@ -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)) {
Expand Down

0 comments on commit 8497e4e

Please sign in to comment.