ListCommand.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. protected IGroupManager $groupManager;
  34. public function __construct(IGroupManager $groupManager) {
  35. $this->groupManager = $groupManager;
  36. parent::__construct();
  37. }
  38. protected function configure() {
  39. $this
  40. ->setName('group:list')
  41. ->setDescription('list configured groups')
  42. ->addOption(
  43. 'limit',
  44. 'l',
  45. InputOption::VALUE_OPTIONAL,
  46. 'Number of groups to retrieve',
  47. '500'
  48. )->addOption(
  49. 'offset',
  50. 'o',
  51. InputOption::VALUE_OPTIONAL,
  52. 'Offset for retrieving groups',
  53. '0'
  54. )->addOption(
  55. 'info',
  56. 'i',
  57. InputOption::VALUE_NONE,
  58. 'Show additional info (backend)'
  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): int {
  68. $groups = $this->groupManager->search('', (int)$input->getOption('limit'), (int)$input->getOption('offset'));
  69. $this->writeArrayInOutputFormat($input, $output, $this->formatGroups($groups, (bool)$input->getOption('info')));
  70. return 0;
  71. }
  72. /**
  73. * @param IGroup[] $groups
  74. * @return array
  75. */
  76. private function formatGroups(array $groups, bool $addInfo = false) {
  77. $keys = array_map(function (IGroup $group) {
  78. return $group->getGID();
  79. }, $groups);
  80. if ($addInfo) {
  81. $values = array_map(function (IGroup $group) {
  82. return [
  83. 'backends' => $group->getBackendNames(),
  84. 'users' => array_keys($group->getUsers()),
  85. ];
  86. }, $groups);
  87. } else {
  88. $values = array_map(function (IGroup $group) {
  89. return array_keys($group->getUsers());
  90. }, $groups);
  91. }
  92. return array_combine($keys, $values);
  93. }
  94. }