Show.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. declare(strict_types = 1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Settings\Command\AdminDelegation;
  8. use OC\Core\Command\Base;
  9. use OC\Settings\AuthorizedGroup;
  10. use OCA\Settings\Service\AuthorizedGroupService;
  11. use OCP\Settings\IDelegatedSettings;
  12. use OCP\Settings\IManager;
  13. use OCP\Settings\ISettings;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. use Symfony\Component\Console\Style\SymfonyStyle;
  17. class Show extends Base {
  18. public function __construct(
  19. private IManager $settingManager,
  20. private AuthorizedGroupService $authorizedGroupService,
  21. ) {
  22. parent::__construct();
  23. }
  24. protected function configure(): void {
  25. $this
  26. ->setName('admin-delegation:show')
  27. ->setDescription('show delegated settings')
  28. ;
  29. }
  30. protected function execute(InputInterface $input, OutputInterface $output): int {
  31. $io = new SymfonyStyle($input, $output);
  32. $io->title('Current delegations');
  33. $sections = $this->settingManager->getAdminSections();
  34. $settings = [];
  35. $headers = ['Name', 'SettingId', 'Delegated to groups'];
  36. foreach ($sections as $sectionPriority) {
  37. foreach ($sectionPriority as $section) {
  38. $sectionSettings = $this->settingManager->getAdminSettings($section->getId());
  39. $sectionSettings = array_reduce($sectionSettings, [$this, 'getDelegatedSettings'], []);
  40. if (empty($sectionSettings)) {
  41. continue;
  42. }
  43. $io->section('Section: '.$section->getID());
  44. $io->table($headers, array_map(function (IDelegatedSettings $setting) use ($section) {
  45. $className = get_class($setting);
  46. $groups = array_map(
  47. static fn (AuthorizedGroup $group) => $group->getGroupId(),
  48. $this->authorizedGroupService->findExistingGroupsForClass($className)
  49. );
  50. natsort($groups);
  51. return [
  52. $setting->getName() ?: 'Global',
  53. $className,
  54. implode(', ', $groups),
  55. ];
  56. }, $sectionSettings));
  57. }
  58. }
  59. return 0;
  60. }
  61. /**
  62. * @param IDelegatedSettings[] $settings
  63. * @param array $innerSection
  64. */
  65. private function getDelegatedSettings(array $settings, array $innerSection): array {
  66. return $settings + array_filter($innerSection, fn (ISettings $setting) => $setting instanceof IDelegatedSettings);
  67. }
  68. }