ListCommand.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. public function __construct(
  34. protected IGroupManager $groupManager,
  35. ) {
  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 $group
  74. * @return string[]
  75. */
  76. public function usersForGroup(IGroup $group) {
  77. $users = array_keys($group->getUsers());
  78. return array_map(function ($userId) {
  79. return (string)$userId;
  80. }, $users);
  81. }
  82. /**
  83. * @param IGroup[] $groups
  84. * @return array
  85. */
  86. private function formatGroups(array $groups, bool $addInfo = false) {
  87. $keys = array_map(function (IGroup $group) {
  88. return $group->getGID();
  89. }, $groups);
  90. if ($addInfo) {
  91. $values = array_map(function (IGroup $group) {
  92. return [
  93. 'backends' => $group->getBackendNames(),
  94. 'users' => $this->usersForGroup($group),
  95. ];
  96. }, $groups);
  97. } else {
  98. $values = array_map(function (IGroup $group) {
  99. return $this->usersForGroup($group);
  100. }, $groups);
  101. }
  102. return array_combine($keys, $values);
  103. }
  104. }