Skip to content

Commit 3e861e3

Browse files
committed
enh(LDAP): add occ command to promote an LDAP group to admin
Signed-off-by: Arthur Schiwon <[email protected]>
1 parent 627a50c commit 3e861e3

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed

apps/user_ldap/appinfo/info.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ A user logs into Nextcloud with their LDAP or AD credentials, and is granted acc
5151
<command>OCA\User_LDAP\Command\CheckGroup</command>
5252
<command>OCA\User_LDAP\Command\CreateEmptyConfig</command>
5353
<command>OCA\User_LDAP\Command\DeleteConfig</command>
54+
<command>OCA\User_LDAP\Command\PromoteGroup</command>
5455
<command>OCA\User_LDAP\Command\ResetGroup</command>
5556
<command>OCA\User_LDAP\Command\ResetUser</command>
5657
<command>OCA\User_LDAP\Command\Search</command>

apps/user_ldap/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
'OCA\\User_LDAP\\Command\\CheckUser' => $baseDir . '/../lib/Command/CheckUser.php',
1616
'OCA\\User_LDAP\\Command\\CreateEmptyConfig' => $baseDir . '/../lib/Command/CreateEmptyConfig.php',
1717
'OCA\\User_LDAP\\Command\\DeleteConfig' => $baseDir . '/../lib/Command/DeleteConfig.php',
18+
'OCA\\User_LDAP\\Command\\PromoteGroup' => $baseDir . '/../lib/Command/PromoteGroup.php',
1819
'OCA\\User_LDAP\\Command\\ResetGroup' => $baseDir . '/../lib/Command/ResetGroup.php',
1920
'OCA\\User_LDAP\\Command\\ResetUser' => $baseDir . '/../lib/Command/ResetUser.php',
2021
'OCA\\User_LDAP\\Command\\Search' => $baseDir . '/../lib/Command/Search.php',

apps/user_ldap/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class ComposerStaticInitUser_LDAP
3030
'OCA\\User_LDAP\\Command\\CheckUser' => __DIR__ . '/..' . '/../lib/Command/CheckUser.php',
3131
'OCA\\User_LDAP\\Command\\CreateEmptyConfig' => __DIR__ . '/..' . '/../lib/Command/CreateEmptyConfig.php',
3232
'OCA\\User_LDAP\\Command\\DeleteConfig' => __DIR__ . '/..' . '/../lib/Command/DeleteConfig.php',
33+
'OCA\\User_LDAP\\Command\\PromoteGroup' => __DIR__ . '/..' . '/../lib/Command/PromoteGroup.php',
3334
'OCA\\User_LDAP\\Command\\ResetGroup' => __DIR__ . '/..' . '/../lib/Command/ResetGroup.php',
3435
'OCA\\User_LDAP\\Command\\ResetUser' => __DIR__ . '/..' . '/../lib/Command/ResetUser.php',
3536
'OCA\\User_LDAP\\Command\\Search' => __DIR__ . '/..' . '/../lib/Command/Search.php',
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* @copyright Copyright (c) 2023 Arthur Schiwon <[email protected]>
6+
*
7+
* @author Arthur Schiwon <[email protected]>
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\User_LDAP\Command;
26+
27+
use OCA\User_LDAP\Group_Proxy;
28+
use OCP\IGroup;
29+
use OCP\IGroupManager;
30+
use Symfony\Component\Console\Command\Command;
31+
use Symfony\Component\Console\Helper\QuestionHelper;
32+
use Symfony\Component\Console\Input\InputArgument;
33+
use Symfony\Component\Console\Input\InputInterface;
34+
use Symfony\Component\Console\Input\InputOption;
35+
use Symfony\Component\Console\Output\OutputInterface;
36+
use Symfony\Component\Console\Question\Question;
37+
38+
class PromoteGroup extends Command {
39+
40+
public function __construct(
41+
private IGroupManager $groupManager,
42+
private Group_Proxy $backend
43+
) {
44+
parent::__construct();
45+
}
46+
47+
protected function configure(): void {
48+
$this
49+
->setName('ldap:promote-group')
50+
->setDescription('declares the specified group as admin group (only one is possible per LDAP configuration)')
51+
->addArgument(
52+
'group',
53+
InputArgument::REQUIRED,
54+
'the group ID in Nextcloud or a group name'
55+
)
56+
->addOption(
57+
'yes',
58+
'y',
59+
InputOption::VALUE_NONE,
60+
'do not ask for confirmation'
61+
);
62+
}
63+
64+
protected function promoteGroup(IGroup $group, InputInterface $input, OutputInterface $output): void {
65+
if ($input->getOption('yes') === false) {
66+
/** @var QuestionHelper $helper */
67+
$helper = $this->getHelper('question');
68+
69+
$idLabel = '';
70+
if ($group->getGID() !== $group->getDisplayName()) {
71+
$idLabel = sprintf(' (Group ID: %s)', $group->getGID());
72+
}
73+
74+
$q = new Question(sprintf('Promote %s%s to the admin group (y|N)? ', $group->getDisplayName(), $idLabel));
75+
$input->setOption('yes', $helper->ask($input, $output, $q) === 'y');
76+
}
77+
if ($input->getOption('yes') === true) {
78+
$access = $this->backend->getLDAPAccess($group->getGID());
79+
$access->connection->setConfiguration(['ldapAdminGroup' => $group->getGID()]);
80+
$access->connection->saveConfiguration();
81+
$output->writeln(sprintf('<info>Group %s was promoted</info>', $group->getDisplayName()));
82+
} else {
83+
$output->writeln('<comment>Group promotion cancelled</comment>');
84+
}
85+
}
86+
87+
protected function execute(InputInterface $input, OutputInterface $output): int {
88+
$groupInput = (string)$input->getArgument('group');
89+
$group = $this->groupManager->get($groupInput);
90+
91+
if ($group instanceof IGroup && $this->backend->groupExists($group->getGID())) {
92+
$this->promoteGroup($group, $input, $output);
93+
return 0;
94+
}
95+
96+
$groupCandidates = $this->backend->getGroups($groupInput, 20);
97+
foreach ($groupCandidates as $gidCandidate) {
98+
$group = $this->groupManager->get($gidCandidate);
99+
if ($group !== null
100+
&& $this->backend->groupExists($group->getGID()) // ensure it is an LDAP group
101+
&& ($group->getGID() === $groupInput
102+
|| $group->getDisplayName() === $groupInput)
103+
) {
104+
$this->promoteGroup($group, $input, $output);
105+
return 0;
106+
}
107+
}
108+
109+
$output->writeln('<error>No matching group found</error>');
110+
return 1;
111+
}
112+
113+
}

0 commit comments

Comments
 (0)