ListCommand.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Robin Appelman <robin@icewind.nl>
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author John Molakvoæ <skjnldsv@protonmail.com>
  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 Symfony\Component\Console\Input\InputInterface;
  31. use Symfony\Component\Console\Input\InputOption;
  32. use Symfony\Component\Console\Output\OutputInterface;
  33. class ListCommand extends Base {
  34. public function __construct(
  35. protected IUserManager $userManager,
  36. protected IGroupManager $groupManager,
  37. ) {
  38. parent::__construct();
  39. }
  40. protected function configure() {
  41. $this
  42. ->setName('user:list')
  43. ->setDescription('list configured users')
  44. ->addOption(
  45. 'disabled',
  46. 'd',
  47. InputOption::VALUE_NONE,
  48. 'List disabled users only'
  49. )->addOption(
  50. 'limit',
  51. 'l',
  52. InputOption::VALUE_OPTIONAL,
  53. 'Number of users to retrieve',
  54. '500'
  55. )->addOption(
  56. 'offset',
  57. 'o',
  58. InputOption::VALUE_OPTIONAL,
  59. 'Offset for retrieving users',
  60. '0'
  61. )->addOption(
  62. 'output',
  63. null,
  64. InputOption::VALUE_OPTIONAL,
  65. 'Output format (plain, json or json_pretty, default is plain)',
  66. $this->defaultOutputFormat
  67. )->addOption(
  68. 'info',
  69. 'i',
  70. InputOption::VALUE_NONE,
  71. 'Show detailed info'
  72. );
  73. }
  74. protected function execute(InputInterface $input, OutputInterface $output): int {
  75. if ($input->getOption('disabled')) {
  76. $users = $this->userManager->getDisabledUsers((int) $input->getOption('limit'), (int) $input->getOption('offset'));
  77. } else {
  78. $users = $this->userManager->searchDisplayName('', (int) $input->getOption('limit'), (int) $input->getOption('offset'));
  79. }
  80. $this->writeArrayInOutputFormat($input, $output, $this->formatUsers($users, (bool)$input->getOption('info')));
  81. return 0;
  82. }
  83. /**
  84. * @param IUser[] $users
  85. * @param bool [$detailed=false]
  86. * @return array
  87. */
  88. private function formatUsers(array $users, bool $detailed = false) {
  89. $keys = array_map(function (IUser $user) {
  90. return $user->getUID();
  91. }, $users);
  92. $values = array_map(function (IUser $user) use ($detailed) {
  93. if ($detailed) {
  94. $groups = $this->groupManager->getUserGroupIds($user);
  95. return [
  96. 'user_id' => $user->getUID(),
  97. 'display_name' => $user->getDisplayName(),
  98. 'email' => (string)$user->getSystemEMailAddress(),
  99. 'cloud_id' => $user->getCloudId(),
  100. 'enabled' => $user->isEnabled(),
  101. 'groups' => $groups,
  102. 'quota' => $user->getQuota(),
  103. 'last_seen' => date(\DateTimeInterface::ATOM, $user->getLastLogin()), // ISO-8601
  104. 'user_directory' => $user->getHome(),
  105. 'backend' => $user->getBackendClassName()
  106. ];
  107. }
  108. return $user->getDisplayName();
  109. }, $users);
  110. return array_combine($keys, $values);
  111. }
  112. }