ResetUser.php 3.5 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. *
  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. /** @var DeletedUsersIndex */
  38. protected $dui;
  39. /** @var IUserManager */
  40. private $userManager;
  41. /** @var UserPluginManager */
  42. private $pluginManager;
  43. public function __construct(
  44. DeletedUsersIndex $dui,
  45. IUserManager $userManager,
  46. UserPluginManager $pluginManager
  47. ) {
  48. $this->dui = $dui;
  49. $this->userManager = $userManager;
  50. $this->pluginManager = $pluginManager;
  51. parent::__construct();
  52. }
  53. protected function configure() {
  54. $this
  55. ->setName('ldap:reset-user')
  56. ->setDescription('deletes an LDAP user independent of the user state')
  57. ->addArgument(
  58. 'uid',
  59. InputArgument::REQUIRED,
  60. 'the user id as used in Nextcloud'
  61. )
  62. ->addOption(
  63. 'yes',
  64. 'y',
  65. InputOption::VALUE_NONE,
  66. 'do not ask for confirmation'
  67. );
  68. }
  69. protected function execute(InputInterface $input, OutputInterface $output): int {
  70. try {
  71. $uid = $input->getArgument('uid');
  72. $user = $this->userManager->get($uid);
  73. if (!$user instanceof IUser) {
  74. throw new \Exception('User not found');
  75. }
  76. $backend = $user->getBackend();
  77. if (!$backend instanceof User_Proxy) {
  78. throw new \Exception('The given user is not a recognized LDAP user.');
  79. }
  80. if ($input->getOption('yes') === false) {
  81. /** @var QuestionHelper $helper */
  82. $helper = $this->getHelper('question');
  83. $q = new Question('Delete all local data of this user (y|N)? ');
  84. $input->setOption('yes', $helper->ask($input, $output, $q) === 'y');
  85. }
  86. if ($input->getOption('yes') !== true) {
  87. throw new \Exception('Reset cancelled by operator');
  88. }
  89. $this->dui->markUser($uid);
  90. $pluginManagerSuppressed = $this->pluginManager->setSuppressDeletion(true);
  91. if ($user->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 user</error>');
  103. return 2;
  104. }
  105. }