ListCommand.php 8.0 KB

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