Show.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. declare(strict_types = 1);
  3. /**
  4. * @copyright Copyright (c) 2023 Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
  5. *
  6. * @author Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Settings\Command\AdminDelegation;
  25. use OC\Core\Command\Base;
  26. use OC\Settings\AuthorizedGroup;
  27. use OCA\Settings\Service\AuthorizedGroupService;
  28. use OCP\Settings\IDelegatedSettings;
  29. use OCP\Settings\IManager;
  30. use OCP\Settings\ISettings;
  31. use Symfony\Component\Console\Input\InputInterface;
  32. use Symfony\Component\Console\Output\OutputInterface;
  33. use Symfony\Component\Console\Style\SymfonyStyle;
  34. class Show extends Base {
  35. public function __construct(
  36. private IManager $settingManager,
  37. private AuthorizedGroupService $authorizedGroupService,
  38. ) {
  39. parent::__construct();
  40. }
  41. protected function configure(): void {
  42. $this
  43. ->setName('admin-delegation:show')
  44. ->setDescription('show delegated settings')
  45. ;
  46. }
  47. protected function execute(InputInterface $input, OutputInterface $output): int {
  48. $io = new SymfonyStyle($input, $output);
  49. $io->title('Current delegations');
  50. $sections = $this->settingManager->getAdminSections();
  51. $settings = [];
  52. $headers = ['Name', 'SettingId', 'Delegated to groups'];
  53. foreach ($sections as $sectionPriority) {
  54. foreach ($sectionPriority as $section) {
  55. $sectionSettings = $this->settingManager->getAdminSettings($section->getId());
  56. $sectionSettings = array_reduce($sectionSettings, [$this, 'getDelegatedSettings'], []);
  57. if (empty($sectionSettings)) {
  58. continue;
  59. }
  60. $io->section('Section: '.$section->getID());
  61. $io->table($headers, array_map(function (IDelegatedSettings $setting) use ($section) {
  62. $className = get_class($setting);
  63. $groups = array_map(
  64. static fn (AuthorizedGroup $group) => $group->getGroupId(),
  65. $this->authorizedGroupService->findExistingGroupsForClass($className)
  66. );
  67. natsort($groups);
  68. return [
  69. $setting->getName() ?: 'Global',
  70. $className,
  71. implode(', ', $groups),
  72. ];
  73. }, $sectionSettings));
  74. }
  75. }
  76. return 0;
  77. }
  78. /**
  79. * @param IDelegatedSettings[] $settings
  80. * @param array $innerSection
  81. */
  82. private function getDelegatedSettings(array $settings, array $innerSection): array {
  83. return $settings + array_filter($innerSection, fn (ISettings $setting) => $setting instanceof IDelegatedSettings);
  84. }
  85. }