ResetPassword.php 4.5 KB

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