Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(occ): add json output option to admin-delegation:show #51124

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 80 additions & 26 deletions apps/settings/lib/Command/AdminDelegation/Show.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use OCP\Settings\IManager;
use OCP\Settings\ISettings;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

Expand All @@ -29,39 +30,56 @@ protected function configure(): void {
$this
->setName('admin-delegation:show')
->setDescription('show delegated settings')
->addOption(
'output',
null,
InputOption::VALUE_OPTIONAL,
'Output format (plain, json or json_pretty, default is plain)',
$this->defaultOutputFormat
)
;
}

protected function execute(InputInterface $input, OutputInterface $output): int {
$io = new SymfonyStyle($input, $output);
$io->title('Current delegations');

$sections = $this->settingManager->getAdminSections();

$settings = [];
$headers = ['Name', 'SettingId', 'Delegated to groups'];
foreach ($sections as $sectionPriority) {
foreach ($sectionPriority as $section) {
$sectionSettings = $this->settingManager->getAdminSettings($section->getId());
$sectionSettings = array_reduce($sectionSettings, [$this, 'getDelegatedSettings'], []);
if (empty($sectionSettings)) {
continue;
}
switch ($input->getOption('output')) {
case self::OUTPUT_FORMAT_JSON:
$output->writeln(json_encode($this->buildJsonOutput($sections)));
break;
case self::OUTPUT_FORMAT_JSON_PRETTY:
$output->writeln(json_encode($this->buildJsonOutput($sections), JSON_PRETTY_PRINT));
break;
default:
$io = new SymfonyStyle($input, $output);
$io->title('Current delegations');

$io->section('Section: ' . $section->getID());
$io->table($headers, array_map(function (IDelegatedSettings $setting) use ($section) {
$className = get_class($setting);
$groups = array_map(
static fn (AuthorizedGroup $group) => $group->getGroupId(),
$this->authorizedGroupService->findExistingGroupsForClass($className)
);
natsort($groups);
return [
$setting->getName() ?: 'Global',
$className,
implode(', ', $groups),
];
}, $sectionSettings));
}
$headers = ['Name', 'SettingId', 'Delegated to groups'];
foreach ($sections as $sectionPriority) {
foreach ($sectionPriority as $section) {
$sectionSettings = $this->settingManager->getAdminSettings($section->getId());
$sectionSettings = array_reduce($sectionSettings, [$this, 'getDelegatedSettings'], []);
if (empty($sectionSettings)) {
continue;
}

$io->section('Section: ' . $section->getID());
$io->table($headers, array_map(function (IDelegatedSettings $setting) use ($section) {
$className = get_class($setting);
$groups = array_map(
static fn (AuthorizedGroup $group) => $group->getGroupId(),
$this->authorizedGroupService->findExistingGroupsForClass($className)
);
natsort($groups);
return [
$setting->getName() ?: 'Global',
$className,
implode(', ', $groups),
];
}, $sectionSettings));
}
}
}

return 0;
Expand All @@ -74,4 +92,40 @@ protected function execute(InputInterface $input, OutputInterface $output): int
private function getDelegatedSettings(array $settings, array $innerSection): array {
return $settings + array_filter($innerSection, fn (ISettings $setting) => $setting instanceof IDelegatedSettings);
}

private function buildJsonOutput(array $sections): array {
$currentDelegations = [
'currentDelegations' => []
];

foreach ($sections as $sectionPriority) {
foreach ($sectionPriority as $section) {
$sectionSettings = $this->settingManager->getAdminSettings($section->getId());
$sectionSettings = array_reduce($sectionSettings, [$this, 'getDelegatedSettings'], []);
if (empty($sectionSettings)) {
continue;
}

$currentDelegations['currentDelegations'][] = [
'section' => $section->getID(),
'delegations' =>
array_map(function (IDelegatedSettings $setting) use ($section, $headers) {
$className = get_class($setting);
$groups = array_map(
static fn (AuthorizedGroup $group) => $group->getGroupId(),
$this->authorizedGroupService->findExistingGroupsForClass($className)
);
natsort($groups);
return [
"name" => $setting->getName() ?: 'Global',
"settingId" => $className,
"delegatedToGroups" => implode(', ', $groups)
];
}, $sectionSettings)
];
}
}

return $currentDelegations;
}
}