RecoverUser.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2018 Bjoern Schiessle <bjoern@schiessle.org>
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\Encryption\Command;
  26. use OCA\Encryption\Util;
  27. use OCP\IConfig;
  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\Output\OutputInterface;
  34. use Symfony\Component\Console\Question\Question;
  35. class RecoverUser extends Command {
  36. /** @var Util */
  37. protected $util;
  38. /** @var IUserManager */
  39. protected $userManager;
  40. /** @var QuestionHelper */
  41. protected $questionHelper;
  42. /**
  43. * @param Util $util
  44. * @param IConfig $config
  45. * @param IUserManager $userManager
  46. * @param QuestionHelper $questionHelper
  47. */
  48. public function __construct(Util $util,
  49. IConfig $config,
  50. IUserManager $userManager,
  51. QuestionHelper $questionHelper) {
  52. $this->util = $util;
  53. $this->questionHelper = $questionHelper;
  54. $this->userManager = $userManager;
  55. parent::__construct();
  56. }
  57. protected function configure() {
  58. $this
  59. ->setName('encryption:recover-user')
  60. ->setDescription('Recover user data in case of password lost. This only works if the user enabled the recovery key.');
  61. $this->addArgument(
  62. 'user',
  63. InputArgument::REQUIRED,
  64. 'user which should be recovered'
  65. );
  66. }
  67. protected function execute(InputInterface $input, OutputInterface $output): int {
  68. $isMasterKeyEnabled = $this->util->isMasterKeyEnabled();
  69. if ($isMasterKeyEnabled) {
  70. $output->writeln('You use the master key, no individual user recovery needed.');
  71. return 0;
  72. }
  73. $uid = $input->getArgument('user');
  74. $userExists = $this->userManager->userExists($uid);
  75. if ($userExists === false) {
  76. $output->writeln('User "' . $uid . '" unknown.');
  77. return 1;
  78. }
  79. $recoveryKeyEnabled = $this->util->isRecoveryEnabledForUser($uid);
  80. if ($recoveryKeyEnabled === false) {
  81. $output->writeln('Recovery key is not enabled for: ' . $uid);
  82. return 1;
  83. }
  84. $question = new Question('Please enter the recovery key password: ');
  85. $question->setHidden(true);
  86. $question->setHiddenFallback(false);
  87. $recoveryPassword = $this->questionHelper->ask($input, $output, $question);
  88. $question = new Question('Please enter the new login password for the user: ');
  89. $question->setHidden(true);
  90. $question->setHiddenFallback(false);
  91. $newLoginPassword = $this->questionHelper->ask($input, $output, $question);
  92. $output->write('Start to recover users files... This can take some time...');
  93. $this->userManager->get($uid)->setPassword($newLoginPassword, $recoveryPassword);
  94. $output->writeln('Done.');
  95. return 0;
  96. }
  97. }