Import.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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\User\NoUserException;
  28. use OCA\Files_External\Lib\StorageConfig;
  29. use OCA\Files_External\Service\BackendService;
  30. use OCA\Files_External\Service\GlobalStoragesService;
  31. use OCA\Files_External\Service\ImportLegacyStoragesService;
  32. use OCA\Files_External\Service\StoragesService;
  33. use OCA\Files_External\Service\UserStoragesService;
  34. use OCP\IUserManager;
  35. use OCP\IUserSession;
  36. use Symfony\Component\Console\Input\ArrayInput;
  37. use Symfony\Component\Console\Input\InputArgument;
  38. use Symfony\Component\Console\Input\InputInterface;
  39. use Symfony\Component\Console\Input\InputOption;
  40. use Symfony\Component\Console\Output\OutputInterface;
  41. class Import extends Base {
  42. public function __construct(
  43. private GlobalStoragesService $globalService,
  44. private UserStoragesService $userService,
  45. private IUserSession $userSession,
  46. private IUserManager $userManager,
  47. private ImportLegacyStoragesService $importLegacyStorageService,
  48. private BackendService $backendService,
  49. ) {
  50. parent::__construct();
  51. }
  52. protected function configure(): void {
  53. $this
  54. ->setName('files_external:import')
  55. ->setDescription('Import mount configurations')
  56. ->addOption(
  57. 'user',
  58. '',
  59. InputOption::VALUE_OPTIONAL,
  60. 'user to add the mount configurations for, if not set the mount will be added as system mount'
  61. )
  62. ->addArgument(
  63. 'path',
  64. InputArgument::REQUIRED,
  65. 'path to a json file containing the mounts to import, use "-" to read from stdin'
  66. )
  67. ->addOption(
  68. 'dry',
  69. '',
  70. InputOption::VALUE_NONE,
  71. 'Don\'t save the imported mounts, only list the new mounts'
  72. );
  73. parent::configure();
  74. }
  75. protected function execute(InputInterface $input, OutputInterface $output): int {
  76. $user = (string) $input->getOption('user');
  77. $path = $input->getArgument('path');
  78. if ($path === '-') {
  79. $json = file_get_contents('php://stdin');
  80. } else {
  81. if (!file_exists($path)) {
  82. $output->writeln('<error>File not found: ' . $path . '</error>');
  83. return self::FAILURE;
  84. }
  85. $json = file_get_contents($path);
  86. }
  87. if (!is_string($json) || strlen($json) < 2) {
  88. $output->writeln('<error>Error while reading json</error>');
  89. return self::FAILURE;
  90. }
  91. $data = json_decode($json, true);
  92. if (!is_array($data)) {
  93. $output->writeln('<error>Error while parsing json</error>');
  94. return self::FAILURE;
  95. }
  96. $isLegacy = isset($data['user']) || isset($data['group']);
  97. if ($isLegacy) {
  98. $this->importLegacyStorageService->setData($data);
  99. $mounts = $this->importLegacyStorageService->getAllStorages();
  100. foreach ($mounts as $mount) {
  101. if ($mount->getBackendOption('password') === false) {
  102. $output->writeln('<error>Failed to decrypt password</error>');
  103. return self::FAILURE;
  104. }
  105. }
  106. } else {
  107. if (!isset($data[0])) { //normalize to an array of mounts
  108. $data = [$data];
  109. }
  110. $mounts = array_map([$this, 'parseData'], $data);
  111. }
  112. if ($user) {
  113. // ensure applicables are correct for personal mounts
  114. foreach ($mounts as $mount) {
  115. $mount->setApplicableGroups([]);
  116. $mount->setApplicableUsers([$user]);
  117. }
  118. }
  119. $storageService = $this->getStorageService($user);
  120. $existingMounts = $storageService->getAllStorages();
  121. foreach ($mounts as $mount) {
  122. foreach ($existingMounts as $existingMount) {
  123. if (
  124. $existingMount->getMountPoint() === $mount->getMountPoint() &&
  125. $existingMount->getApplicableGroups() === $mount->getApplicableGroups() &&
  126. $existingMount->getApplicableUsers() === $mount->getApplicableUsers() &&
  127. $existingMount->getBackendOptions() === $mount->getBackendOptions()
  128. ) {
  129. $output->writeln("<error>Duplicate mount (" . $mount->getMountPoint() . ")</error>");
  130. return self::FAILURE;
  131. }
  132. }
  133. }
  134. if ($input->getOption('dry')) {
  135. if (count($mounts) === 0) {
  136. $output->writeln('<error>No mounts to be imported</error>');
  137. return self::FAILURE;
  138. }
  139. $listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager);
  140. $listInput = new ArrayInput([], $listCommand->getDefinition());
  141. $listInput->setOption('output', $input->getOption('output'));
  142. $listInput->setOption('show-password', true);
  143. $listCommand->listMounts($user, $mounts, $listInput, $output);
  144. } else {
  145. foreach ($mounts as $mount) {
  146. $storageService->addStorage($mount);
  147. }
  148. }
  149. return self::SUCCESS;
  150. }
  151. private function parseData(array $data): StorageConfig {
  152. $mount = new StorageConfig($data['mount_id']);
  153. $mount->setMountPoint($data['mount_point']);
  154. $mount->setBackend($this->getBackendByClass($data['storage']));
  155. $authBackend = $this->backendService->getAuthMechanism($data['authentication_type']);
  156. $mount->setAuthMechanism($authBackend);
  157. $mount->setBackendOptions($data['configuration']);
  158. $mount->setMountOptions($data['options']);
  159. $mount->setApplicableUsers($data['applicable_users'] ?? []);
  160. $mount->setApplicableGroups($data['applicable_groups'] ?? []);
  161. return $mount;
  162. }
  163. private function getBackendByClass(string $className) {
  164. $backends = $this->backendService->getBackends();
  165. foreach ($backends as $backend) {
  166. if ($backend->getStorageClass() === $className) {
  167. return $backend;
  168. }
  169. }
  170. }
  171. protected function getStorageService(string $userId): StoragesService {
  172. if (empty($userId)) {
  173. return $this->globalService;
  174. }
  175. $user = $this->userManager->get($userId);
  176. if (is_null($user)) {
  177. throw new NoUserException("user $userId not found");
  178. }
  179. $this->userSession->setUser($user);
  180. return $this->userService;
  181. }
  182. }