Skip to content

Commit 7681fa2

Browse files
committed
feat(settings): add occ commands to handle admin delegation
Signed-off-by: Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
1 parent 056401b commit 7681fa2

File tree

6 files changed

+255
-1
lines changed

6 files changed

+255
-1
lines changed

apps/settings/appinfo/info.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<name>Nextcloud settings</name>
66
<summary>Nextcloud settings</summary>
77
<description>Nextcloud settings</description>
8-
<version>1.10.0</version>
8+
<version>1.10.1</version>
99
<licence>agpl</licence>
1010
<author>Nextcloud</author>
1111
<namespace>Settings</namespace>
@@ -16,6 +16,12 @@
1616
<nextcloud min-version="28" max-version="28"/>
1717
</dependencies>
1818

19+
<commands>
20+
<command>OCA\Settings\Command\AdminDelegation\Show</command>
21+
<command>OCA\Settings\Command\AdminDelegation\Add</command>
22+
<command>OCA\Settings\Command\AdminDelegation\Remove</command>
23+
</commands>
24+
1925
<settings>
2026
<admin>OCA\Settings\Settings\Admin\Mail</admin>
2127
<admin>OCA\Settings\Settings\Admin\Overview</admin>

apps/settings/composer/composer/autoload_classmap.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
'OCA\\Settings\\Activity\\Setting' => $baseDir . '/../lib/Activity/Setting.php',
1717
'OCA\\Settings\\AppInfo\\Application' => $baseDir . '/../lib/AppInfo/Application.php',
1818
'OCA\\Settings\\BackgroundJobs\\VerifyUserData' => $baseDir . '/../lib/BackgroundJobs/VerifyUserData.php',
19+
'OCA\\Settings\\Command\\AdminDelegation\\Add' => $baseDir . '/../lib/Command/AdminDelegation/Add.php',
20+
'OCA\\Settings\\Command\\AdminDelegation\\Remove' => $baseDir . '/../lib/Command/AdminDelegation/Remove.php',
21+
'OCA\\Settings\\Command\\AdminDelegation\\Show' => $baseDir . '/../lib/Command/AdminDelegation/Show.php',
1922
'OCA\\Settings\\Controller\\AISettingsController' => $baseDir . '/../lib/Controller/AISettingsController.php',
2023
'OCA\\Settings\\Controller\\AdminSettingsController' => $baseDir . '/../lib/Controller/AdminSettingsController.php',
2124
'OCA\\Settings\\Controller\\AppSettingsController' => $baseDir . '/../lib/Controller/AppSettingsController.php',

