ListCommand.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Robin Appelman <robin@icewind.nl>
  4. *
  5. * @author Joas Schilling <coding@schilljs.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\Group;
  25. use OC\Core\Command\Base;
  26. use OCP\IGroup;
  27. use OCP\IGroupManager;
  28. use Symfony\Component\Console\Input\InputInterface;
  29. use Symfony\Component\Console\Input\InputOption;
  30. use Symfony\Component\Console\Output\OutputInterface;
  31. class ListCommand extends Base {
  32. /** @var IGroupManager */
  33. protected $groupManager;
  34. /**
  35. * @param IGroupManager $groupManager
  36. */
  37. public function __construct(IGroupManager $groupManager) {
  38. $this->groupManager = $groupManager;
  39. parent::__construct();
  40. }
  41. protected function configure() {
  42. $this
  43. ->setName('group:list')
  44. ->setDescription('list configured groups')
  45. ->addOption(
  46. 'limit',
  47. 'l',
  48. InputOption::VALUE_OPTIONAL,
  49. 'Number of groups to retrieve',
  50. 500
  51. )->addOption(
  52. 'offset',
  53. 'o',
  54. InputOption::VALUE_OPTIONAL,
  55. 'Offset for retrieving groups',
  56. 0
  57. )->addOption(
  58. 'output',
  59. null,
  60. InputOption::VALUE_OPTIONAL,
  61. 'Output format (plain, json or json_pretty, default is plain)',
  62. $this->defaultOutputFormat
  63. );
  64. }
  65. protected function execute(InputInterface $input, OutputInterface $output): int {
  66. $groups = $this->groupManager->search('', (int)$input->getOption('limit'), (int)$input->getOption('offset'));
  67. $this->writeArrayInOutputFormat($input, $output, $this->formatGroups($groups));
  68. return 0;
  69. }
  70. /**
  71. * @param IGroup[] $groups
  72. * @return array
  73. */
  74. private function formatGroups(array $groups) {
  75. $keys = array_map(function (IGroup $group) {
  76. return $group->getGID();
  77. }, $groups);
  78. $values = array_map(function (IGroup $group) {
  79. return array_keys($group->getUsers());
  80. }, $groups);
  81. return array_combine($keys, $values);
  82. }
  83. }