Import.php 6.8 KB

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