ListCommand.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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\Group;
  7. use OC\Core\Command\Base;
  8. use OCP\IGroup;
  9. use OCP\IGroupManager;
  10. use Symfony\Component\Console\Input\InputInterface;
  11. use Symfony\Component\Console\Input\InputOption;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. class ListCommand extends Base {
  14. public function __construct(
  15. protected IGroupManager $groupManager,
  16. ) {
  17. parent::__construct();
  18. }
  19. protected function configure() {
  20. $this
  21. ->setName('group:list')
  22. ->setDescription('list configured groups')
  23. ->addOption(
  24. 'limit',
  25. 'l',
  26. InputOption::VALUE_OPTIONAL,
  27. 'Number of groups to retrieve',
  28. '500'
  29. )->addOption(
  30. 'offset',
  31. 'o',
  32. InputOption::VALUE_OPTIONAL,
  33. 'Offset for retrieving groups',
  34. '0'
  35. )->addOption(
  36. 'info',
  37. 'i',
  38. InputOption::VALUE_NONE,
  39. 'Show additional info (backend)'
  40. )->addOption(
  41. 'output',
  42. null,
  43. InputOption::VALUE_OPTIONAL,
  44. 'Output format (plain, json or json_pretty, default is plain)',
  45. $this->defaultOutputFormat
  46. );
  47. }
  48. protected function execute(InputInterface $input, OutputInterface $output): int {
  49. $groups = $this->groupManager->search('', (int)$input->getOption('limit'), (int)$input->getOption('offset'));
  50. $this->writeArrayInOutputFormat($input, $output, $this->formatGroups($groups, (bool)$input->getOption('info')));
  51. return 0;
  52. }
  53. /**
  54. * @param IGroup $group
  55. * @return string[]
  56. */
  57. public function usersForGroup(IGroup $group) {
  58. $users = array_keys($group->getUsers());
  59. return array_map(function ($userId) {
  60. return (string)$userId;
  61. }, $users);
  62. }
  63. /**
  64. * @param IGroup[] $groups
  65. */
  66. private function formatGroups(array $groups, bool $addInfo = false): \Generator {
  67. foreach ($groups as $group) {
  68. if ($addInfo) {
  69. $value = [
  70. 'displayName' => $group->getDisplayName(),
  71. 'backends' => $group->getBackendNames(),
  72. 'users' => $this->usersForGroup($group),
  73. ];
  74. } else {
  75. $value = $this->usersForGroup($group);
  76. }
  77. yield $group->getGID() => $value;
  78. }
  79. }
  80. }