Import.php 6.8 KB

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