-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathShow.php
More file actions
161 lines (138 loc) · 4.26 KB
/
Show.php
File metadata and controls
161 lines (138 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
declare(strict_types = 1);
/**
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Settings\Command\AdminDelegation;
use OC\Core\Command\Base;
use OC\Settings\AuthorizedGroup;
use OCA\Settings\Service\AuthorizedGroupService;
use OCP\Settings\IDelegatedSettings;
use OCP\Settings\IManager;
use OCP\Settings\ISettings;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
class Show extends Base {
public function __construct(
private IManager $settingManager,
private AuthorizedGroupService $authorizedGroupService,
) {
parent::__construct();
}
protected function configure(): void {
parent::configure();
$this
->setName('admin-delegation:show')
->setDescription('show delegated settings')
;
}
protected function execute(InputInterface $input, OutputInterface $output): int {
$io = new SymfonyStyle($input, $output);
$outputFormat = $input->getOption('output');
// Validate output format
if (!$this->validateOutputFormat($outputFormat)) {
$io->error("Invalid output format: {$outputFormat}. Valid formats are: plain, json, json_pretty");
return 1;
}
// Collect delegation data
$delegationData = $this->collectDelegationData();
// Handle empty results
if (empty($delegationData)) {
if ($outputFormat === self::OUTPUT_FORMAT_PLAIN) {
$io->info('No delegated settings found.');
} else {
$this->writeArrayInOutputFormat($input, $io, []);
}
return 0;
}
// Output based on format
switch ($outputFormat) {
case self::OUTPUT_FORMAT_JSON:
case self::OUTPUT_FORMAT_JSON_PRETTY:
$this->writeArrayInOutputFormat($input, $io, $delegationData);
break;
default:
$this->outputPlainFormat($io, $delegationData);
break;
}
return 0;
}
/**
* Collect all delegation data in a structured format
*/
private function collectDelegationData(): array {
$result = [];
$sections = $this->settingManager->getAdminSections();
foreach ($sections as $sectionPriority) {
foreach ($sectionPriority as $section) {
$sectionSettings = $this->settingManager->getAdminSettings($section->getId());
$delegatedSettings = array_reduce($sectionSettings, [$this, 'getDelegatedSettings'], []);
if (empty($delegatedSettings)) {
continue;
}
$result[] = [
'id' => $section->getID(),
'name' => $section->getName() ?: $section->getID(),
'settings' => $this->formatSettingsData($delegatedSettings)
];
}
}
return $result;
}
/**
* Format settings data for consistent output
*/
private function formatSettingsData(array $settings): array {
return array_map(function (IDelegatedSettings $setting) {
$className = get_class($setting);
$groups = array_map(
static fn (AuthorizedGroup $group) => $group->getGroupId(),
$this->authorizedGroupService->findExistingGroupsForClass($className)
);
natsort($groups);
return [
'name' => $setting->getName() ?: 'Global',
'className' => $className,
'priority' => $setting->getPriority(),
'delegatedGroups' => $groups,
];
}, $settings);
}
/**
* Output data in plain table format
*/
private function outputPlainFormat(SymfonyStyle $io, array $data): void {
$io->title('Current delegations');
$headers = ['Name', 'SettingId', 'Delegated to groups'];
foreach ($data as $section) {
$io->section('Section: ' . $section['id']);
$tableData = array_map(static function (array $setting) {
return [
$setting['name'],
$setting['className'],
implode(', ', $setting['delegatedGroups']),
];
}, $section['settings']);
$io->table($headers, $tableData);
}
}
/**
* Validate the output format parameter
*/
private function validateOutputFormat(string $format): bool {
return in_array($format, [
self::OUTPUT_FORMAT_PLAIN,
self::OUTPUT_FORMAT_JSON,
self::OUTPUT_FORMAT_JSON_PRETTY
], true);
}
/**
* @param IDelegatedSettings[] $settings
* @param array $innerSection
*/
private function getDelegatedSettings(array $settings, array $innerSection): array {
return array_merge($settings, array_filter($innerSection, fn (ISettings $setting) => $setting instanceof IDelegatedSettings));
}
}