Skip to content

Commit 25ab3d4

Browse files
committed
feat(occ): add json output option to admin-delegation:show
Signed-off-by: Kent Delante <[email protected]>
1 parent e1c8c20 commit 25ab3d4

File tree

1 file changed

+80
-26
lines changed
  • apps/settings/lib/Command/AdminDelegation

1 file changed

+80
-26
lines changed

apps/settings/lib/Command/AdminDelegation/Show.php

+80-26
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use OCP\Settings\IManager;
1515
use OCP\Settings\ISettings;
1616
use Symfony\Component\Console\Input\InputInterface;
17+
use Symfony\Component\Console\Input\InputOption;
1718
use Symfony\Component\Console\Output\OutputInterface;
1819
use Symfony\Component\Console\Style\SymfonyStyle;
1920

@@ -29,39 +30,56 @@ protected function configure(): void {
2930
$this
3031
->setName('admin-delegation:show')
3132
->setDescription('show delegated settings')
33+
->addOption(
34+
'output',
35+
null,
36+
InputOption::VALUE_OPTIONAL,
37+
'Output format (plain, json or json_pretty, default is plain)',
38+
$this->defaultOutputFormat
39+
)
3240
;
3341
}
3442

3543
protected function execute(InputInterface $input, OutputInterface $output): int {
36-
$io = new SymfonyStyle($input, $output);
37-
$io->title('Current delegations');
38-
3944
$sections = $this->settingManager->getAdminSections();
45+
4046
$settings = [];
41-
$headers = ['Name', 'SettingId', 'Delegated to groups'];
42-
foreach ($sections as $sectionPriority) {
43-
foreach ($sectionPriority as $section) {
44-
$sectionSettings = $this->settingManager->getAdminSettings($section->getId());
45-
$sectionSettings = array_reduce($sectionSettings, [$this, 'getDelegatedSettings'], []);
46-
if (empty($sectionSettings)) {
47-
continue;
48-
}
47+
switch ($input->getOption('output')) {
48+
case self::OUTPUT_FORMAT_JSON:
49+
$output->writeln(json_encode($this->buildJsonOutput($sections)));
50+
break;
51+
case self::OUTPUT_FORMAT_JSON_PRETTY:
52+
$output->writeln(json_encode($this->buildJsonOutput($sections), JSON_PRETTY_PRINT));
53+
break;
54+
default:
55+
$io = new SymfonyStyle($input, $output);
56+
$io->title('Current delegations');
4957

50-
$io->section('Section: ' . $section->getID());
51-
$io->table($headers, array_map(function (IDelegatedSettings $setting) use ($section) {
52-
$className = get_class($setting);
53-
$groups = array_map(
54-
static fn (AuthorizedGroup $group) => $group->getGroupId(),
55-
$this->authorizedGroupService->findExistingGroupsForClass($className)
56-
);
57-
natsort($groups);
58-
return [
59-
$setting->getName() ?: 'Global',
60-
$className,
61-
implode(', ', $groups),
62-
];
63-
}, $sectionSettings));
64-
}
58+
$headers = ['Name', 'SettingId', 'Delegated to groups'];
59+
foreach ($sections as $sectionPriority) {
60+
foreach ($sectionPriority as $section) {
61+
$sectionSettings = $this->settingManager->getAdminSettings($section->getId());
62+
$sectionSettings = array_reduce($sectionSettings, [$this, 'getDelegatedSettings'], []);
63+
if (empty($sectionSettings)) {
64+
continue;
65+
}
66+
67+
$io->section('Section: ' . $section->getID());
68+
$io->table($headers, array_map(function (IDelegatedSettings $setting) use ($section) {
69+
$className = get_class($setting);
70+
$groups = array_map(
71+
static fn (AuthorizedGroup $group) => $group->getGroupId(),
72+
$this->authorizedGroupService->findExistingGroupsForClass($className)
73+
);
74+
natsort($groups);
75+
return [
76+
$setting->getName() ?: 'Global',
77+
$className,
78+
implode(', ', $groups),
79+
];
80+
}, $sectionSettings));
81+
}
82+
}
6583
}
6684

6785
return 0;
@@ -74,4 +92,40 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7492
private function getDelegatedSettings(array $settings, array $innerSection): array {
7593
return $settings + array_filter($innerSection, fn (ISettings $setting) => $setting instanceof IDelegatedSettings);
7694
}
95+
96+
protected function buildJsonOutput(array $sections): array {
97+
$currentDelegations = [
98+
'current_delegations' => []
99+
];
100+
101+
foreach ($sections as $sectionPriority) {
102+
foreach ($sectionPriority as $section) {
103+
$sectionSettings = $this->settingManager->getAdminSettings($section->getId());
104+
$sectionSettings = array_reduce($sectionSettings, [$this, 'getDelegatedSettings'], []);
105+
if (empty($sectionSettings)) {
106+
continue;
107+
}
108+
109+
$currentDelegations['current_delegations'][] = [
110+
'section' => $section->getID(),
111+
'delegations' =>
112+
array_map(function (IDelegatedSettings $setting) use ($section, $headers) {
113+
$className = get_class($setting);
114+
$groups = array_map(
115+
static fn (AuthorizedGroup $group) => $group->getGroupId(),
116+
$this->authorizedGroupService->findExistingGroupsForClass($className)
117+
);
118+
natsort($groups);
119+
return [
120+
"name" => $setting->getName() ?: 'Global',
121+
"settingId" => $className,
122+
"delegatedToGroups" => implode(', ', $groups)
123+
];
124+
}, $sectionSettings)
125+
];
126+
}
127+
}
128+
129+
return $currentDelegations;
130+
}
77131
}

0 commit comments

Comments
 (0)