ListCommand.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. * @param bool [$detailed=false]
  67. * @return array
  68. */
  69. private function formatUsers(array $users, bool $detailed = false) {
  70. $keys = array_map(function (IUser $user) {
  71. return $user->getUID();
  72. }, $users);
  73. $values = array_map(function (IUser $user) use ($detailed) {
  74. if ($detailed) {
  75. $groups = $this->groupManager->getUserGroupIds($user);
  76. return [
  77. 'user_id' => $user->getUID(),
  78. 'display_name' => $user->getDisplayName(),
  79. 'email' => (string)$user->getSystemEMailAddress(),
  80. 'cloud_id' => $user->getCloudId(),
  81. 'enabled' => $user->isEnabled(),
  82. 'groups' => $groups,
  83. 'quota' => $user->getQuota(),
  84. 'last_seen' => date(\DateTimeInterface::ATOM, $user->getLastLogin()), // ISO-8601
  85. 'user_directory' => $user->getHome(),
  86. 'backend' => $user->getBackendClassName()
  87. ];
  88. }
  89. return $user->getDisplayName();
  90. }, $users);
  91. return array_combine($keys, $values);
  92. }
  93. }