PromoteGroup.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\User_LDAP\Command;
  8. use OCA\User_LDAP\Group_Proxy;
  9. use OCP\IGroup;
  10. use OCP\IGroupManager;
  11. use Symfony\Component\Console\Command\Command;
  12. use Symfony\Component\Console\Helper\QuestionHelper;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Input\InputOption;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. use Symfony\Component\Console\Question\Question;
  18. class PromoteGroup extends Command {
  19. public function __construct(
  20. private IGroupManager $groupManager,
  21. private Group_Proxy $backend,
  22. ) {
  23. parent::__construct();
  24. }
  25. protected function configure(): void {
  26. $this
  27. ->setName('ldap:promote-group')
  28. ->setDescription('declares the specified group as admin group (only one is possible per LDAP configuration)')
  29. ->addArgument(
  30. 'group',
  31. InputArgument::REQUIRED,
  32. 'the group ID in Nextcloud or a group name'
  33. )
  34. ->addOption(
  35. 'yes',
  36. 'y',
  37. InputOption::VALUE_NONE,
  38. 'do not ask for confirmation'
  39. );
  40. }
  41. protected function formatGroupName(IGroup $group): string {
  42. $idLabel = '';
  43. if ($group->getGID() !== $group->getDisplayName()) {
  44. $idLabel = sprintf(' (Group ID: %s)', $group->getGID());
  45. }
  46. return sprintf('%s%s', $group->getDisplayName(), $idLabel);
  47. }
  48. protected function promoteGroup(IGroup $group, InputInterface $input, OutputInterface $output): void {
  49. $access = $this->backend->getLDAPAccess($group->getGID());
  50. $currentlyPromotedGroupId = $access->connection->ldapAdminGroup;
  51. if ($currentlyPromotedGroupId === $group->getGID()) {
  52. $output->writeln('<info>The specified group is already promoted</info>');
  53. return;
  54. }
  55. if ($input->getOption('yes') === false) {
  56. $currentlyPromotedGroup = $this->groupManager->get($currentlyPromotedGroupId);
  57. $demoteLabel = '';
  58. if ($currentlyPromotedGroup instanceof IGroup && $this->backend->groupExists($currentlyPromotedGroup->getGID())) {
  59. $groupNameLabel = $this->formatGroupName($currentlyPromotedGroup);
  60. $demoteLabel = sprintf('and demote %s ', $groupNameLabel);
  61. }
  62. /** @var QuestionHelper $helper */
  63. $helper = $this->getHelper('question');
  64. $q = new Question(sprintf('Promote %s to the admin group %s(y|N)? ', $this->formatGroupName($group), $demoteLabel));
  65. $input->setOption('yes', $helper->ask($input, $output, $q) === 'y');
  66. }
  67. if ($input->getOption('yes') === true) {
  68. $access->connection->setConfiguration(['ldapAdminGroup' => $group->getGID()]);
  69. $access->connection->saveConfiguration();
  70. $output->writeln(sprintf('<info>Group %s was promoted</info>', $group->getDisplayName()));
  71. } else {
  72. $output->writeln('<comment>Group promotion cancelled</comment>');
  73. }
  74. }
  75. protected function execute(InputInterface $input, OutputInterface $output): int {
  76. $groupInput = (string)$input->getArgument('group');
  77. $group = $this->groupManager->get($groupInput);
  78. if ($group instanceof IGroup && $this->backend->groupExists($group->getGID())) {
  79. $this->promoteGroup($group, $input, $output);
  80. return 0;
  81. }
  82. $groupCandidates = $this->backend->getGroups($groupInput, 20);
  83. foreach ($groupCandidates as $gidCandidate) {
  84. $group = $this->groupManager->get($gidCandidate);
  85. if ($group !== null
  86. && $this->backend->groupExists($group->getGID()) // ensure it is an LDAP group
  87. && ($group->getGID() === $groupInput
  88. || $group->getDisplayName() === $groupInput)
  89. ) {
  90. $this->promoteGroup($group, $input, $output);
  91. return 0;
  92. }
  93. }
  94. $output->writeln('<error>No matching group found</error>');
  95. return 1;
  96. }
  97. }