1
0

Create.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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\UserStoragesService;
  36. use OCP\IUserManager;
  37. use OCP\IUserSession;
  38. use Symfony\Component\Console\Input\ArrayInput;
  39. use Symfony\Component\Console\Input\InputArgument;
  40. use Symfony\Component\Console\Input\InputInterface;
  41. use Symfony\Component\Console\Input\InputOption;
  42. use Symfony\Component\Console\Output\OutputInterface;
  43. class Create extends Base {
  44. /**
  45. * @var GlobalStoragesService
  46. */
  47. private $globalService;
  48. /**
  49. * @var UserStoragesService
  50. */
  51. private $userService;
  52. /**
  53. * @var IUserManager
  54. */
  55. private $userManager;
  56. /** @var BackendService */
  57. private $backendService;
  58. /** @var IUserSession */
  59. private $userSession;
  60. public function __construct(GlobalStoragesService $globalService,
  61. UserStoragesService $userService,
  62. IUserManager $userManager,
  63. IUserSession $userSession,
  64. BackendService $backendService
  65. ) {
  66. parent::__construct();
  67. $this->globalService = $globalService;
  68. $this->userService = $userService;
  69. $this->userManager = $userManager;
  70. $this->userSession = $userSession;
  71. $this->backendService = $backendService;
  72. }
  73. protected function configure() {
  74. $this
  75. ->setName('files_external:create')
  76. ->setDescription('Create a new mount configuration')
  77. ->addOption(
  78. 'user',
  79. '',
  80. InputOption::VALUE_OPTIONAL,
  81. 'user to add the mount configuration for, if not set the mount will be added as system mount'
  82. )
  83. ->addArgument(
  84. 'mount_point',
  85. InputArgument::REQUIRED,
  86. 'mount point for the new mount'
  87. )
  88. ->addArgument(
  89. 'storage_backend',
  90. InputArgument::REQUIRED,
  91. 'storage backend identifier for the new mount, see `occ files_external:backends` for possible values'
  92. )
  93. ->addArgument(
  94. 'authentication_backend',
  95. InputArgument::REQUIRED,
  96. 'authentication backend identifier for the new mount, see `occ files_external:backends` for possible values'
  97. )
  98. ->addOption(
  99. 'config',
  100. 'c',
  101. InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
  102. 'Mount configuration option in key=value format'
  103. )
  104. ->addOption(
  105. 'dry',
  106. '',
  107. InputOption::VALUE_NONE,
  108. 'Don\'t save the created mount, only list the new mount'
  109. );
  110. parent::configure();
  111. }
  112. protected function execute(InputInterface $input, OutputInterface $output): int {
  113. $user = (string) $input->getOption('user');
  114. $mountPoint = $input->getArgument('mount_point');
  115. $storageIdentifier = $input->getArgument('storage_backend');
  116. $authIdentifier = $input->getArgument('authentication_backend');
  117. $configInput = $input->getOption('config');
  118. $storageBackend = $this->backendService->getBackend($storageIdentifier);
  119. $authBackend = $this->backendService->getAuthMechanism($authIdentifier);
  120. if (!Filesystem::isValidPath($mountPoint)) {
  121. $output->writeln('<error>Invalid mountpoint "' . $mountPoint . '"</error>');
  122. return 1;
  123. }
  124. if (is_null($storageBackend)) {
  125. $output->writeln('<error>Storage backend with identifier "' . $storageIdentifier . '" not found (see `occ files_external:backends` for possible values)</error>');
  126. return 404;
  127. }
  128. if (is_null($authBackend)) {
  129. $output->writeln('<error>Authentication backend with identifier "' . $authIdentifier . '" not found (see `occ files_external:backends` for possible values)</error>');
  130. return 404;
  131. }
  132. $supportedSchemes = array_keys($storageBackend->getAuthSchemes());
  133. if (!in_array($authBackend->getScheme(), $supportedSchemes)) {
  134. $output->writeln('<error>Authentication backend "' . $authIdentifier . '" not valid for storage backend "' . $storageIdentifier . '" (see `occ files_external:backends storage ' . $storageIdentifier . '` for possible values)</error>');
  135. return 1;
  136. }
  137. $config = [];
  138. foreach ($configInput as $configOption) {
  139. if (!strpos($configOption, '=')) {
  140. $output->writeln('<error>Invalid mount configuration option "' . $configOption . '"</error>');
  141. return 1;
  142. }
  143. [$key, $value] = explode('=', $configOption, 2);
  144. if (!$this->validateParam($key, $value, $storageBackend, $authBackend)) {
  145. $output->writeln('<error>Unknown configuration for backends "' . $key . '"</error>');
  146. return 1;
  147. }
  148. $config[$key] = $value;
  149. }
  150. $mount = new StorageConfig();
  151. $mount->setMountPoint($mountPoint);
  152. $mount->setBackend($storageBackend);
  153. $mount->setAuthMechanism($authBackend);
  154. $mount->setBackendOptions($config);
  155. if ($user) {
  156. if (!$this->userManager->userExists($user)) {
  157. $output->writeln('<error>User "' . $user . '" not found</error>');
  158. return 1;
  159. }
  160. $mount->setApplicableUsers([$user]);
  161. }
  162. if ($input->getOption('dry')) {
  163. $this->showMount($user, $mount, $input, $output);
  164. } else {
  165. $this->getStorageService($user)->addStorage($mount);
  166. if ($input->getOption('output') === self::OUTPUT_FORMAT_PLAIN) {
  167. $output->writeln('<info>Storage created with id ' . $mount->getId() . '</info>');
  168. } else {
  169. $output->writeln((string)$mount->getId());
  170. }
  171. }
  172. return 0;
  173. }
  174. private function validateParam($key, &$value, Backend $storageBackend, AuthMechanism $authBackend) {
  175. $params = array_merge($storageBackend->getParameters(), $authBackend->getParameters());
  176. foreach ($params as $param) {
  177. /** @var DefinitionParameter $param */
  178. if ($param->getName() === $key) {
  179. if ($param->getType() === DefinitionParameter::VALUE_BOOLEAN) {
  180. $value = ($value === 'true');
  181. }
  182. return true;
  183. }
  184. }
  185. return false;
  186. }
  187. private function showMount($user, StorageConfig $mount, InputInterface $input, OutputInterface $output) {
  188. $listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager);
  189. $listInput = new ArrayInput([], $listCommand->getDefinition());
  190. $listInput->setOption('output', $input->getOption('output'));
  191. $listInput->setOption('show-password', true);
  192. $listCommand->listMounts($user, [$mount], $listInput, $output);
  193. }
  194. protected function getStorageService($userId) {
  195. if (!empty($userId)) {
  196. $user = $this->userManager->get($userId);
  197. if (is_null($user)) {
  198. throw new NoUserException("user $userId not found");
  199. }
  200. $this->userSession->setUser($user);
  201. return $this->userService;
  202. } else {
  203. return $this->globalService;
  204. }
  205. }
  206. }