Create.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\Files_External\Command;
  26. use OC\Core\Command\Base;
  27. use OC\Files\Filesystem;
  28. use OC\User\NoUserException;
  29. use OCA\Files_External\Lib\Auth\AuthMechanism;
  30. use OCA\Files_External\Lib\Backend\Backend;
  31. use OCA\Files_External\Lib\DefinitionParameter;
  32. use OCA\Files_External\Lib\StorageConfig;
  33. use OCA\Files_External\Service\BackendService;
  34. use OCA\Files_External\Service\GlobalStoragesService;
  35. use OCA\Files_External\Service\StoragesService;
  36. use OCA\Files_External\Service\UserStoragesService;
  37. use OCP\IUserManager;
  38. use OCP\IUserSession;
  39. use Symfony\Component\Console\Input\ArrayInput;
  40. use Symfony\Component\Console\Input\InputArgument;
  41. use Symfony\Component\Console\Input\InputInterface;
  42. use Symfony\Component\Console\Input\InputOption;
  43. use Symfony\Component\Console\Output\OutputInterface;
  44. use Symfony\Component\HttpFoundation\Response;
  45. class Create extends Base {
  46. public function __construct(
  47. private GlobalStoragesService $globalService,
  48. private UserStoragesService $userService,
  49. private IUserManager $userManager,
  50. private IUserSession $userSession,
  51. private BackendService $backendService,
  52. ) {
  53. parent::__construct();
  54. }
  55. protected function configure(): void {
  56. $this
  57. ->setName('files_external:create')
  58. ->setDescription('Create a new mount configuration')
  59. ->addOption(
  60. 'user',
  61. '',
  62. InputOption::VALUE_OPTIONAL,
  63. 'user to add the mount configuration for, if not set the mount will be added as system mount'
  64. )
  65. ->addArgument(
  66. 'mount_point',
  67. InputArgument::REQUIRED,
  68. 'mount point for the new mount'
  69. )
  70. ->addArgument(
  71. 'storage_backend',
  72. InputArgument::REQUIRED,
  73. 'storage backend identifier for the new mount, see `occ files_external:backends` for possible values'
  74. )
  75. ->addArgument(
  76. 'authentication_backend',
  77. InputArgument::REQUIRED,
  78. 'authentication backend identifier for the new mount, see `occ files_external:backends` for possible values'
  79. )
  80. ->addOption(
  81. 'config',
  82. 'c',
  83. InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
  84. 'Mount configuration option in key=value format'
  85. )
  86. ->addOption(
  87. 'dry',
  88. '',
  89. InputOption::VALUE_NONE,
  90. 'Don\'t save the created mount, only list the new mount'
  91. );
  92. parent::configure();
  93. }
  94. protected function execute(InputInterface $input, OutputInterface $output): int {
  95. $user = (string)$input->getOption('user');
  96. $mountPoint = $input->getArgument('mount_point');
  97. $storageIdentifier = $input->getArgument('storage_backend');
  98. $authIdentifier = $input->getArgument('authentication_backend');
  99. $configInput = $input->getOption('config');
  100. $storageBackend = $this->backendService->getBackend($storageIdentifier);
  101. $authBackend = $this->backendService->getAuthMechanism($authIdentifier);
  102. if (!Filesystem::isValidPath($mountPoint)) {
  103. $output->writeln('<error>Invalid mountpoint "' . $mountPoint . '"</error>');
  104. return self::FAILURE;
  105. }
  106. if (is_null($storageBackend)) {
  107. $output->writeln('<error>Storage backend with identifier "' . $storageIdentifier . '" not found (see `occ files_external:backends` for possible values)</error>');
  108. return Response::HTTP_NOT_FOUND;
  109. }
  110. if (is_null($authBackend)) {
  111. $output->writeln('<error>Authentication backend with identifier "' . $authIdentifier . '" not found (see `occ files_external:backends` for possible values)</error>');
  112. return Response::HTTP_NOT_FOUND;
  113. }
  114. $supportedSchemes = array_keys($storageBackend->getAuthSchemes());
  115. if (!in_array($authBackend->getScheme(), $supportedSchemes)) {
  116. $output->writeln('<error>Authentication backend "' . $authIdentifier . '" not valid for storage backend "' . $storageIdentifier . '" (see `occ files_external:backends storage ' . $storageIdentifier . '` for possible values)</error>');
  117. return self::FAILURE;
  118. }
  119. $config = [];
  120. foreach ($configInput as $configOption) {
  121. if (!str_contains($configOption, '=')) {
  122. $output->writeln('<error>Invalid mount configuration option "' . $configOption . '"</error>');
  123. return self::FAILURE;
  124. }
  125. [$key, $value] = explode('=', $configOption, 2);
  126. if (!$this->validateParam($key, $value, $storageBackend, $authBackend)) {
  127. $output->writeln('<error>Unknown configuration for backends "' . $key . '"</error>');
  128. return self::FAILURE;
  129. }
  130. $config[$key] = $value;
  131. }
  132. $mount = new StorageConfig();
  133. $mount->setMountPoint($mountPoint);
  134. $mount->setBackend($storageBackend);
  135. $mount->setAuthMechanism($authBackend);
  136. $mount->setBackendOptions($config);
  137. if ($user) {
  138. if (!$this->userManager->userExists($user)) {
  139. $output->writeln('<error>User "' . $user . '" not found</error>');
  140. return self::FAILURE;
  141. }
  142. $mount->setApplicableUsers([$user]);
  143. }
  144. if ($input->getOption('dry')) {
  145. $this->showMount($user, $mount, $input, $output);
  146. } else {
  147. $this->getStorageService($user)->addStorage($mount);
  148. if ($input->getOption('output') === self::OUTPUT_FORMAT_PLAIN) {
  149. $output->writeln('<info>Storage created with id ' . $mount->getId() . '</info>');
  150. } else {
  151. $output->writeln((string)$mount->getId());
  152. }
  153. }
  154. return self::SUCCESS;
  155. }
  156. private function validateParam(string $key, &$value, Backend $storageBackend, AuthMechanism $authBackend): bool {
  157. $params = array_merge($storageBackend->getParameters(), $authBackend->getParameters());
  158. foreach ($params as $param) {
  159. /** @var DefinitionParameter $param */
  160. if ($param->getName() === $key) {
  161. if ($param->getType() === DefinitionParameter::VALUE_BOOLEAN) {
  162. $value = ($value === 'true');
  163. }
  164. return true;
  165. }
  166. }
  167. return false;
  168. }
  169. private function showMount(string $user, StorageConfig $mount, InputInterface $input, OutputInterface $output): void {
  170. $listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager);
  171. $listInput = new ArrayInput([], $listCommand->getDefinition());
  172. $listInput->setOption('output', $input->getOption('output'));
  173. $listInput->setOption('show-password', true);
  174. $listCommand->listMounts($user, [$mount], $listInput, $output);
  175. }
  176. protected function getStorageService(string $userId): StoragesService {
  177. if (empty($userId)) {
  178. return $this->globalService;
  179. }
  180. $user = $this->userManager->get($userId);
  181. if (is_null($user)) {
  182. throw new NoUserException("user $userId not found");
  183. }
  184. $this->userSession->setUser($user);
  185. return $this->userService;
  186. }
  187. }