StorageAuthBase.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Files_External\Command;
  8. use OC\Core\Command\Base;
  9. use OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException;
  10. use OCA\Files_External\Lib\StorageConfig;
  11. use OCA\Files_External\NotFoundException;
  12. use OCA\Files_External\Service\GlobalStoragesService;
  13. use OCP\Files\Storage\IStorage;
  14. use OCP\Files\StorageNotAvailableException;
  15. use OCP\IUserManager;
  16. use Symfony\Component\Console\Input\InputInterface;
  17. use Symfony\Component\Console\Output\OutputInterface;
  18. abstract class StorageAuthBase extends Base {
  19. public function __construct(
  20. protected GlobalStoragesService $globalService,
  21. protected IUserManager $userManager,
  22. ) {
  23. parent::__construct();
  24. }
  25. private function getUserOption(InputInterface $input): ?string {
  26. if ($input->getOption('user')) {
  27. return (string)$input->getOption('user');
  28. }
  29. return $_ENV['NOTIFY_USER'] ?? $_SERVER['NOTIFY_USER'] ?? null;
  30. }
  31. private function getPasswordOption(InputInterface $input): ?string {
  32. if ($input->getOption('password')) {
  33. return (string)$input->getOption('password');
  34. }
  35. return $_ENV['NOTIFY_PASSWORD'] ?? $_SERVER['NOTIFY_PASSWORD'] ?? null;
  36. }
  37. /**
  38. * @param InputInterface $input
  39. * @param OutputInterface $output
  40. * @return array
  41. * @psalm-return array{0: StorageConfig, 1: IStorage}|array{0: null, 1: null}
  42. */
  43. protected function createStorage(InputInterface $input, OutputInterface $output): array {
  44. try {
  45. /** @var StorageConfig|null $mount */
  46. $mount = $this->globalService->getStorage((int)$input->getArgument('mount_id'));
  47. } catch (NotFoundException $e) {
  48. $output->writeln('<error>Mount not found</error>');
  49. return [null, null];
  50. }
  51. if (is_null($mount)) {
  52. $output->writeln('<error>Mount not found</error>');
  53. return [null, null];
  54. }
  55. $noAuth = false;
  56. $userOption = $this->getUserOption($input);
  57. $passwordOption = $this->getPasswordOption($input);
  58. // if only the user is provided, we get the user object to pass along to the auth backend
  59. // this allows using saved user credentials
  60. $user = ($userOption && !$passwordOption) ? $this->userManager->get($userOption) : null;
  61. try {
  62. $authBackend = $mount->getAuthMechanism();
  63. $authBackend->manipulateStorageConfig($mount, $user);
  64. } catch (InsufficientDataForMeaningfulAnswerException $e) {
  65. $noAuth = true;
  66. } catch (StorageNotAvailableException $e) {
  67. $noAuth = true;
  68. }
  69. if ($userOption) {
  70. $mount->setBackendOption('user', $userOption);
  71. }
  72. if ($passwordOption) {
  73. $mount->setBackendOption('password', $passwordOption);
  74. }
  75. try {
  76. $backend = $mount->getBackend();
  77. $backend->manipulateStorageConfig($mount, $user);
  78. } catch (InsufficientDataForMeaningfulAnswerException $e) {
  79. $noAuth = true;
  80. } catch (StorageNotAvailableException $e) {
  81. $noAuth = true;
  82. }
  83. try {
  84. $class = $mount->getBackend()->getStorageClass();
  85. /** @var IStorage $storage */
  86. $storage = new $class($mount->getBackendOptions());
  87. if (!$storage->test()) {
  88. throw new \Exception();
  89. }
  90. return [$mount, $storage];
  91. } catch (\Exception $e) {
  92. $output->writeln('<error>Error while trying to create storage</error>');
  93. if ($noAuth) {
  94. $output->writeln('<error>Username and/or password required</error>');
  95. }
  96. return [null, null];
  97. }
  98. }
  99. }