1
0

ListCommand.php 2.5 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\Group;
  24. use OC\Core\Command\Base;
  25. use OCP\IGroup;
  26. use OCP\IGroupManager;
  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 IGroupManager */
  32. protected $groupManager;
  33. /**
  34. * @param IGroupManager $groupManager
  35. */
  36. public function __construct(IGroupManager $groupManager) {
  37. $this->groupManager = $groupManager;
  38. parent::__construct();
  39. }
  40. protected function configure() {
  41. $this
  42. ->setName('group:list')
  43. ->setDescription('list configured groups')
  44. ->addOption(
  45. 'limit',
  46. 'l',
  47. InputOption::VALUE_OPTIONAL,
  48. 'Number of groups to retrieve',
  49. 500
  50. )->addOption(
  51. 'offset',
  52. 'o',
  53. InputOption::VALUE_OPTIONAL,
  54. 'Offset for retrieving groups',
  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. $groups = $this->groupManager->search('', (int)$input->getOption('limit'), (int)$input->getOption('offset'));
  66. $this->writeArrayInOutputFormat($input, $output, $this->formatGroups($groups));
  67. }
  68. /**
  69. * @param IGroup[] $groups
  70. * @return array
  71. */
  72. private function formatGroups(array $groups) {
  73. $keys = array_map(function (IGroup $group) {
  74. return $group->getGID();
  75. }, $groups);
  76. $values = array_map(function (IGroup $group) {
  77. return array_keys($group->getUsers());
  78. }, $groups);
  79. return array_combine($keys, $values);
  80. }
  81. }