You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ListCommand.php 2.5KB

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