ResetPassword.php 4.0 KB

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