ListCommand.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Robin Appelman <robin@icewind.nl>
  4. *
  5. * @author Robin Appelman <robin@icewind.nl>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OC\Core\Command\User;
  24. use OC\Core\Command\Base;
  25. use OCP\IUser;
  26. use OCP\IUserManager;
  27. use Symfony\Component\Console\Input\InputInterface;
  28. use Symfony\Component\Console\Input\InputOption;
  29. use Symfony\Component\Console\Output\OutputInterface;
  30. class ListCommand extends Base {
  31. /** @var IUserManager */
  32. protected $userManager;
  33. /**
  34. * @param IUserManager $userManager
  35. */
  36. public function __construct(IUserManager $userManager) {
  37. $this->userManager = $userManager;
  38. parent::__construct();
  39. }
  40. protected function configure() {
  41. $this
  42. ->setName('user:list')
  43. ->setDescription('list configured users')
  44. ->addOption(
  45. 'limit',
  46. 'l',
  47. InputOption::VALUE_OPTIONAL,
  48. 'Number of users to retrieve',
  49. 500
  50. )->addOption(
  51. 'offset',
  52. 'o',
  53. InputOption::VALUE_OPTIONAL,
  54. 'Offset for retrieving users',
  55. 0
  56. )->addOption(
  57. 'output',
  58. null,
  59. InputOption::VALUE_OPTIONAL,
  60. 'Output format (plain, json or json_pretty, default is plain)',
  61. $this->defaultOutputFormat
  62. );
  63. }
  64. protected function execute(InputInterface $input, OutputInterface $output) {
  65. $users = $this->userManager->search('', (int)$input->getOption('limit'), (int)$input->getOption('offset'));
  66. $this->writeArrayInOutputFormat($input, $output, $this->formatUsers($users));
  67. }
  68. /**
  69. * @param IUser[] $users
  70. * @return array
  71. */
  72. private function formatUsers(array $users) {
  73. $keys = array_map(function (IUser $user) {
  74. return $user->getUID();
  75. }, $users);
  76. $values = array_map(function (IUser $user) {
  77. return $user->getDisplayName();
  78. }, $users);
  79. return array_combine($keys, $values);
  80. }
  81. }