ResetGroup.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\User_LDAP\Command;
  7. use OCA\User_LDAP\Group_Proxy;
  8. use OCA\User_LDAP\GroupPluginManager;
  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 ResetGroup extends Command {
  19. public function __construct(
  20. private IGroupManager $groupManager,
  21. private GroupPluginManager $pluginManager,
  22. private Group_Proxy $backend,
  23. ) {
  24. parent::__construct();
  25. }
  26. protected function configure(): void {
  27. $this
  28. ->setName('ldap:reset-group')
  29. ->setDescription('deletes an LDAP group independent of the group state in the LDAP')
  30. ->addArgument(
  31. 'gid',
  32. InputArgument::REQUIRED,
  33. 'the group name as used in Nextcloud'
  34. )
  35. ->addOption(
  36. 'yes',
  37. 'y',
  38. InputOption::VALUE_NONE,
  39. 'do not ask for confirmation'
  40. );
  41. }
  42. protected function execute(InputInterface $input, OutputInterface $output): int {
  43. try {
  44. $gid = $input->getArgument('gid');
  45. $group = $this->groupManager->get($gid);
  46. if (!$group instanceof IGroup) {
  47. throw new \Exception('Group not found');
  48. }
  49. $backends = $group->getBackendNames();
  50. if (!in_array('LDAP', $backends)) {
  51. throw new \Exception('The given group is not a recognized LDAP group.');
  52. }
  53. if ($input->getOption('yes') === false) {
  54. /** @var QuestionHelper $helper */
  55. $helper = $this->getHelper('question');
  56. $q = new Question('Delete all local data of this group (y|N)? ');
  57. $input->setOption('yes', $helper->ask($input, $output, $q) === 'y');
  58. }
  59. if ($input->getOption('yes') !== true) {
  60. throw new \Exception('Reset cancelled by operator');
  61. }
  62. // Disable real deletion if a plugin supports it
  63. $pluginManagerSuppressed = $this->pluginManager->setSuppressDeletion(true);
  64. // Bypass groupExists test to force mapping deletion
  65. $this->backend->getLDAPAccess($gid)->connection->writeToCache('groupExists' . $gid, false);
  66. echo "calling delete $gid\n";
  67. if ($group->delete()) {
  68. $this->pluginManager->setSuppressDeletion($pluginManagerSuppressed);
  69. return self::SUCCESS;
  70. }
  71. } catch (\Throwable $e) {
  72. if (isset($pluginManagerSuppressed)) {
  73. $this->pluginManager->setSuppressDeletion($pluginManagerSuppressed);
  74. }
  75. $output->writeln('<error>' . $e->getMessage() . '</error>');
  76. return self::FAILURE;
  77. }
  78. $output->writeln('<error>Error while resetting group</error>');
  79. return self::INVALID;
  80. }
  81. }