1
0

ResetGroup.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. public function __construct(
  38. private IGroupManager $groupManager,
  39. private GroupPluginManager $pluginManager,
  40. private Group_Proxy $backend,
  41. ) {
  42. parent::__construct();
  43. }
  44. protected function configure(): void {
  45. $this
  46. ->setName('ldap:reset-group')
  47. ->setDescription('deletes an LDAP group independent of the group state in the LDAP')
  48. ->addArgument(
  49. 'gid',
  50. InputArgument::REQUIRED,
  51. 'the group name as used in Nextcloud'
  52. )
  53. ->addOption(
  54. 'yes',
  55. 'y',
  56. InputOption::VALUE_NONE,
  57. 'do not ask for confirmation'
  58. );
  59. }
  60. protected function execute(InputInterface $input, OutputInterface $output): int {
  61. try {
  62. $gid = $input->getArgument('gid');
  63. $group = $this->groupManager->get($gid);
  64. if (!$group instanceof IGroup) {
  65. throw new \Exception('Group not found');
  66. }
  67. $backends = $group->getBackendNames();
  68. if (!in_array('LDAP', $backends)) {
  69. throw new \Exception('The given group is not a recognized LDAP group.');
  70. }
  71. if ($input->getOption('yes') === false) {
  72. /** @var QuestionHelper $helper */
  73. $helper = $this->getHelper('question');
  74. $q = new Question('Delete all local data of this group (y|N)? ');
  75. $input->setOption('yes', $helper->ask($input, $output, $q) === 'y');
  76. }
  77. if ($input->getOption('yes') !== true) {
  78. throw new \Exception('Reset cancelled by operator');
  79. }
  80. // Disable real deletion if a plugin supports it
  81. $pluginManagerSuppressed = $this->pluginManager->setSuppressDeletion(true);
  82. // Bypass groupExists test to force mapping deletion
  83. $this->backend->getLDAPAccess($gid)->connection->writeToCache('groupExists' . $gid, false);
  84. echo "calling delete $gid\n";
  85. if ($group->delete()) {
  86. $this->pluginManager->setSuppressDeletion($pluginManagerSuppressed);
  87. return self::SUCCESS;
  88. }
  89. } catch (\Throwable $e) {
  90. if (isset($pluginManagerSuppressed)) {
  91. $this->pluginManager->setSuppressDeletion($pluginManagerSuppressed);
  92. }
  93. $output->writeln('<error>' . $e->getMessage() . '</error>');
  94. return self::FAILURE;
  95. }
  96. $output->writeln('<error>Error while resetting group</error>');
  97. return self::INVALID;
  98. }
  99. }