You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ResetPassword.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Core\Command\User;
  8. use OC\Core\Command\Base;
  9. use OCP\App\IAppManager;
  10. use OCP\IUser;
  11. use OCP\IUserManager;
  12. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  13. use Symfony\Component\Console\Helper\QuestionHelper;
  14. use Symfony\Component\Console\Input\InputArgument;
  15. use Symfony\Component\Console\Input\InputInterface;
  16. use Symfony\Component\Console\Input\InputOption;
  17. use Symfony\Component\Console\Output\OutputInterface;
  18. use Symfony\Component\Console\Question\ConfirmationQuestion;
  19. use Symfony\Component\Console\Question\Question;
  20. class ResetPassword extends Base {
  21. public function __construct(
  22. protected IUserManager $userManager,
  23. private IAppManager $appManager,
  24. ) {
  25. parent::__construct();
  26. }
  27. protected function configure() {
  28. $this
  29. ->setName('user:resetpassword')
  30. ->setDescription('Resets the password of the named user')
  31. ->addArgument(
  32. 'user',
  33. InputArgument::REQUIRED,
  34. 'Login to reset password'
  35. )
  36. ->addOption(
  37. 'password-from-env',
  38. null,
  39. InputOption::VALUE_NONE,
  40. 'read password from environment variable OC_PASS'
  41. )
  42. ;
  43. }
  44. protected function execute(InputInterface $input, OutputInterface $output): int {
  45. $username = $input->getArgument('user');
  46. $user = $this->userManager->get($username);
  47. if (is_null($user)) {
  48. $output->writeln('<error>User does not exist</error>');
  49. return 1;
  50. }
  51. if ($input->getOption('password-from-env')) {
  52. $password = getenv('OC_PASS');
  53. if (!$password) {
  54. $output->writeln('<error>--password-from-env given, but OC_PASS is empty!</error>');
  55. return 1;
  56. }
  57. } elseif ($input->isInteractive()) {
  58. /** @var QuestionHelper $helper */
  59. $helper = $this->getHelper('question');
  60. if ($this->appManager->isEnabledForUser('encryption', $user)) {
  61. $output->writeln(
  62. '<error>Warning: Resetting the password when using encryption will result in data loss!</error>'
  63. );
  64. $question = new ConfirmationQuestion('Do you want to continue?');
  65. if (!$helper->ask($input, $output, $question)) {
  66. return 1;
  67. }
  68. }
  69. $question = new Question('Enter a new password: ');
  70. $question->setHidden(true);
  71. $password = $helper->ask($input, $output, $question);
  72. if ($password === null) {
  73. $output->writeln("<error>Password cannot be empty!</error>");
  74. return 1;
  75. }
  76. $question = new Question('Confirm the new password: ');
  77. $question->setHidden(true);
  78. $confirm = $helper->ask($input, $output, $question);
  79. if ($password !== $confirm) {
  80. $output->writeln("<error>Passwords did not match!</error>");
  81. return 1;
  82. }
  83. } else {
  84. $output->writeln("<error>Interactive input or --password-from-env is needed for entering a new password!</error>");
  85. return 1;
  86. }
  87. try {
  88. $success = $user->setPassword($password);
  89. } catch (\Exception $e) {
  90. $output->writeln('<error>' . $e->getMessage() . '</error>');
  91. return 1;
  92. }
  93. if ($success) {
  94. $output->writeln("<info>Successfully reset password for " . $username . "</info>");
  95. } else {
  96. $output->writeln("<error>Error while resetting password!</error>");
  97. return 1;
  98. }
  99. return 0;
  100. }
  101. /**
  102. * @param string $argumentName
  103. * @param CompletionContext $context
  104. * @return string[]
  105. */
  106. public function completeArgumentValues($argumentName, CompletionContext $context) {
  107. if ($argumentName === 'user') {
  108. return array_map(static fn (IUser $user) => $user->getUID(), $this->userManager->search($context->getCurrentWord()));
  109. }
  110. return [];
  111. }
  112. }