ListCommand.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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\UserStoragesService;
  32. use OCP\IUserManager;
  33. use OCP\IUserSession;
  34. use Symfony\Component\Console\Helper\Table;
  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 ListCommand extends Base {
  40. protected GlobalStoragesService $globalService;
  41. protected UserStoragesService $userService;
  42. protected IUserSession $userSession;
  43. protected IUserManager $userManager;
  44. public const ALL = -1;
  45. public function __construct(GlobalStoragesService $globalService, UserStoragesService $userService, IUserSession $userSession, IUserManager $userManager) {
  46. parent::__construct();
  47. $this->globalService = $globalService;
  48. $this->userService = $userService;
  49. $this->userSession = $userSession;
  50. $this->userManager = $userManager;
  51. }
  52. protected function configure(): void {
  53. $this
  54. ->setName('files_external:list')
  55. ->setDescription('List configured admin or personal mounts')
  56. ->addArgument(
  57. 'user_id',
  58. InputArgument::OPTIONAL,
  59. 'user id to list the personal mounts for, if no user is provided admin mounts will be listed'
  60. )->addOption(
  61. 'show-password',
  62. '',
  63. InputOption::VALUE_NONE,
  64. 'show passwords and secrets'
  65. )->addOption(
  66. 'full',
  67. null,
  68. InputOption::VALUE_NONE,
  69. 'don\'t truncate long values in table output'
  70. )->addOption(
  71. 'all',
  72. 'a',
  73. InputOption::VALUE_NONE,
  74. 'show both system wide mounts and all personal mounts'
  75. );
  76. parent::configure();
  77. }
  78. protected function execute(InputInterface $input, OutputInterface $output): int {
  79. /** @var StorageConfig[] $mounts */
  80. if ($input->getOption('all')) {
  81. $mounts = $this->globalService->getStorageForAllUsers();
  82. $userId = self::ALL;
  83. } else {
  84. $userId = (string)$input->getArgument('user_id');
  85. $storageService = $this->getStorageService($userId);
  86. $mounts = $storageService->getAllStorages();
  87. }
  88. $this->listMounts($userId, $mounts, $input, $output);
  89. return 0;
  90. }
  91. /**
  92. * @param ?string|ListCommand::ALL $userId
  93. * @param StorageConfig[] $mounts
  94. */
  95. public function listMounts($userId, array $mounts, InputInterface $input, OutputInterface $output): void {
  96. $outputType = $input->getOption('output');
  97. if (count($mounts) === 0) {
  98. if ($outputType === self::OUTPUT_FORMAT_JSON || $outputType === self::OUTPUT_FORMAT_JSON_PRETTY) {
  99. $output->writeln('[]');
  100. } else {
  101. if ($userId === self::ALL) {
  102. $output->writeln("<info>No mounts configured</info>");
  103. } elseif ($userId) {
  104. $output->writeln("<info>No mounts configured by $userId</info>");
  105. } else {
  106. $output->writeln("<info>No admin mounts configured</info>");
  107. }
  108. }
  109. return;
  110. }
  111. $headers = ['Mount ID', 'Mount Point', 'Storage', 'Authentication Type', 'Configuration', 'Options'];
  112. if (!$userId || $userId === self::ALL) {
  113. $headers[] = 'Applicable Users';
  114. $headers[] = 'Applicable Groups';
  115. }
  116. if ($userId === self::ALL) {
  117. $headers[] = 'Type';
  118. }
  119. if (!$input->getOption('show-password')) {
  120. $hideKeys = ['password', 'refresh_token', 'token', 'client_secret', 'public_key', 'private_key'];
  121. foreach ($mounts as $mount) {
  122. $config = $mount->getBackendOptions();
  123. foreach ($config as $key => $value) {
  124. if (in_array($key, $hideKeys)) {
  125. $mount->setBackendOption($key, '***');
  126. }
  127. }
  128. }
  129. }
  130. if ($outputType === self::OUTPUT_FORMAT_JSON || $outputType === self::OUTPUT_FORMAT_JSON_PRETTY) {
  131. $keys = array_map(function ($header) {
  132. return strtolower(str_replace(' ', '_', $header));
  133. }, $headers);
  134. $pairs = array_map(function (StorageConfig $config) use ($keys, $userId) {
  135. $values = [
  136. $config->getId(),
  137. $config->getMountPoint(),
  138. $config->getBackend()->getStorageClass(),
  139. $config->getAuthMechanism()->getIdentifier(),
  140. $config->getBackendOptions(),
  141. $config->getMountOptions()
  142. ];
  143. if (!$userId || $userId === self::ALL) {
  144. $values[] = $config->getApplicableUsers();
  145. $values[] = $config->getApplicableGroups();
  146. }
  147. if ($userId === self::ALL) {
  148. $values[] = $config->getType() === StorageConfig::MOUNT_TYPE_ADMIN ? 'admin' : 'personal';
  149. }
  150. return array_combine($keys, $values);
  151. }, $mounts);
  152. if ($outputType === self::OUTPUT_FORMAT_JSON) {
  153. $output->writeln(json_encode(array_values($pairs)));
  154. } else {
  155. $output->writeln(json_encode(array_values($pairs), JSON_PRETTY_PRINT));
  156. }
  157. } else {
  158. $full = $input->getOption('full');
  159. $defaultMountOptions = [
  160. 'encrypt' => true,
  161. 'previews' => true,
  162. 'filesystem_check_changes' => 1,
  163. 'enable_sharing' => false,
  164. 'encoding_compatibility' => false,
  165. 'readonly' => false,
  166. ];
  167. $rows = array_map(function (StorageConfig $config) use ($userId, $defaultMountOptions, $full) {
  168. $storageConfig = $config->getBackendOptions();
  169. $keys = array_keys($storageConfig);
  170. $values = array_values($storageConfig);
  171. if (!$full) {
  172. $values = array_map(function ($value) {
  173. if (is_string($value) && strlen($value) > 32) {
  174. return substr($value, 0, 6) . '...' . substr($value, -6, 6);
  175. } else {
  176. return $value;
  177. }
  178. }, $values);
  179. }
  180. $configStrings = array_map(function ($key, $value) {
  181. return $key . ': ' . json_encode($value);
  182. }, $keys, $values);
  183. $configString = implode(', ', $configStrings);
  184. $mountOptions = $config->getMountOptions();
  185. // hide defaults
  186. foreach ($mountOptions as $key => $value) {
  187. if ($value === $defaultMountOptions[$key]) {
  188. unset($mountOptions[$key]);
  189. }
  190. }
  191. $keys = array_keys($mountOptions);
  192. $values = array_values($mountOptions);
  193. $optionsStrings = array_map(function ($key, $value) {
  194. return $key . ': ' . json_encode($value);
  195. }, $keys, $values);
  196. $optionsString = implode(', ', $optionsStrings);
  197. $values = [
  198. $config->getId(),
  199. $config->getMountPoint(),
  200. $config->getBackend()->getText(),
  201. $config->getAuthMechanism()->getText(),
  202. $configString,
  203. $optionsString
  204. ];
  205. if (!$userId || $userId === self::ALL) {
  206. $applicableUsers = implode(', ', $config->getApplicableUsers());
  207. $applicableGroups = implode(', ', $config->getApplicableGroups());
  208. if ($applicableUsers === '' && $applicableGroups === '') {
  209. $applicableUsers = 'All';
  210. }
  211. $values[] = $applicableUsers;
  212. $values[] = $applicableGroups;
  213. }
  214. if ($userId === self::ALL) {
  215. $values[] = $config->getType() === StorageConfig::MOUNT_TYPE_ADMIN ? 'Admin' : 'Personal';
  216. }
  217. return $values;
  218. }, $mounts);
  219. $table = new Table($output);
  220. $table->setHeaders($headers);
  221. $table->setRows($rows);
  222. $table->render();
  223. }
  224. }
  225. protected function getStorageService($userId) {
  226. if (!empty($userId)) {
  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. } else {
  234. return $this->globalService;
  235. }
  236. }
  237. }