Skip to content

Commit

Permalink
Merge pull request #39131 from fsamapoor/refactor_lib_files_external_…
Browse files Browse the repository at this point in the history
…commands

Refactors files_external app commands
  • Loading branch information
artonge committed Sep 28, 2023
2 parents c5293bb + eab7818 commit 45cac16
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 204 deletions.
24 changes: 9 additions & 15 deletions apps/files_external/lib/Command/Applicable.php
Expand Up @@ -34,21 +34,15 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\HttpFoundation\Response;

class Applicable extends Base {
protected GlobalStoragesService $globalService;
private IUserManager $userManager;
private IGroupManager $groupManager;

public function __construct(
GlobalStoragesService $globalService,
IUserManager $userManager,
IGroupManager $groupManager
protected GlobalStoragesService $globalService,
private IUserManager $userManager,
private IGroupManager $groupManager,
) {
parent::__construct();
$this->globalService = $globalService;
$this->userManager = $userManager;
$this->groupManager = $groupManager;
}

protected function configure(): void {
Expand Down Expand Up @@ -94,12 +88,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$mount = $this->globalService->getStorage($mountId);
} catch (NotFoundException $e) {
$output->writeln('<error>Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts</error>');
return 404;
return Response::HTTP_NOT_FOUND;
}

if ($mount->getType() === StorageConfig::MOUNT_TYPE_PERSONAl) {
$output->writeln('<error>Can\'t change applicables on personal mounts</error>');
return 1;
return self::FAILURE;
}

$addUsers = $input->getOption('add-user');
Expand All @@ -114,13 +108,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
foreach ($addUsers as $addUser) {
if (!$this->userManager->userExists($addUser)) {
$output->writeln('<error>User "' . $addUser . '" not found</error>');
return 404;
return Response::HTTP_NOT_FOUND;
}
}
foreach ($addGroups as $addGroup) {
if (!$this->groupManager->groupExists($addGroup)) {
$output->writeln('<error>Group "' . $addGroup . '" not found</error>');
return 404;
return Response::HTTP_NOT_FOUND;
}
}

Expand All @@ -142,6 +136,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
'users' => $applicableUsers,
'groups' => $applicableGroups
]);
return 0;
return self::SUCCESS;
}
}
17 changes: 7 additions & 10 deletions apps/files_external/lib/Command/Backends.php
Expand Up @@ -33,13 +33,10 @@
use Symfony\Component\Console\Output\OutputInterface;

