ListCommand.php 8.2 KB

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