Skip to content

Commit 10ddab5

Browse files
Merge pull request nextcloud#55429 from nextcloud/backport/55260/stable31
[stable31] admin-delegation occ - add output option for show command to support JSON formats Signed-off-by: Misha M.-Kupriyanov <kupriyanov@strato.de>
1 parent ff77104 commit 10ddab5

1 file changed

Lines changed: 103 additions & 20 deletions

File tree

  • apps/settings/lib/Command/AdminDelegation

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

Lines changed: 103 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public function __construct(
2626
}
2727

2828
protected function configure(): void {
29+
parent::configure();
2930
$this
3031
->setName('admin-delegation:show')
3132
->setDescription('show delegated settings')
@@ -34,37 +35,119 @@ protected function configure(): void {
3435

3536
protected function execute(InputInterface $input, OutputInterface $output): int {
3637
$io = new SymfonyStyle($input, $output);
37-
$io->title('Current delegations');
38+
$outputFormat = $input->getOption('output');
39+
40+
// Validate output format
41+
if (!$this->validateOutputFormat($outputFormat)) {
42+
$io->error("Invalid output format: {$outputFormat}. Valid formats are: plain, json, json_pretty");
43+
return 1;
44+
}
45+
46+
// Collect delegation data
47+
$delegationData = $this->collectDelegationData();
48+
49+
// Handle empty results
50+
if (empty($delegationData)) {
51+
if ($outputFormat === self::OUTPUT_FORMAT_PLAIN) {
52+
$io->info('No delegated settings found.');
53+
} else {
54+
$this->writeArrayInOutputFormat($input, $io, []);
55+
}
56+
return 0;
57+
}
3858

59+
// Output based on format
60+
switch ($outputFormat) {
61+
case self::OUTPUT_FORMAT_JSON:
62+
case self::OUTPUT_FORMAT_JSON_PRETTY:
63+
$this->writeArrayInOutputFormat($input, $io, $delegationData);
64+
break;
65+
default:
66+
$this->outputPlainFormat($io, $delegationData);
67+
break;
68+
}
69+
70+
return 0;
71+
}
72+
73+
/**
74+
* Collect all delegation data in a structured format
75+
*/
76+
private function collectDelegationData(): array {
77+
$result = [];
3978
$sections = $this->settingManager->getAdminSections();
40-
$settings = [];
41-
$headers = ['Name', 'SettingId', 'Delegated to groups'];
79+
4280
foreach ($sections as $sectionPriority) {
4381
foreach ($sectionPriority as $section) {
4482
$sectionSettings = $this->settingManager->getAdminSettings($section->getId());
45-
$sectionSettings = array_reduce($sectionSettings, [$this, 'getDelegatedSettings'], []);
46-
if (empty($sectionSettings)) {
83+
$delegatedSettings = array_reduce($sectionSettings, [$this, 'getDelegatedSettings'], []);
84+
85+
if (empty($delegatedSettings)) {
4786
continue;
4887
}
4988

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));
89+
$result[] = [
90+
'id' => $section->getID(),
91+
'name' => $section->getName() ?: $section->getID(),
92+
'settings' => $this->formatSettingsData($delegatedSettings)
93+
];
6494
}
6595
}
6696

67-
return 0;
97+
return $result;
98+
}
99+
100+
/**
101+
* Format settings data for consistent output
102+
*/
103+
private function formatSettingsData(array $settings): array {
104+
return array_map(function (IDelegatedSettings $setting) {
105+
$className = get_class($setting);
106+
$groups = array_map(
107+
static fn (AuthorizedGroup $group) => $group->getGroupId(),
108+
$this->authorizedGroupService->findExistingGroupsForClass($className)
109+
);
110+
natsort($groups);
111+
112+
return [
113+
'name' => $setting->getName() ?: 'Global',
114+
'className' => $className,
115+
'delegatedGroups' => $groups,
116+
];
117+
}, $settings);
118+
}
119+
120+
/**
121+
* Output data in plain table format
122+
*/
123+
private function outputPlainFormat(SymfonyStyle $io, array $data): void {
124+
$io->title('Current delegations');
125+
$headers = ['Name', 'SettingId', 'Delegated to groups'];
126+
127+
foreach ($data as $section) {
128+
$io->section('Section: ' . $section['id']);
129+
130+
$tableData = array_map(static function (array $setting) {
131+
return [
132+
$setting['name'],
133+
$setting['className'],
134+
implode(', ', $setting['delegatedGroups']),
135+
];
136+
}, $section['settings']);
137+
138+
$io->table($headers, $tableData);
139+
}
140+
}
141+
142+
/**
143+
* Validate the output format parameter
144+
*/
145+
private function validateOutputFormat(string $format): bool {
146+
return in_array($format, [
147+
self::OUTPUT_FORMAT_PLAIN,
148+
self::OUTPUT_FORMAT_JSON,
149+
self::OUTPUT_FORMAT_JSON_PRETTY
150+
], true);
68151
}
69152

70153
/**

0 commit comments

Comments
 (0)