ResetGroup.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2021 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Côme Chilliet <come.chilliet@nextcloud.com>
  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 OCA\User_LDAP\GroupPluginManager;
  27. use OCP\IGroup;
  28. use OCP\IGroupManager;
  29. use Symfony\Component\Console\Command\Command;
  30. use Symfony\Component\Console\Helper\QuestionHelper;
  31. use Symfony\Component\Console\Input\InputArgument;
  32. use Symfony\Component\Console\Input\InputInterface;
  33. use Symfony\Component\Console\Input\InputOption;
  34. use Symfony\Component\Console\Output\OutputInterface;
  35. use Symfony\Component\Console\Question\Question;
  36. class ResetGroup extends Command {
  37. private IGroupManager $groupManager;
  38. private GroupPluginManager $pluginManager;
  39. private Group_Proxy $backend;
  40. public function __construct(
  41. IGroupManager $groupManager,
  42. GroupPluginManager $pluginManager,
  43. Group_Proxy $backend
  44. ) {
  45. $this->groupManager = $groupManager;
  46. $this->pluginManager = $pluginManager;
  47. $this->backend = $backend;
  48. parent::__construct();
  49. }
  50. protected function configure(): void {
  51. $this
  52. ->setName('ldap:reset-group')
  53. ->setDescription('deletes an LDAP group independent of the group state in the LDAP')
  54. ->addArgument(
  55. 'gid',
  56. InputArgument::REQUIRED,
  57. 'the group name as used in Nextcloud'
  58. )
  59. ->addOption(
  60. 'yes',
  61. 'y',
  62. InputOption::VALUE_NONE,
  63. 'do not ask for confirmation'
  64. );
  65. }
  66. protected function execute(InputInterface $input, OutputInterface $output): int {
  67. try {
  68. $gid = $input->getArgument('gid');
  69. $group = $this->groupManager->get($gid);
  70. if (!$group instanceof IGroup) {
  71. throw new \Exception('Group not found');
  72. }
  73. $backends = $group->getBackendNames();
  74. if (!in_array('LDAP', $backends)) {
  75. throw new \Exception('The given group is not a recognized LDAP group.');
  76. }
  77. if ($input->getOption('yes') === false) {
  78. /** @var QuestionHelper $helper */
  79. $helper = $this->getHelper('question');
  80. $q = new Question('Delete all local data of this group (y|N)? ');
  81. $input->setOption('yes', $helper->ask($input, $output, $q) === 'y');
  82. }
  83. if ($input->getOption('yes') !== true) {
  84. throw new \Exception('Reset cancelled by operator');
  85. }
  86. // Disable real deletion if a plugin supports it
  87. $pluginManagerSuppressed = $this->pluginManager->setSuppressDeletion(true);
  88. // Bypass groupExists test to force mapping deletion
  89. $this->backend->getLDAPAccess($gid)->connection->writeToCache('groupExists' . $gid, false);
  90. echo "calling delete $gid\n";
  91. if ($group->delete()) {
  92. $this->pluginManager->setSuppressDeletion($pluginManagerSuppressed);
  93. return 0;
  94. }
  95. } catch (\Throwable $e) {
  96. if (isset($pluginManagerSuppressed)) {
  97. $this->pluginManager->setSuppressDeletion($pluginManagerSuppressed);
  98. }
  99. $output->writeln('<error>' . $e->getMessage() . '</error>');
  100. return 1;
  101. }
  102. $output->writeln('<error>Error while resetting group</error>');
  103. return 2;
  104. }
  105. }