ListCommand.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\Files_External\Command;
  27. use OC\Core\Command\Base;
  28. use OC\User\NoUserException;
  29. use OCA\Files_External\Lib\StorageConfig;
  30. use OCA\Files_External\Service\GlobalStoragesService;
  31. use OCA\Files_External\Service\StoragesService;
  32. use OCA\Files_External\Service\UserStoragesService;
  33. use OCP\IUserManager;
  34. use OCP\IUserSession;
  35. use Symfony\Component\Console\Helper\Table;
  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 ListCommand extends Base {
  41. public const ALL = -1;
  42. public function __construct(
  43. protected GlobalStoragesService $globalService,
  44. protected UserStoragesService $userService,
  45. protected IUserSession $userSession,
  46. protected IUserManager $userManager,
  47. ) {
  48. parent::__construct();
  49. }
  50. protected function configure(): void {
  51. $this
  52. ->setName('files_external:list')
  53. ->setDescription('List configured admin or personal mounts')
  54. ->addArgument(
  55. 'user_id',
  56. InputArgument::OPTIONAL,
  57. 'user id to list the personal mounts for, if no user is provided admin mounts will be listed'
  58. )->addOption(
  59. 'show-password',
  60. '',
  61. InputOption::VALUE_NONE,
  62. 'show passwords and secrets'
  63. )->addOption(
  64. 'full',
  65. null,
  66. InputOption::VALUE_NONE,
  67. 'don\'t truncate long values in table output'
  68. )->addOption(
  69. 'all',
  70. 'a',
  71. InputOption::VALUE_NONE,
  72. 'show both system wide mounts and all personal mounts'
  73. );
  74. parent::configure();
  75. }
  76. protected function execute(InputInterface $input, OutputInterface $output): int {
  77. /** @var StorageConfig[] $mounts */
  78. if ($input->getOption('all')) {
  79. $mounts = $this->globalService->getStorageForAllUsers();
  80. $userId = self::ALL;
  81. } else {
  82. $userId = (string)$input->getArgument('user_id');
  83. $storageService = $this->getStorageService($userId);
  84. $mounts = $storageService->getAllStorages();
  85. }
  86. $this->listMounts($userId, $mounts, $input, $output);
  87. return self::SUCCESS;
  88. }
  89. /**
  90. * @param ?string|ListCommand::ALL $userId
  91. * @param StorageConfig[] $mounts
  92. */
  93. public function listMounts($userId, array $mounts, InputInterface $input, OutputInterface $output): void {
  94. $outputType = $input->getOption('output');
  95. if (count($mounts) === 0) {
  96. if ($outputType === self::OUTPUT_FORMAT_JSON || $outputType === self::OUTPUT_FORMAT_JSON_PRETTY) {
  97. $output->writeln('[]');
  98. } else {
  99. if ($userId === self::ALL) {
  100. $output->writeln("<info>No mounts configured</info>");
  101. } elseif ($userId) {
  102. $output->writeln("<info>No mounts configured by $userId</info>");
  103. } else {
  104. $output->writeln("<info>No admin mounts configured</info>");
  105. }
  106. }
  107. return;
  108. }
  109. $headers = ['Mount ID', 'Mount Point', 'Storage', 'Authentication Type', 'Configuration', 'Options'];
  110. if (!$userId || $userId === self::ALL) {
  111. $headers[] = 'Applicable Users';
  112. $headers[] = 'Applicable Groups';
  113. }
  114. if ($userId === self::ALL) {
  115. $headers[] = 'Type';
  116. }
  117. if (!$input->getOption('show-password')) {
  118. $hideKeys = ['key', 'bucket', 'secret', 'password', 'refresh_token', 'token', 'client_secret', 'public_key', 'private_key'];
  119. foreach ($mounts as $mount) {
  120. $config = $mount->getBackendOptions();
  121. foreach ($config as $key => $value) {
  122. if (in_array($key, $hideKeys)) {
  123. $mount->setBackendOption($key, '***REMOVED SENSITIVE VALUE***');
  124. }
  125. }
  126. }
  127. }
  128. if ($outputType === self::OUTPUT_FORMAT_JSON || $outputType === self::OUTPUT_FORMAT_JSON_PRETTY) {
  129. $keys = array_map(function ($header) {
  130. return strtolower(str_replace(' ', '_', $header));
  131. }, $headers);
  132. $pairs = array_map(function (StorageConfig $config) use ($keys, $userId) {
  133. $values = [
  134. $config->getId(),
  135. $config->getMountPoint(),
  136. $config->getBackend()->getStorageClass(),
  137. $config->getAuthMechanism()->getIdentifier(),
  138. $config->getBackendOptions(),
  139. $config->getMountOptions()
  140. ];
  141. if (!$userId || $userId === self::ALL) {
  142. $values[] = $config->getApplicableUsers();
  143. $values[] = $config->getApplicableGroups();
  144. }
  145. if ($userId === self::ALL) {
  146. $values[] = $config->getType() === StorageConfig::MOUNT_TYPE_ADMIN ? 'admin' : 'personal';
  147. }
  148. return array_combine($keys, $values);
  149. }, $mounts);
  150. if ($outputType === self::OUTPUT_FORMAT_JSON) {
  151. $output->writeln(json_encode(array_values($pairs)));
  152. } else {
  153. $output->writeln(json_encode(array_values($pairs), JSON_PRETTY_PRINT));
  154. }
  155. } else {
  156. $full = $input->getOption('full');
  157. $defaultMountOptions = [
  158. 'encrypt' => true,
  159. 'previews' => true,
  160. 'filesystem_check_changes' => 1,
  161. 'enable_sharing' => false,
  162. 'encoding_compatibility' => false,
  163. 'readonly' => false,
  164. ];
  165. $rows = array_map(function (StorageConfig $config) use ($userId, $defaultMountOptions, $full) {
  166. $storageConfig = $config->getBackendOptions();
  167. $keys = array_keys($storageConfig);
  168. $values = array_values($storageConfig);
  169. if (!$full) {
  170. $values = array_map(function ($value) {
  171. if (is_string($value) && strlen($value) > 32) {
  172. return substr($value, 0, 6) . '...' . substr($value, -6, 6);
  173. } else {
  174. return $value;
  175. }
  176. }, $values);
  177. }
  178. $configStrings = array_map(function ($key, $value) {
  179. return $key . ': ' . json_encode($value);
  180. }, $keys, $values);
  181. $configString = implode(', ', $configStrings);
  182. $mountOptions = $config->getMountOptions();
  183. // hide defaults
  184. foreach ($mountOptions as $key => $value) {
  185. if ($value === $defaultMountOptions[$key]) {
  186. unset($mountOptions[$key]);
  187. }
  188. }
  189. $keys = array_keys($mountOptions);
  190. $values = array_values($mountOptions);
  191. $optionsStrings = array_map(function ($key, $value) {
  192. return $key . ': ' . json_encode($value);
  193. }, $keys, $values);
  194. $optionsString = implode(', ', $optionsStrings);
  195. $values = [
  196. $config->getId(),
  197. $config->getMountPoint(),
  198. $config->getBackend()->getText(),
  199. $config->getAuthMechanism()->getText(),
  200. $configString,
  201. $optionsString
  202. ];
  203. if (!$userId || $userId === self::ALL) {
  204. $applicableUsers = implode(', ', $config->getApplicableUsers());
  205. $applicableGroups = implode(', ', $config->getApplicableGroups());
  206. if ($applicableUsers === '' && $applicableGroups === '') {
  207. $applicableUsers = 'All';
  208. }
  209. $values[] = $applicableUsers;
  210. $values[] = $applicableGroups;
  211. }
  212. if ($userId === self::ALL) {
  213. $values[] = $config->getType() === StorageConfig::MOUNT_TYPE_ADMIN ? 'Admin' : 'Personal';
  214. }
  215. return $values;
  216. }, $mounts);
  217. $table = new Table($output);
  218. $table->setHeaders($headers);
  219. $table->setRows($rows);
  220. $table->render();
  221. }
  222. }
  223. protected function getStorageService(string $userId): StoragesService {
  224. if (empty($userId)) {
  225. return $this->globalService;
  226. }
  227. $user = $this->userManager->get($userId);
  228. if (is_null($user)) {
  229. throw new NoUserException("user $userId not found");
  230. }
  231. $this->userSession->setUser($user);
  232. return $this->userService;
  233. }
  234. }