apps/settings/composer/composer/autoload_static.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ class ComposerStaticInitSettings
3131
'OCA\\Settings\\Activity\\Setting' => __DIR__ . '/..' . '/../lib/Activity/Setting.php',
3232
'OCA\\Settings\\AppInfo\\Application' => __DIR__ . '/..' . '/../lib/AppInfo/Application.php',
3333
'OCA\\Settings\\BackgroundJobs\\VerifyUserData' => __DIR__ . '/..' . '/../lib/BackgroundJobs/VerifyUserData.php',
34+
'OCA\\Settings\\Command\\AdminDelegation\\Add' => __DIR__ . '/..' . '/../lib/Command/AdminDelegation/Add.php',
35+
'OCA\\Settings\\Command\\AdminDelegation\\Remove' => __DIR__ . '/..' . '/../lib/Command/AdminDelegation/Remove.php',
36+
'OCA\\Settings\\Command\\AdminDelegation\\Show' => __DIR__ . '/..' . '/../lib/Command/AdminDelegation/Show.php',
3437
'OCA\\Settings\\Controller\\AISettingsController' => __DIR__ . '/..' . '/../lib/Controller/AISettingsController.php',
3538
'OCA\\Settings\\Controller\\AdminSettingsController' => __DIR__ . '/..' . '/../lib/Controller/AdminSettingsController.php',
3639
'OCA\\Settings\\Controller\\AppSettingsController' => __DIR__ . '/..' . '/../lib/Controller/AppSettingsController.php',
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
/**
5+
* @copyright Copyright (c) 2023 Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
6+
*
7+
* @author Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
8+
*
9+
* @license GNU AGPL version 3 or any later version
10+
*
11+
* This program is free software: you can redistribute it and/or modify
12+
* it under the terms of the GNU Affero General Public License as
13+
* published by the Free Software Foundation, either version 3 of the
14+
* License, or (at your option) any later version.
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Affero General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Affero General Public License
22+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23+
*
24+
*/
25+
namespace OCA\Settings\Command\AdminDelegation;
26+
27+
use OC\Core\Command\Base;
28+
use OCA\Settings\Service\AuthorizedGroupService;
29+
use OCP\IGroupManager;
30+
use OCP\Settings\IDelegatedSettings;
31+
use OCP\Settings\IManager;
32+
use Symfony\Component\Console\Input\InputArgument;
33+
use Symfony\Component\Console\Input\InputInterface;
34+
use Symfony\Component\Console\Output\OutputInterface;
35+
use Symfony\Component\Console\Style\SymfonyStyle;
36+
37+
class Add extends Base {
38+
public function __construct(
39+
private IManager $settingManager,
40+
private AuthorizedGroupService $authorizedGroupService,
41+
private IGroupManager $groupManager,
42+
) {
43+
parent::__construct();
44+
}
45+
46+
protected function configure(): void {
47+
$this
48+
->setName('admin-delegation:add')
49+
->setDescription('add setting delegation to a group')
50+
->addArgument('settingClass', InputArgument::REQUIRED, 'Admin setting class')
51+
->addArgument('groupId', InputArgument::REQUIRED, 'Delegate to group ID')
52+
->addUsage('\'OCA\Settings\Settings\Admin\Server\' mygroup')
53+
;
54+
}
55+
56+
protected function execute(InputInterface $input, OutputInterface $output): int {
57+
$io = new SymfonyStyle($input, $output);
58+
$settingClass = $input->getArgument('settingClass');
59+
if (!in_array(IDelegatedSettings::class, (array) class_implements($settingClass), true)) {
60+
$io->error('The specified class isn’t a valid delegated setting.');
61+
return 2;
62+
}
63+
64+
$groupId = $input->getArgument('groupId');
65+
if (!$this->groupManager->groupExists($groupId)) {
66+
$io->error('The specified group didn’t exist.');
67+
return 3;
68+
}
69+
70+
$this->authorizedGroupService->create($groupId, $settingClass);
71+
72+
$io->success('Administration of '.$settingClass.' delegated to '.$groupId.'.');
73+
74+
return 0;
75+
}
76+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
/**
5+
* @copyright Copyright (c) 2023 Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
6+
*
7+
* @author Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
8+
*
9+
* @license GNU AGPL version 3 or any later version
10+
*
11+
* This program is free software: you can redistribute it and/or modify
12+
* it under the terms of the GNU Affero General Public License as
13+
* published by the Free Software Foundation, either version 3 of the
14+
* License, or (at your option) any later version.
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Affero General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Affero General Public License
22+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23+
*
24+
*/
25+
namespace OCA\Settings\Command\AdminDelegation;
26+
27+
use OC\Core\Command\Base;
28+
use OCA\Settings\Service\AuthorizedGroupService;
29+
use OCP\IGroupManager;
30+
use OCP\Settings\IManager;
31+
use Symfony\Component\Console\Input\InputArgument;
32+
use Symfony\Component\Console\Input\InputInterface;
33+
use Symfony\Component\Console\Output\OutputInterface;
34+
use Symfony\Component\Console\Style\SymfonyStyle;
35+
36+
class Remove extends Base {
37+
public function __construct(
38+
private IManager $settingManager,
39+
private AuthorizedGroupService $authorizedGroupService,
40+
private IGroupManager $groupManager,
41+
) {
42+
parent::__construct();
43+
}
44+
45+
protected function configure(): void {
46+
$this
47+
->setName('admin-delegation:remove')
48+
->setDescription('remove settings delegation from a group')
49+
->addArgument('settingClass', InputArgument::REQUIRED, 'Admin setting class')
50+
->addArgument('groupId', InputArgument::REQUIRED, 'Group ID to remove')
51+
->addUsage('\'OCA\Settings\Settings\Admin\Server\' mygroup')
52+
;
53+
}
54+
55+
protected function execute(InputInterface $input, OutputInterface $output): int {
56+
$io = new SymfonyStyle($input, $output);
57+
$settingClass = $input->getArgument('settingClass');
58+
$groups = $this->authorizedGroupService->findExistingGroupsForClass($settingClass);
59+
$groupId = $input->getArgument('groupId');
60+
foreach ($groups as $group) {
61+
if ($group->getGroupId() === $groupId) {
62+
$this->authorizedGroupService->delete($group->getId());
63+
$io->success('Removed delegation of '.$settingClass.' to '.$groupId.'.');
64+
return 0;
65+
}
66+
}
67+
68+
$io->success('Group '.$groupId.' didn’t have delegation for '.$settingClass.'.');
69+
70+
return 0;
71+
}
72+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
/**
5+
* @copyright Copyright (c) 2023 Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
6+
*
7+
* @author Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
8+
*
9+
* @license GNU AGPL version 3 or any later version
10+
*
11+
* This program is free software: you can redistribute it and/or modify
12+
* it under the terms of the GNU Affero General Public License as
13+
* published by the Free Software Foundation, either version 3 of the
14+
* License, or (at your option) any later version.
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Affero General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Affero General Public License
22+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23+
*
24+
*/
25+
namespace OCA\Settings\Command\AdminDelegation;
26+
27+
use OC\Core\Command\Base;
28+
use OC\Settings\AuthorizedGroup;
29+
use OCA\Settings\Service\AuthorizedGroupService;
30+
use OCP\Settings\IDelegatedSettings;
31+
use OCP\Settings\IManager;
32+
use OCP\Settings\ISettings;
33+
use Symfony\Component\Console\Input\InputInterface;
34+
use Symfony\Component\Console\Output\OutputInterface;
35+
use Symfony\Component\Console\Style\SymfonyStyle;
36+
37+
class Show extends Base {
38+
public function __construct(
39+
private IManager $settingManager,
40+
private AuthorizedGroupService $authorizedGroupService,
41+
) {
42+
parent::__construct();
43+
}
44+
45+
protected function configure(): void {
46+
$this
47+
->setName('admin-delegation:show')
48+
->setDescription('show delegated settings')
49+
;
50+
}
51+
52+
protected function execute(InputInterface $input, OutputInterface $output): int {
53+
$io = new SymfonyStyle($input, $output);
54+
$io->title('Current delegations');
55+
56+
$sections = $this->settingManager->getAdminSections();
57+
$settings = [];
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+
}
83+
84+
return 0;
85+
}
86+
87+
/**
88+
* @param IDelegatedSettings[] $settings
89+
* @param array $innerSection
90+
*/
91+
private function getDelegatedSettings(array $settings, array $innerSection): array {
92+
return $settings + array_filter($innerSection, fn (ISettings $setting) => $setting instanceof IDelegatedSettings);
93+
}
94+
}

0 commit comments

Comments
 (0)