ListCommand.php 2.9 KB

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