RecoverUser.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Encryption\Command;
  7. use OCA\Encryption\Util;
  8. use OCP\IConfig;
  9. use OCP\IUserManager;
  10. use Symfony\Component\Console\Command\Command;
  11. use Symfony\Component\Console\Helper\QuestionHelper;
  12. use Symfony\Component\Console\Input\InputArgument;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use Symfony\Component\Console\Question\Question;
  16. class RecoverUser extends Command {
  17. public function __construct(
  18. protected Util $util,
  19. IConfig $config,
  20. protected IUserManager $userManager,
  21. protected QuestionHelper $questionHelper,
  22. ) {
  23. parent::__construct();
  24. }
  25. protected function configure(): void {
  26. $this
  27. ->setName('encryption:recover-user')
  28. ->setDescription('Recover user data in case of password lost. This only works if the user enabled the recovery key.');
  29. $this->addArgument(
  30. 'user',
  31. InputArgument::REQUIRED,
  32. 'user which should be recovered'
  33. );
  34. }
  35. protected function execute(InputInterface $input, OutputInterface $output): int {
  36. $isMasterKeyEnabled = $this->util->isMasterKeyEnabled();
  37. if ($isMasterKeyEnabled) {
  38. $output->writeln('You use the master key, no individual user recovery needed.');
  39. return self::SUCCESS;
  40. }
  41. $uid = $input->getArgument('user');
  42. $userExists = $this->userManager->userExists($uid);
  43. if ($userExists === false) {
  44. $output->writeln('User "' . $uid . '" unknown.');
  45. return self::FAILURE;
  46. }
  47. $recoveryKeyEnabled = $this->util->isRecoveryEnabledForUser($uid);
  48. if ($recoveryKeyEnabled === false) {
  49. $output->writeln('Recovery key is not enabled for: ' . $uid);
  50. return self::FAILURE;
  51. }
  52. $question = new Question('Please enter the recovery key password: ');
  53. $question->setHidden(true);
  54. $question->setHiddenFallback(false);
  55. $recoveryPassword = $this->questionHelper->ask($input, $output, $question);
  56. $question = new Question('Please enter the new login password for the user: ');
  57. $question->setHidden(true);
  58. $question->setHiddenFallback(false);
  59. $newLoginPassword = $this->questionHelper->ask($input, $output, $question);
  60. $output->write('Start to recover users files... This can take some time...');
  61. $this->userManager->get($uid)->setPassword($newLoginPassword, $recoveryPassword);
  62. $output->writeln('Done.');
  63. return self::SUCCESS;
  64. }
  65. }