class Backends extends Base {
private BackendService $backendService;

public function __construct(BackendService $backendService
public function __construct(
private BackendService $backendService,
) {
parent::__construct();

$this->backendService = $backendService;
}

protected function configure(): void {
Expand Down Expand Up @@ -72,24 +69,24 @@ protected function execute(InputInterface $input, OutputInterface $output): int
if ($type) {
if (!isset($data[$type])) {
$output->writeln('<error>Invalid type "' . $type . '". Possible values are "authentication" or "storage"</error>');
return 1;
return self::FAILURE;
}
$data = $data[$type];

if ($backend) {
if (!isset($data[$backend])) {
$output->writeln('<error>Unknown backend "' . $backend . '" of type "' . $type . '"</error>');
return 1;
return self::FAILURE;
}
$data = $data[$backend];
}
}

$this->writeArrayInOutputFormat($input, $output, $data);
return 0;
return self::SUCCESS;
}

private function serializeAuthBackend(\JsonSerializable $backend) {
private function serializeAuthBackend(\JsonSerializable $backend): array {
$data = $backend->jsonSerialize();
$result = [
'name' => $data['name'],
Expand All @@ -112,7 +109,7 @@ private function serializeAuthBackend(\JsonSerializable $backend) {
* @param DefinitionParameter[] $parameters
* @return string[]
*/
private function formatConfiguration(array $parameters) {
private function formatConfiguration(array $parameters): array {
$configuration = array_filter($parameters, function (DefinitionParameter $parameter) {
return $parameter->getType() !== DefinitionParameter::VALUE_HIDDEN;
});
Expand Down
20 changes: 8 additions & 12 deletions apps/files_external/lib/Command/Config.php
Expand Up @@ -31,13 +31,13 @@
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\HttpFoundation\Response;

class Config extends Base {
protected GlobalStoragesService $globalService;

public function __construct(GlobalStoragesService $globalService) {
public function __construct(
protected GlobalStoragesService $globalService,
) {
parent::__construct();
$this->globalService = $globalService;
}

protected function configure(): void {
Expand Down Expand Up @@ -67,7 +67,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$mount = $this->globalService->getStorage($mountId);
} catch (NotFoundException $e) {
$output->writeln('<error>Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts"</error>');
return 404;
return Response::HTTP_NOT_FOUND;
}

$value = $input->getArgument('value');
Expand All @@ -76,15 +76,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
} else {
$this->getOption($mount, $key, $output);
}
return 0;
return self::SUCCESS;
}

/**
* @param StorageConfig $mount
* @param string $key
* @param OutputInterface $output
*/
protected function getOption(StorageConfig $mount, $key, OutputInterface $output) {
protected function getOption(StorageConfig $mount, $key, OutputInterface $output): void {
if ($key === 'mountpoint' || $key === 'mount_point') {
$value = $mount->getMountPoint();
} else {
Expand All @@ -97,12 +95,10 @@ protected function getOption(StorageConfig $mount, $key, OutputInterface $output
}

/**
* @param StorageConfig $mount
* @param string $key
* @param string $value
* @param OutputInterface $output
*/
protected function setOption(StorageConfig $mount, $key, $value, OutputInterface $output) {
protected function setOption(StorageConfig $mount, $key, $value, OutputInterface $output): void {
$decoded = json_decode($value, true);
if (!is_null($decoded) && json_encode($decoded) === $value) {
$value = $decoded;
Expand Down
55 changes: 23 additions & 32 deletions apps/files_external/lib/Command/Create.php
Expand Up @@ -42,26 +42,17 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\HttpFoundation\Response;

class Create extends Base {
private GlobalStoragesService $globalService;
private UserStoragesService $userService;
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(
private GlobalStoragesService $globalService,
private UserStoragesService $userService,
private IUserManager $userManager,
private IUserSession $userSession,
private BackendService $backendService,
) {
parent::__construct();
$this->globalService = $globalService;
$this->userService = $userService;
$this->userManager = $userManager;
$this->userSession = $userSession;
$this->backendService = $backendService;
}

protected function configure(): void {
Expand Down Expand Up @@ -116,32 +107,32 @@ protected function execute(InputInterface $input, OutputInterface $output): int

if (!Filesystem::isValidPath($mountPoint)) {
$output->writeln('<error>Invalid mountpoint "' . $mountPoint . '"</error>');
return 1;
return self::FAILURE;
}
if (is_null($storageBackend)) {
$output->writeln('<error>Storage backend with identifier "' . $storageIdentifier . '" not found (see `occ files_external:backends` for possible values)</error>');
return 404;
return Response::HTTP_NOT_FOUND;
}
if (is_null($authBackend)) {
$output->writeln('<error>Authentication backend with identifier "' . $authIdentifier . '" not found (see `occ files_external:backends` for possible values)</error>');
return 404;
return Response::HTTP_NOT_FOUND;
}
$supportedSchemes = array_keys($storageBackend->getAuthSchemes());
if (!in_array($authBackend->getScheme(), $supportedSchemes)) {
$output->writeln('<error>Authentication backend "' . $authIdentifier . '" not valid for storage backend "' . $storageIdentifier . '" (see `occ files_external:backends storage ' . $storageIdentifier . '` for possible values)</error>');
return 1;
return self::FAILURE;
}

$config = [];
foreach ($configInput as $configOption) {
if (!str_contains($configOption, '=')) {
$output->writeln('<error>Invalid mount configuration option "' . $configOption . '"</error>');
return 1;
return self::FAILURE;
}
[$key, $value] = explode('=', $configOption, 2);
if (!$this->validateParam($key, $value, $storageBackend, $authBackend)) {
$output->writeln('<error>Unknown configuration for backends "' . $key . '"</error>');
return 1;
return self::FAILURE;
}
$config[$key] = $value;
}
Expand All @@ -155,7 +146,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
if ($user) {
if (!$this->userManager->userExists($user)) {
$output->writeln('<error>User "' . $user . '" not found</error>');
return 1;
return self::FAILURE;
}
$mount->setApplicableUsers([$user]);
}
Expand All @@ -170,7 +161,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$output->writeln((string)$mount->getId());
}
}
return 0;
return self::SUCCESS;
}

private function validateParam(string $key, &$value, Backend $storageBackend, AuthMechanism $authBackend): bool {
Expand All @@ -196,15 +187,15 @@ private function showMount(string $user, StorageConfig $mount, InputInterface $i
}

protected function getStorageService(string $userId): StoragesService {
if (!empty($userId)) {
$user = $this->userManager->get($userId);
if (is_null($user)) {
throw new NoUserException("user $userId not found");
}
$this->userSession->setUser($user);
return $this->userService;
} else {
if (empty($userId)) {
return $this->globalService;
}

$user = $this->userManager->get($userId);
if (is_null($user)) {
throw new NoUserException("user $userId not found");
}
$this->userSession->setUser($user);
return $this->userService;
}
}
23 changes: 10 additions & 13 deletions apps/files_external/lib/Command/Delete.php
Expand Up @@ -35,19 +35,16 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\HttpFoundation\Response;

class Delete extends Base {
protected GlobalStoragesService $globalService;
protected UserStoragesService $userService;
protected IUserSession $userSession;
protected IUserManager $userManager;

public function __construct(GlobalStoragesService $globalService, UserStoragesService $userService, IUserSession $userSession, IUserManager $userManager) {
public function __construct(
protected GlobalStoragesService $globalService,
protected UserStoragesService $userService,
protected IUserSession $userSession,
protected IUserManager $userManager,
) {
parent::__construct();
$this->globalService = $globalService;
$this->userService = $userService;
$this->userSession = $userSession;
$this->userManager = $userManager;
}

protected function configure(): void {
Expand All @@ -73,7 +70,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$mount = $this->globalService->getStorage($mountId);
} catch (NotFoundException $e) {
$output->writeln('<error>Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts"</error>');
return 404;
return Response::HTTP_NOT_FOUND;
}

$noConfirm = $input->getOption('yes');
Expand All @@ -88,11 +85,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$question = new ConfirmationQuestion('Delete this mount? [y/N] ', false);

if (!$questionHelper->ask($input, $output, $question)) {
return 1;
return self::FAILURE;
}
}

$this->globalService->removeStorage($mountId);
return 0;
return self::SUCCESS;
}
}
2 changes: 1 addition & 1 deletion apps/files_external/lib/Command/Export.php
Expand Up @@ -54,6 +54,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$listInput->setOption('show-password', true);
$listInput->setOption('full', true);
$listCommand->execute($listInput, $output);
return 0;
return self::SUCCESS;
}
}

0 comments on commit 45cac16

Please sign in to comment.