ResetPassword.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 OC\Core\Command\Base;
  30. use OCP\App\IAppManager;
  31. use OCP\IUser;
  32. use OCP\IUserManager;
  33. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  34. use Symfony\Component\Console\Helper\QuestionHelper;
  35. use Symfony\Component\Console\Input\InputArgument;
  36. use Symfony\Component\Console\Input\InputInterface;
  37. use Symfony\Component\Console\Input\InputOption;
  38. use Symfony\Component\Console\Output\OutputInterface;
  39. use Symfony\Component\Console\Question\ConfirmationQuestion;
  40. use Symfony\Component\Console\Question\Question;
  41. class ResetPassword extends Base {
  42. public function __construct(
  43. protected IUserManager $userManager,
  44. private IAppManager $appManager,
  45. ) {
  46. parent::__construct();
  47. }
  48. protected function configure() {
  49. $this
  50. ->setName('user:resetpassword')
  51. ->setDescription('Resets the password of the named user')
  52. ->addArgument(
  53. 'user',
  54. InputArgument::REQUIRED,
  55. 'Username to reset password'
  56. )
  57. ->addOption(
  58. 'password-from-env',
  59. null,
  60. InputOption::VALUE_NONE,
  61. 'read password from environment variable OC_PASS'
  62. )
  63. ;
  64. }
  65. protected function execute(InputInterface $input, OutputInterface $output): int {
  66. $username = $input->getArgument('user');
  67. $user = $this->userManager->get($username);
  68. if (is_null($user)) {
  69. $output->writeln('<error>User does not exist</error>');
  70. return 1;
  71. }
  72. if ($input->getOption('password-from-env')) {
  73. $password = getenv('OC_PASS');
  74. if (!$password) {
  75. $output->writeln('<error>--password-from-env given, but OC_PASS is empty!</error>');
  76. return 1;
  77. }
  78. } elseif ($input->isInteractive()) {
  79. /** @var QuestionHelper $helper */
  80. $helper = $this->getHelper('question');
  81. if ($this->appManager->isEnabledForUser('encryption', $user)) {
  82. $output->writeln(
  83. '<error>Warning: Resetting the password when using encryption will result in data loss!</error>'
  84. );
  85. $question = new ConfirmationQuestion('Do you want to continue?');
  86. if (!$helper->ask($input, $output, $question)) {
  87. return 1;
  88. }
  89. }
  90. $question = new Question('Enter a new password: ');
  91. $question->setHidden(true);
  92. $password = $helper->ask($input, $output, $question);
  93. if ($password === null) {
  94. $output->writeln("<error>Password cannot be empty!</error>");
  95. return 1;
  96. }
  97. $question = new Question('Confirm the new password: ');
  98. $question->setHidden(true);
  99. $confirm = $helper->ask($input, $output, $question);
  100. if ($password !== $confirm) {
  101. $output->writeln("<error>Passwords did not match!</error>");
  102. return 1;
  103. }
  104. } else {
  105. $output->writeln("<error>Interactive input or --password-from-env is needed for entering a new password!</error>");
  106. return 1;
  107. }
  108. try {
  109. $success = $user->setPassword($password);
  110. } catch (\Exception $e) {
  111. $output->writeln('<error>' . $e->getMessage() . '</error>');
  112. return 1;
  113. }
  114. if ($success) {
  115. $output->writeln("<info>Successfully reset password for " . $username . "</info>");
  116. } else {
  117. $output->writeln("<error>Error while resetting password!</error>");
  118. return 1;
  119. }
  120. return 0;
  121. }
  122. /**
  123. * @param string $argumentName
  124. * @param CompletionContext $context
  125. * @return string[]
  126. */
  127. public function completeArgumentValues($argumentName, CompletionContext $context) {
  128. if ($argumentName === 'user') {
  129. return array_map(static fn (IUser $user) => $user->getUID(), $this->userManager->search($context->getCurrentWord()));
  130. }
  131. return [];
  132. }
  133. }