Info.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\Core\Command\User;
  7. use OC\Core\Command\Base;
  8. use OCP\IGroupManager;
  9. use OCP\IUser;
  10. use OCP\IUserManager;
  11. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  12. use Symfony\Component\Console\Input\InputArgument;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Input\InputOption;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. class Info extends Base {
  17. public function __construct(
  18. protected IUserManager $userManager,
  19. protected IGroupManager $groupManager,
  20. ) {
  21. parent::__construct();
  22. }
  23. protected function configure() {
  24. $this
  25. ->setName('user:info')
  26. ->setDescription('show user info')
  27. ->addArgument(
  28. 'user',
  29. InputArgument::REQUIRED,
  30. 'user to show'
  31. )->addOption(
  32. 'output',
  33. null,
  34. InputOption::VALUE_OPTIONAL,
  35. 'Output format (plain, json or json_pretty, default is plain)',
  36. $this->defaultOutputFormat
  37. );
  38. }
  39. protected function execute(InputInterface $input, OutputInterface $output): int {
  40. $user = $this->userManager->get($input->getArgument('user'));
  41. if (is_null($user)) {
  42. $output->writeln('<error>user not found</error>');
  43. return 1;
  44. }
  45. $groups = $this->groupManager->getUserGroupIds($user);
  46. $data = [
  47. 'user_id' => $user->getUID(),
  48. 'display_name' => $user->getDisplayName(),
  49. 'email' => (string)$user->getSystemEMailAddress(),
  50. 'cloud_id' => $user->getCloudId(),
  51. 'enabled' => $user->isEnabled(),
  52. 'groups' => $groups,
  53. 'quota' => $user->getQuota(),
  54. 'storage' => $this->getStorageInfo($user),
  55. 'last_seen' => date(\DateTimeInterface::ATOM, $user->getLastLogin()), // ISO-8601
  56. 'user_directory' => $user->getHome(),
  57. 'backend' => $user->getBackendClassName()
  58. ];
  59. $this->writeArrayInOutputFormat($input, $output, $data);
  60. return 0;
  61. }
  62. /**
  63. * @param IUser $user
  64. * @return array
  65. */
  66. protected function getStorageInfo(IUser $user): array {
  67. \OC_Util::tearDownFS();
  68. \OC_Util::setupFS($user->getUID());
  69. try {
  70. $storage = \OC_Helper::getStorageInfo('/');
  71. } catch (\OCP\Files\NotFoundException $e) {
  72. return [];
  73. }
  74. return [
  75. 'free' => $storage['free'],
  76. 'used' => $storage['used'],
  77. 'total' => $storage['total'],
  78. 'relative' => $storage['relative'],
  79. 'quota' => $storage['quota'],
  80. ];
  81. }
  82. /**
  83. * @param string $argumentName
  84. * @param CompletionContext $context
  85. * @return string[]
  86. */
  87. public function completeArgumentValues($argumentName, CompletionContext $context) {
  88. if ($argumentName === 'user') {
  89. return array_map(static fn (IUser $user) => $user->getUID(), $this->userManager->search($context->getCurrentWord()));
  90. }
  91. return [];
  92. }
  93. }