ListCommand.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 Symfony\Component\Console\Input\InputInterface;
  12. use Symfony\Component\Console\Input\InputOption;
  13. use Symfony\Component\Console\Output\OutputInterface;
  14. class ListCommand extends Base {
  15. public function __construct(
  16. protected IUserManager $userManager,
  17. protected IGroupManager $groupManager,
  18. ) {
  19. parent::__construct();
  20. }
  21. protected function configure() {
  22. $this
  23. ->setName('user:list')
  24. ->setDescription('list configured users')
  25. ->addOption(
  26. 'disabled',
  27. 'd',
  28. InputOption::VALUE_NONE,
  29. 'List disabled users only'
  30. )->addOption(
  31. 'limit',
  32. 'l',
  33. InputOption::VALUE_OPTIONAL,
  34. 'Number of users to retrieve',
  35. '500'
  36. )->addOption(
  37. 'offset',
  38. 'o',
  39. InputOption::VALUE_OPTIONAL,
  40. 'Offset for retrieving users',
  41. '0'
  42. )->addOption(
  43. 'output',
  44. null,
  45. InputOption::VALUE_OPTIONAL,
  46. 'Output format (plain, json or json_pretty, default is plain)',
  47. $this->defaultOutputFormat
  48. )->addOption(
  49. 'info',
  50. 'i',
  51. InputOption::VALUE_NONE,
  52. 'Show detailed info'
  53. );
  54. }
  55. protected function execute(InputInterface $input, OutputInterface $output): int {
  56. if ($input->getOption('disabled')) {
  57. $users = $this->userManager->getDisabledUsers((int)$input->getOption('limit'), (int)$input->getOption('offset'));
  58. } else {
  59. $users = $this->userManager->searchDisplayName('', (int)$input->getOption('limit'), (int)$input->getOption('offset'));
  60. }
  61. $this->writeArrayInOutputFormat($input, $output, $this->formatUsers($users, (bool)$input->getOption('info')));
  62. return 0;
  63. }
  64. /**
  65. * @param IUser[] $users
  66. * @return \Generator<string,string|array>
  67. */
  68. private function formatUsers(array $users, bool $detailed = false): \Generator {
  69. foreach ($users as $user) {
  70. if ($detailed) {
  71. $groups = $this->groupManager->getUserGroupIds($user);
  72. $value = [
  73. 'user_id' => $user->getUID(),
  74. 'display_name' => $user->getDisplayName(),
  75. 'email' => (string)$user->getSystemEMailAddress(),
  76. 'cloud_id' => $user->getCloudId(),
  77. 'enabled' => $user->isEnabled(),
  78. 'groups' => $groups,
  79. 'quota' => $user->getQuota(),
  80. 'last_seen' => date(\DateTimeInterface::ATOM, $user->getLastLogin()), // ISO-8601
  81. 'user_directory' => $user->getHome(),
  82. 'backend' => $user->getBackendClassName()
  83. ];
  84. } else {
  85. $value = $user->getDisplayName();
  86. }
  87. yield $user->getUID() => $value;
  88. }
  89. }
  90. }