|
| 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 formatGroupName(IGroup $group): string { |
| 65 | + $idLabel = ''; |
| 66 | + if ($group->getGID() !== $group->getDisplayName()) { |
| 67 | + $idLabel = sprintf(' (Group ID: %s)', $group->getGID()); |
| 68 | + } |
| 69 | + return sprintf('%s%s', $group->getDisplayName(), $idLabel); |
| 70 | + } |
| 71 | + |
| 72 | + protected function promoteGroup(IGroup $group, InputInterface $input, OutputInterface $output): void { |
| 73 | + $access = $this->backend->getLDAPAccess($group->getGID()); |
| 74 | + $currentlyPromotedGroupId = $access->connection->ldapAdminGroup; |
| 75 | + if ($currentlyPromotedGroupId === $group->getGID()) { |
| 76 | + $output->writeln('<info>The specified group is already promoted</info>'); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + if ($input->getOption('yes') === false) { |
| 81 | + $currentlyPromotedGroup = $this->groupManager->get($currentlyPromotedGroupId); |
| 82 | + $demoteLabel = ''; |
| 83 | + if ($currentlyPromotedGroup instanceof IGroup && $this->backend->groupExists($currentlyPromotedGroup->getGID())) { |
| 84 | + $groupNameLabel = $this->formatGroupName($currentlyPromotedGroup); |
| 85 | + $demoteLabel = sprintf('and demote %s ', $groupNameLabel); |
| 86 | + } |
| 87 | + |
| 88 | + /** @var QuestionHelper $helper */ |
| 89 | + $helper = $this->getHelper('question'); |
| 90 | + $q = new Question(sprintf('Promote %s to the admin group %s(y|N)? ', $this->formatGroupName($group), $demoteLabel)); |
| 91 | + $input->setOption('yes', $helper->ask($input, $output, $q) === 'y'); |
| 92 | + } |
| 93 | + if ($input->getOption('yes') === true) { |
| 94 | + $access->connection->setConfiguration(['ldapAdminGroup' => $group->getGID()]); |
| 95 | + $access->connection->saveConfiguration(); |
| 96 | + $output->writeln(sprintf('<info>Group %s was promoted</info>', $group->getDisplayName())); |
| 97 | + } else { |
| 98 | + $output->writeln('<comment>Group promotion cancelled</comment>'); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
| 103 | + $groupInput = (string)$input->getArgument('group'); |
| 104 | + $group = $this->groupManager->get($groupInput); |
| 105 | + |
| 106 | + if ($group instanceof IGroup && $this->backend->groupExists($group->getGID())) { |
| 107 | + $this->promoteGroup($group, $input, $output); |
| 108 | + return 0; |
| 109 | + } |
| 110 | + |
| 111 | + $groupCandidates = $this->backend->getGroups($groupInput, 20); |
| 112 | + foreach ($groupCandidates as $gidCandidate) { |
| 113 | + $group = $this->groupManager->get($gidCandidate); |
| 114 | + if ($group !== null |
| 115 | + && $this->backend->groupExists($group->getGID()) // ensure it is an LDAP group |
| 116 | + && ($group->getGID() === $groupInput |
| 117 | + || $group->getDisplayName() === $groupInput) |
| 118 | + ) { |
| 119 | + $this->promoteGroup($group, $input, $output); |
| 120 | + return 0; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + $output->writeln('<error>No matching group found</error>'); |
| 125 | + return 1; |
| 126 | + } |
| 127 | + |
| 128 | +} |
0 commit comments