ResetUser.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2021 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OCA\User_LDAP\Command;
  24. use OCA\User_LDAP\User\DeletedUsersIndex;
  25. use OCA\User_LDAP\User_Proxy;
  26. use OCA\User_LDAP\UserPluginManager;
  27. use OCP\IUser;
  28. use OCP\IUserManager;
  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 ResetUser extends Command {
  37. public function __construct(
  38. protected DeletedUsersIndex $dui,
  39. private IUserManager $userManager,
  40. private UserPluginManager $pluginManager,
  41. ) {
  42. parent::__construct();
  43. }
  44. protected function configure(): void {
  45. $this
  46. ->setName('ldap:reset-user')
  47. ->setDescription('deletes an LDAP user independent of the user state')
  48. ->addArgument(
  49. 'uid',
  50. InputArgument::REQUIRED,
  51. 'the user id 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. $uid = $input->getArgument('uid');
  63. $user = $this->userManager->get($uid);
  64. if (!$user instanceof IUser) {
  65. throw new \Exception('User not found');
  66. }
  67. $backend = $user->getBackend();
  68. if (!$backend instanceof User_Proxy) {
  69. throw new \Exception('The given user is not a recognized LDAP user.');
  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 user (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. $this->dui->markUser($uid);
  81. $pluginManagerSuppressed = $this->pluginManager->setSuppressDeletion(true);
  82. if ($user->delete()) {
  83. $this->pluginManager->setSuppressDeletion($pluginManagerSuppressed);
  84. return self::SUCCESS;
  85. }
  86. } catch (\Throwable $e) {
  87. if (isset($pluginManagerSuppressed)) {
  88. $this->pluginManager->setSuppressDeletion($pluginManagerSuppressed);
  89. }
  90. $output->writeln('<error>' . $e->getMessage() . '</error>');
  91. return self::FAILURE;
  92. }
  93. $output->writeln('<error>Error while resetting user</error>');
  94. return self::INVALID;
  95. }
  96. }