1
0

PromoteGroup.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Arthur Schiwon <blizzz@arthur-schiwon.de>
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\User_LDAP\Command;
  25. use OCA\User_LDAP\Group_Proxy;
  26. use OCP\IGroup;
  27. use OCP\IGroupManager;
  28. use Symfony\Component\Console\Command\Command;
  29. use Symfony\Component\Console\Helper\QuestionHelper;
  30. use Symfony\Component\Console\Input\InputArgument;
  31. use Symfony\Component\Console\Input\InputInterface;
  32. use Symfony\Component\Console\Input\InputOption;
  33. use Symfony\Component\Console\Output\OutputInterface;
  34. use Symfony\Component\Console\Question\Question;
  35. class PromoteGroup extends Command {
  36. public function __construct(
  37. private IGroupManager $groupManager,
  38. private Group_Proxy $backend
  39. ) {
  40. parent::__construct();
  41. }
  42. protected function configure(): void {
  43. $this
  44. ->setName('ldap:promote-group')
  45. ->setDescription('declares the specified group as admin group (only one is possible per LDAP configuration)')
  46. ->addArgument(
  47. 'group',
  48. InputArgument::REQUIRED,
  49. 'the group ID in Nextcloud or a group name'
  50. )
  51. ->addOption(
  52. 'yes',
  53. 'y',
  54. InputOption::VALUE_NONE,
  55. 'do not ask for confirmation'
  56. );
  57. }
  58. protected function formatGroupName(IGroup $group): string {
  59. $idLabel = '';
  60. if ($group->getGID() !== $group->getDisplayName()) {
  61. $idLabel = sprintf(' (Group ID: %s)', $group->getGID());
  62. }
  63. return sprintf('%s%s', $group->getDisplayName(), $idLabel);
  64. }
  65. protected function promoteGroup(IGroup $group, InputInterface $input, OutputInterface $output): void {
  66. $access = $this->backend->getLDAPAccess($group->getGID());
  67. $currentlyPromotedGroupId = $access->connection->ldapAdminGroup;
  68. if ($currentlyPromotedGroupId === $group->getGID()) {
  69. $output->writeln('<info>The specified group is already promoted</info>');
  70. return;
  71. }
  72. if ($input->getOption('yes') === false) {
  73. $currentlyPromotedGroup = $this->groupManager->get($currentlyPromotedGroupId);
  74. $demoteLabel = '';
  75. if ($currentlyPromotedGroup instanceof IGroup && $this->backend->groupExists($currentlyPromotedGroup->getGID())) {
  76. $groupNameLabel = $this->formatGroupName($currentlyPromotedGroup);
  77. $demoteLabel = sprintf('and demote %s ', $groupNameLabel);
  78. }
  79. /** @var QuestionHelper $helper */
  80. $helper = $this->getHelper('question');
  81. $q = new Question(sprintf('Promote %s to the admin group %s(y|N)? ', $this->formatGroupName($group), $demoteLabel));
  82. $input->setOption('yes', $helper->ask($input, $output, $q) === 'y');
  83. }
  84. if ($input->getOption('yes') === true) {
  85. $access->connection->setConfiguration(['ldapAdminGroup' => $group->getGID()]);
  86. $access->connection->saveConfiguration();
  87. $output->writeln(sprintf('<info>Group %s was promoted</info>', $group->getDisplayName()));
  88. } else {
  89. $output->writeln('<comment>Group promotion cancelled</comment>');
  90. }
  91. }
  92. protected function execute(InputInterface $input, OutputInterface $output): int {
  93. $groupInput = (string)$input->getArgument('group');
  94. $group = $this->groupManager->get($groupInput);
  95. if ($group instanceof IGroup && $this->backend->groupExists($group->getGID())) {
  96. $this->promoteGroup($group, $input, $output);
  97. return 0;
  98. }
  99. $groupCandidates = $this->backend->getGroups($groupInput, 20);
  100. foreach ($groupCandidates as $gidCandidate) {
  101. $group = $this->groupManager->get($gidCandidate);
  102. if ($group !== null
  103. && $this->backend->groupExists($group->getGID()) // ensure it is an LDAP group
  104. && ($group->getGID() === $groupInput
  105. || $group->getDisplayName() === $groupInput)
  106. ) {
  107. $this->promoteGroup($group, $input, $output);
  108. return 0;
  109. }
  110. }
  111. $output->writeln('<error>No matching group found</error>');
  112. return 1;
  113. }
  114. }