ResetUser.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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\User\DeletedUsersIndex;
  8. use OCA\User_LDAP\User_Proxy;
  9. use OCA\User_LDAP\UserPluginManager;
  10. use OCP\IUser;
  11. use OCP\IUserManager;
  12. use Symfony\Component\Console\Command\Command;
  13. use Symfony\Component\Console\Helper\QuestionHelper;
  14. use Symfony\Component\Console\Input\InputArgument;
  15. use Symfony\Component\Console\Input\InputInterface;
  16. use Symfony\Component\Console\Input\InputOption;
  17. use Symfony\Component\Console\Output\OutputInterface;
  18. use Symfony\Component\Console\Question\Question;
  19. class ResetUser extends Command {
  20. public function __construct(
  21. protected DeletedUsersIndex $dui,
  22. private IUserManager $userManager,
  23. private UserPluginManager $pluginManager,
  24. ) {
  25. parent::__construct();
  26. }
  27. protected function configure(): void {
  28. $this
  29. ->setName('ldap:reset-user')
  30. ->setDescription('deletes an LDAP user independent of the user state')
  31. ->addArgument(
  32. 'uid',
  33. InputArgument::REQUIRED,
  34. 'the user id as used in Nextcloud'
  35. )
  36. ->addOption(
  37. 'yes',
  38. 'y',
  39. InputOption::VALUE_NONE,
  40. 'do not ask for confirmation'
  41. );
  42. }
  43. protected function execute(InputInterface $input, OutputInterface $output): int {
  44. try {
  45. $uid = $input->getArgument('uid');
  46. $user = $this->userManager->get($uid);
  47. if (!$user instanceof IUser) {
  48. throw new \Exception('User not found');
  49. }
  50. $backend = $user->getBackend();
  51. if (!$backend instanceof User_Proxy) {
  52. throw new \Exception('The given user is not a recognized LDAP user.');
  53. }
  54. if ($input->getOption('yes') === false) {
  55. /** @var QuestionHelper $helper */
  56. $helper = $this->getHelper('question');
  57. $q = new Question('Delete all local data of this user (y|N)? ');
  58. $input->setOption('yes', $helper->ask($input, $output, $q) === 'y');
  59. }
  60. if ($input->getOption('yes') !== true) {
  61. throw new \Exception('Reset cancelled by operator');
  62. }
  63. $this->dui->markUser($uid);
  64. $pluginManagerSuppressed = $this->pluginManager->setSuppressDeletion(true);
  65. if ($user->delete()) {
  66. $this->pluginManager->setSuppressDeletion($pluginManagerSuppressed);
  67. return self::SUCCESS;
  68. }
  69. } catch (\Throwable $e) {
  70. if (isset($pluginManagerSuppressed)) {
  71. $this->pluginManager->setSuppressDeletion($pluginManagerSuppressed);
  72. }
  73. $output->writeln('<error>' . $e->getMessage() . '</error>');
  74. return self::FAILURE;
  75. }
  76. $output->writeln('<error>Error while resetting user</error>');
  77. return self::INVALID;
  78. }
  79. }