ListCommand.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Robin Appelman <robin@icewind.nl>
  4. *
  5. * @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  6. * @author Robin Appelman <robin@icewind.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Core\Command\User;
  25. use OC\Core\Command\Base;
  26. use OCP\IGroupManager;
  27. use OCP\IUser;
  28. use OCP\IUserManager;
  29. use Symfony\Component\Console\Input\InputInterface;
  30. use Symfony\Component\Console\Input\InputOption;
  31. use Symfony\Component\Console\Output\OutputInterface;
  32. class ListCommand extends Base {
  33. /** @var IUserManager */
  34. protected $userManager;
  35. /** @var IGroupManager */
  36. protected $groupManager;
  37. /**
  38. * @param IUserManager $userManager
  39. * @param IGroupManager $groupManager
  40. */
  41. public function __construct(IUserManager $userManager,
  42. IGroupManager $groupManager) {
  43. $this->userManager = $userManager;
  44. $this->groupManager = $groupManager;
  45. parent::__construct();
  46. }
  47. protected function configure() {
  48. $this
  49. ->setName('user:list')
  50. ->setDescription('list configured users')
  51. ->addOption(
  52. 'limit',
  53. 'l',
  54. InputOption::VALUE_OPTIONAL,
  55. 'Number of users to retrieve',
  56. 500
  57. )->addOption(
  58. 'offset',
  59. 'o',
  60. InputOption::VALUE_OPTIONAL,
  61. 'Offset for retrieving users',
  62. 0
  63. )->addOption(
  64. 'output',
  65. null,
  66. InputOption::VALUE_OPTIONAL,
  67. 'Output format (plain, json or json_pretty, default is plain)',
  68. $this->defaultOutputFormat
  69. )->addOption(
  70. 'info',
  71. 'i',
  72. InputOption::VALUE_NONE,
  73. 'Show detailed info'
  74. );
  75. }
  76. protected function execute(InputInterface $input, OutputInterface $output): int {
  77. $users = $this->userManager->search('', (int) $input->getOption('limit'), (int) $input->getOption('offset'));
  78. $this->writeArrayInOutputFormat($input, $output, $this->formatUsers($users, (bool)$input->getOption('info')));
  79. return 0;
  80. }
  81. /**
  82. * @param IUser[] $users
  83. * @param bool [$detailed=false]
  84. * @return array
  85. */
  86. private function formatUsers(array $users, bool $detailed = false) {
  87. $keys = array_map(function (IUser $user) {
  88. return $user->getUID();
  89. }, $users);
  90. $values = array_map(function (IUser $user) use ($detailed) {
  91. if ($detailed) {
  92. $groups = $this->groupManager->getUserGroupIds($user);
  93. return [
  94. 'user_id' => $user->getUID(),
  95. 'display_name' => $user->getDisplayName(),
  96. 'email' => $user->getEMailAddress() ? $user->getEMailAddress() : '',
  97. 'cloud_id' => $user->getCloudId(),
  98. 'enabled' => $user->isEnabled(),
  99. 'groups' => $groups,
  100. 'quota' => $user->getQuota(),
  101. 'last_seen' => date(\DateTime::ATOM, $user->getLastLogin()), // ISO-8601
  102. 'user_directory' => $user->getHome(),
  103. 'backend' => $user->getBackendClassName()
  104. ];
  105. }
  106. return $user->getDisplayName();
  107. }, $users);
  108. return array_combine($keys, $values);
  109. }
  110. }