1
0

RecoverUser.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. /** @var Util */
  18. protected $util;
  19. /** @var IUserManager */
  20. protected $userManager;
  21. /** @var QuestionHelper */
  22. protected $questionHelper;
  23. /**
  24. * @param Util $util
  25. * @param IConfig $config
  26. * @param IUserManager $userManager
  27. * @param QuestionHelper $questionHelper
  28. */
  29. public function __construct(Util $util,
  30. IConfig $config,
  31. IUserManager $userManager,
  32. QuestionHelper $questionHelper) {
  33. $this->util = $util;
  34. $this->questionHelper = $questionHelper;
  35. $this->userManager = $userManager;
  36. parent::__construct();
  37. }
  38. protected function configure() {
  39. $this
  40. ->setName('encryption:recover-user')
  41. ->setDescription('Recover user data in case of password lost. This only works if the user enabled the recovery key.');
  42. $this->addArgument(
  43. 'user',
  44. InputArgument::REQUIRED,
  45. 'user which should be recovered'
  46. );
  47. }
  48. protected function execute(InputInterface $input, OutputInterface $output): int {
  49. $isMasterKeyEnabled = $this->util->isMasterKeyEnabled();
  50. if ($isMasterKeyEnabled) {
  51. $output->writeln('You use the master key, no individual user recovery needed.');
  52. return 0;
  53. }
  54. $uid = $input->getArgument('user');
  55. $userExists = $this->userManager->userExists($uid);
  56. if ($userExists === false) {
  57. $output->writeln('User "' . $uid . '" unknown.');
  58. return 1;
  59. }
  60. $recoveryKeyEnabled = $this->util->isRecoveryEnabledForUser($uid);
  61. if ($recoveryKeyEnabled === false) {
  62. $output->writeln('Recovery key is not enabled for: ' . $uid);
  63. return 1;
  64. }
  65. $question = new Question('Please enter the recovery key password: ');
  66. $question->setHidden(true);
  67. $question->setHiddenFallback(false);
  68. $recoveryPassword = $this->questionHelper->ask($input, $output, $question);
  69. $question = new Question('Please enter the new login password for the user: ');
  70. $question->setHidden(true);
  71. $question->setHiddenFallback(false);
  72. $newLoginPassword = $this->questionHelper->ask($input, $output, $question);
  73. $output->write('Start to recover users files... This can take some time...');
  74. $this->userManager->get($uid)->setPassword($newLoginPassword, $recoveryPassword);
  75. $output->writeln('Done.');
  76. return 0;
  77. }
  78. }