Info.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Robin Appelman <robin@icewind.nl>
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC\Core\Command\User;
  26. use OC\Core\Command\Base;
  27. use OCP\IGroupManager;
  28. use OCP\IUser;
  29. use OCP\IUserManager;
  30. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  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 Info extends Base {
  36. protected IUserManager $userManager;
  37. protected IGroupManager $groupManager;
  38. public function __construct(IUserManager $userManager, IGroupManager $groupManager) {
  39. $this->userManager = $userManager;
  40. $this->groupManager = $groupManager;
  41. parent::__construct();
  42. }
  43. protected function configure() {
  44. $this
  45. ->setName('user:info')
  46. ->setDescription('show user info')
  47. ->addArgument(
  48. 'user',
  49. InputArgument::REQUIRED,
  50. 'user to show'
  51. )->addOption(
  52. 'output',
  53. null,
  54. InputOption::VALUE_OPTIONAL,
  55. 'Output format (plain, json or json_pretty, default is plain)',
  56. $this->defaultOutputFormat
  57. );
  58. }
  59. protected function execute(InputInterface $input, OutputInterface $output): int {
  60. $user = $this->userManager->get($input->getArgument('user'));
  61. if (is_null($user)) {
  62. $output->writeln('<error>user not found</error>');
  63. return 1;
  64. }
  65. $groups = $this->groupManager->getUserGroupIds($user);
  66. $data = [
  67. 'user_id' => $user->getUID(),
  68. 'display_name' => $user->getDisplayName(),
  69. 'email' => (string)$user->getSystemEMailAddress(),
  70. 'cloud_id' => $user->getCloudId(),
  71. 'enabled' => $user->isEnabled(),
  72. 'groups' => $groups,
  73. 'quota' => $user->getQuota(),
  74. 'storage' => $this->getStorageInfo($user),
  75. 'last_seen' => date(\DateTimeInterface::ATOM, $user->getLastLogin()), // ISO-8601
  76. 'user_directory' => $user->getHome(),
  77. 'backend' => $user->getBackendClassName()
  78. ];
  79. $this->writeArrayInOutputFormat($input, $output, $data);
  80. return 0;
  81. }
  82. /**
  83. * @param IUser $user
  84. * @return array
  85. */
  86. protected function getStorageInfo(IUser $user): array {
  87. \OC_Util::tearDownFS();
  88. \OC_Util::setupFS($user->getUID());
  89. try {
  90. $storage = \OC_Helper::getStorageInfo('/');
  91. } catch (\OCP\Files\NotFoundException $e) {
  92. return [];
  93. }
  94. return [
  95. 'free' => $storage['free'],
  96. 'used' => $storage['used'],
  97. 'total' => $storage['total'],
  98. 'relative' => $storage['relative'],
  99. 'quota' => $storage['quota'],
  100. ];
  101. }
  102. /**
  103. * @param string $argumentName
  104. * @param CompletionContext $context
  105. * @return string[]
  106. */
  107. public function completeArgumentValues($argumentName, CompletionContext $context) {
  108. if ($argumentName === 'user') {
  109. return array_map(static fn (IUser $user) => $user->getUID(), $this->userManager->search($context->getCurrentWord()));
  110. }
  111. return [];
  112. }
  113. }