Create.php 7.3 KB

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