Import.php 6.9 KB

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