Create.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_External\Command;
  8. use OC\Core\Command\Base;
  9. use OC\Files\Filesystem;
  10. use OC\User\NoUserException;
  11. use OCA\Files_External\Lib\Auth\AuthMechanism;
  12. use OCA\Files_External\Lib\Backend\Backend;
  13. use OCA\Files_External\Lib\DefinitionParameter;
  14. use OCA\Files_External\Lib\StorageConfig;
  15. use OCA\Files_External\Service\BackendService;
  16. use OCA\Files_External\Service\GlobalStoragesService;
  17. use OCA\Files_External\Service\StoragesService;
  18. use OCA\Files_External\Service\UserStoragesService;
  19. use OCP\IUserManager;
  20. use OCP\IUserSession;
  21. use Symfony\Component\Console\Input\ArrayInput;
  22. use Symfony\Component\Console\Input\InputArgument;
  23. use Symfony\Component\Console\Input\InputInterface;
  24. use Symfony\Component\Console\Input\InputOption;
  25. use Symfony\Component\Console\Output\OutputInterface;
  26. use Symfony\Component\HttpFoundation\Response;
  27. class Create extends Base {
  28. public function __construct(
  29. private GlobalStoragesService $globalService,
  30. private UserStoragesService $userService,
  31. private IUserManager $userManager,
  32. private IUserSession $userSession,
  33. private BackendService $backendService,
  34. ) {
  35. parent::__construct();
  36. }
  37. protected function configure(): void {
  38. $this
  39. ->setName('files_external:create')
  40. ->setDescription('Create a new mount configuration')
  41. ->addOption(
  42. 'user',
  43. '',
  44. InputOption::VALUE_OPTIONAL,
  45. 'user to add the mount configuration for, if not set the mount will be added as system mount'
  46. )
  47. ->addArgument(
  48. 'mount_point',
  49. InputArgument::REQUIRED,
  50. 'mount point for the new mount'
  51. )
  52. ->addArgument(
  53. 'storage_backend',
  54. InputArgument::REQUIRED,
  55. 'storage backend identifier for the new mount, see `occ files_external:backends` for possible values'
  56. )
  57. ->addArgument(
  58. 'authentication_backend',
  59. InputArgument::REQUIRED,
  60. 'authentication backend identifier for the new mount, see `occ files_external:backends` for possible values'
  61. )
  62. ->addOption(
  63. 'config',
  64. 'c',
  65. InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
  66. 'Mount configuration option in key=value format'
  67. )
  68. ->addOption(
  69. 'dry',
  70. '',
  71. InputOption::VALUE_NONE,
  72. 'Don\'t save the created mount, only list the new mount'
  73. );
  74. parent::configure();
  75. }
  76. protected function execute(InputInterface $input, OutputInterface $output): int {
  77. $user = (string)$input->getOption('user');
  78. $mountPoint = $input->getArgument('mount_point');
  79. $storageIdentifier = $input->getArgument('storage_backend');
  80. $authIdentifier = $input->getArgument('authentication_backend');
  81. $configInput = $input->getOption('config');
  82. $storageBackend = $this->backendService->getBackend($storageIdentifier);
  83. $authBackend = $this->backendService->getAuthMechanism($authIdentifier);
  84. if (!Filesystem::isValidPath($mountPoint)) {
  85. $output->writeln('<error>Invalid mountpoint "' . $mountPoint . '"</error>');
  86. return self::FAILURE;
  87. }
  88. if (is_null($storageBackend)) {
  89. $output->writeln('<error>Storage backend with identifier "' . $storageIdentifier . '" not found (see `occ files_external:backends` for possible values)</error>');
  90. return Response::HTTP_NOT_FOUND;
  91. }
  92. if (is_null($authBackend)) {
  93. $output->writeln('<error>Authentication backend with identifier "' . $authIdentifier . '" not found (see `occ files_external:backends` for possible values)</error>');
  94. return Response::HTTP_NOT_FOUND;
  95. }
  96. $supportedSchemes = array_keys($storageBackend->getAuthSchemes());
  97. if (!in_array($authBackend->getScheme(), $supportedSchemes)) {
  98. $output->writeln('<error>Authentication backend "' . $authIdentifier . '" not valid for storage backend "' . $storageIdentifier . '" (see `occ files_external:backends storage ' . $storageIdentifier . '` for possible values)</error>');
  99. return self::FAILURE;
  100. }
  101. $config = [];
  102. foreach ($configInput as $configOption) {
  103. if (!str_contains($configOption, '=')) {
  104. $output->writeln('<error>Invalid mount configuration option "' . $configOption . '"</error>');
  105. return self::FAILURE;
  106. }
  107. [$key, $value] = explode('=', $configOption, 2);
  108. if (!$this->validateParam($key, $value, $storageBackend, $authBackend)) {
  109. $output->writeln('<error>Unknown configuration for backends "' . $key . '"</error>');
  110. return self::FAILURE;
  111. }
  112. $config[$key] = $value;
  113. }
  114. $mount = new StorageConfig();
  115. $mount->setMountPoint($mountPoint);
  116. $mount->setBackend($storageBackend);
  117. $mount->setAuthMechanism($authBackend);
  118. $mount->setBackendOptions($config);
  119. if ($user) {
  120. if (!$this->userManager->userExists($user)) {
  121. $output->writeln('<error>User "' . $user . '" not found</error>');
  122. return self::FAILURE;
  123. }
  124. $mount->setApplicableUsers([$user]);
  125. }
  126. if ($input->getOption('dry')) {
  127. $this->showMount($user, $mount, $input, $output);
  128. } else {
  129. $this->getStorageService($user)->addStorage($mount);
  130. if ($input->getOption('output') === self::OUTPUT_FORMAT_PLAIN) {
  131. $output->writeln('<info>Storage created with id ' . $mount->getId() . '</info>');
  132. } else {
  133. $output->writeln((string)$mount->getId());
  134. }
  135. }
  136. return self::SUCCESS;
  137. }
  138. private function validateParam(string $key, &$value, Backend $storageBackend, AuthMechanism $authBackend): bool {
  139. $params = array_merge($storageBackend->getParameters(), $authBackend->getParameters());
  140. foreach ($params as $param) {
  141. /** @var DefinitionParameter $param */
  142. if ($param->getName() === $key) {
  143. if ($param->getType() === DefinitionParameter::VALUE_BOOLEAN) {
  144. $value = ($value === 'true');
  145. }
  146. return true;
  147. }
  148. }
  149. return false;
  150. }
  151. private function showMount(string $user, StorageConfig $mount, InputInterface $input, OutputInterface $output): void {
  152. $listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager);
  153. $listInput = new ArrayInput([], $listCommand->getDefinition());
  154. $listInput->setOption('output', $input->getOption('output'));
  155. $listInput->setOption('show-password', true);
  156. $listCommand->listMounts($user, [$mount], $listInput, $output);
  157. }
  158. protected function getStorageService(string $userId): StoragesService {
  159. if (empty($userId)) {
  160. return $this->globalService;
  161. }
  162. $user = $this->userManager->get($userId);
  163. if (is_null($user)) {
  164. throw new NoUserException("user $userId not found");
  165. }
  166. $this->userSession->setUser($user);
  167. return $this->userService;
  168. }
  169. }