|
| 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 | + |
| 24 | +namespace OCA\User_LDAP\Command; |
| 25 | + |
| 26 | +use OCA\User_LDAP\User\DeletedUsersIndex; |
| 27 | +use OCA\User_LDAP\User_Proxy; |
| 28 | +use OCA\User_LDAP\UserPluginManager; |
| 29 | +use OCP\IUser; |
| 30 | +use OCP\IUserManager; |
| 31 | +use Symfony\Component\Console\Command\Command; |
| 32 | +use Symfony\Component\Console\Helper\QuestionHelper; |
| 33 | +use Symfony\Component\Console\Input\InputArgument; |
| 34 | +use Symfony\Component\Console\Input\InputInterface; |
| 35 | +use Symfony\Component\Console\Input\InputOption; |
| 36 | +use Symfony\Component\Console\Output\OutputInterface; |
| 37 | +use Symfony\Component\Console\Question\Question; |
| 38 | + |
| 39 | +class ResetUser extends Command { |
| 40 | + /** @var DeletedUsersIndex */ |
| 41 | + protected $dui; |
| 42 | + /** @var IUserManager */ |
| 43 | + private $userManager; |
| 44 | + /** @var UserPluginManager */ |
| 45 | + private $pluginManager; |
| 46 | + |
| 47 | + public function __construct( |
| 48 | + DeletedUsersIndex $dui, |
| 49 | + IUserManager $userManager, |
| 50 | + UserPluginManager $pluginManager |
| 51 | + ) { |
| 52 | + $this->dui = $dui; |
| 53 | + $this->userManager = $userManager; |
| 54 | + $this->pluginManager = $pluginManager; |
| 55 | + parent::__construct(); |
| 56 | + } |
| 57 | + |
| 58 | + protected function configure() { |
| 59 | + $this |
| 60 | + ->setName('ldap:reset-user') |
| 61 | + ->setDescription('deletes an LDAP user independent of the user state') |
| 62 | + ->addArgument( |
| 63 | + 'uid', |
| 64 | + InputArgument::REQUIRED, |
| 65 | + 'the user id as used in Nextcloud' |
| 66 | + ) |
| 67 | + ->addOption( |
| 68 | + 'yes', |
| 69 | + 'y', |
| 70 | + InputOption::VALUE_NONE, |
| 71 | + 'do not ask for confirmation' |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
| 76 | + try { |
| 77 | + $uid = $input->getArgument('uid'); |
| 78 | + $user = $this->userManager->get($uid); |
| 79 | + if (!$user instanceof IUser) { |
| 80 | + throw new \Exception('User not found'); |
| 81 | + } |
| 82 | + $backend = $user->getBackend(); |
| 83 | + if (!$backend instanceof User_Proxy) { |
| 84 | + throw new \Exception('The given user is not a recognized LDAP user.'); |
| 85 | + } |
| 86 | + if ($input->getOption('yes') === false) { |
| 87 | + /** @var QuestionHelper $helper */ |
| 88 | + $helper = $this->getHelper('question'); |
| 89 | + $q = new Question('Delete all local data of this user (y|N)? '); |
| 90 | + $input->setOption('yes', $helper->ask($input, $output, $q) === 'y'); |
| 91 | + } |
| 92 | + if ($input->getOption('yes') !== true) { |
| 93 | + throw new \Exception('Reset cancelled by operator'); |
| 94 | + } |
| 95 | + |
| 96 | + $this->dui->markUser($uid); |
| 97 | + $pluginManagerSuppressed = $this->pluginManager->setSuppressDeletion(true); |
| 98 | + if ($user->delete()) { |
| 99 | + $this->pluginManager->setSuppressDeletion($pluginManagerSuppressed); |
| 100 | + return 0; |
| 101 | + } |
| 102 | + } catch (\Throwable $e) { |
| 103 | + if (isset($pluginManagerSuppressed)) { |
| 104 | + $this->pluginManager->setSuppressDeletion($pluginManagerSuppressed); |
| 105 | + } |
| 106 | + $output->writeln('<error>' . $e->getMessage() . '</error>'); |
| 107 | + return 1; |
| 108 | + } |
| 109 | + $output->writeln('<error>Error while resetting user</error>'); |
| 110 | + return 2; |
| 111 | + } |
| 112 | +} |
0 commit comments