ResetPassword.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Andreas Fischer <bantu@owncloud.com>
  6. * @author Christopher Schäpers <kondou@ts.unde.re>
  7. * @author Clark Tomlinson <fallen013@gmail.com>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Laurens Post <lkpost@scept.re>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author Sujith H <sharidasan@owncloud.com>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OC\Core\Command\User;
  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\ConfirmationQuestion;
  38. use Symfony\Component\Console\Question\Question;
  39. class ResetPassword extends Command {
  40. /** @var IUserManager */
  41. protected $userManager;
  42. public function __construct(IUserManager $userManager) {
  43. $this->userManager = $userManager;
  44. parent::__construct();
  45. }
  46. protected function configure() {
  47. $this
  48. ->setName('user:resetpassword')
  49. ->setDescription('Resets the password of the named user')
  50. ->addArgument(
  51. 'user',
  52. InputArgument::REQUIRED,
  53. 'Username to reset password'
  54. )
  55. ->addOption(
  56. 'password-from-env',
  57. null,
  58. InputOption::VALUE_NONE,
  59. 'read password from environment variable OC_PASS'
  60. )
  61. ;
  62. }
  63. protected function execute(InputInterface $input, OutputInterface $output): int {
  64. $username = $input->getArgument('user');
  65. $user = $this->userManager->get($username);
  66. if (is_null($user)) {
  67. $output->writeln('<error>User does not exist</error>');
  68. return 1;
  69. }
  70. if ($input->getOption('password-from-env')) {
  71. $password = getenv('OC_PASS');
  72. if (!$password) {
  73. $output->writeln('<error>--password-from-env given, but OC_PASS is empty!</error>');
  74. return 1;
  75. }
  76. } elseif ($input->isInteractive()) {
  77. /** @var QuestionHelper $helper */
  78. $helper = $this->getHelper('question');
  79. if (\OCP\App::isEnabled('encryption')) {
  80. $output->writeln(
  81. '<error>Warning: Resetting the password when using encryption will result in data loss!</error>'
  82. );
  83. $question = new ConfirmationQuestion('Do you want to continue?');
  84. if (!$helper->ask($input, $output, $question)) {
  85. return 1;
  86. }
  87. }
  88. $question = new Question('Enter a new password: ');
  89. $question->setHidden(true);
  90. $password = $helper->ask($input, $output, $question);
  91. if ($password === null) {
  92. $output->writeln("<error>Password cannot be empty!</error>");
  93. return 1;
  94. }
  95. $question = new Question('Confirm the new password: ');
  96. $question->setHidden(true);
  97. $confirm = $helper->ask($input, $output, $question);
  98. if ($password !== $confirm) {
  99. $output->writeln("<error>Passwords did not match!</error>");
  100. return 1;
  101. }
  102. } else {
  103. $output->writeln("<error>Interactive input or --password-from-env is needed for entering a new password!</error>");
  104. return 1;
  105. }
  106. try {
  107. $success = $user->setPassword($password);
  108. } catch (\Exception $e) {
  109. $output->writeln('<error>' . $e->getMessage() . '</error>');
  110. return 1;
  111. }
  112. if ($success) {
  113. $output->writeln("<info>Successfully reset password for " . $username . "</info>");
  114. } else {
  115. $output->writeln("<error>Error while resetting password!</error>");
  116. return 1;
  117. }
  118. return 0;
  119. }
  120. }