resetpassword.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * @author Andreas Fischer <bantu@owncloud.com>
  4. * @author Christopher Schäpers <kondou@ts.unde.re>
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. *
  7. * @copyright Copyright (c) 2015, ownCloud, Inc.
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC\Core\Command\User;
  24. use Symfony\Component\Console\Command\Command;
  25. use Symfony\Component\Console\Input\InputInterface;
  26. use Symfony\Component\Console\Input\InputArgument;
  27. use Symfony\Component\Console\Output\OutputInterface;
  28. class ResetPassword extends Command {
  29. /** @var \OC\User\Manager */
  30. protected $userManager;
  31. public function __construct(\OC\User\Manager $userManager) {
  32. $this->userManager = $userManager;
  33. parent::__construct();
  34. }
  35. protected function configure() {
  36. $this
  37. ->setName('user:resetpassword')
  38. ->setDescription('Resets the password of the named user')
  39. ->addArgument(
  40. 'user',
  41. InputArgument::REQUIRED,
  42. 'Username to reset password'
  43. )
  44. ;
  45. }
  46. protected function execute(InputInterface $input, OutputInterface $output) {
  47. $username = $input->getArgument('user');
  48. /** @var $user \OC\User\User */
  49. $user = $this->userManager->get($username);
  50. if (is_null($user)) {
  51. $output->writeln("<error>There is no user called " . $username . "</error>");
  52. return 1;
  53. }
  54. if ($input->isInteractive()) {
  55. /** @var $dialog \Symfony\Component\Console\Helper\DialogHelper */
  56. $dialog = $this->getHelperSet()->get('dialog');
  57. if (\OCP\App::isEnabled('files_encryption')) {
  58. $output->writeln(
  59. '<error>Warning: Resetting the password when using encryption will result in data loss!</error>'
  60. );
  61. if (!$dialog->askConfirmation($output, '<question>Do you want to continue?</question>', true)) {
  62. return 1;
  63. }
  64. }
  65. $password = $dialog->askHiddenResponse(
  66. $output,
  67. '<question>Enter a new password: </question>',
  68. false
  69. );
  70. $confirm = $dialog->askHiddenResponse(
  71. $output,
  72. '<question>Confirm the new password: </question>',
  73. false
  74. );
  75. if ($password === $confirm) {
  76. $success = $user->setPassword($password);
  77. if ($success) {
  78. $output->writeln("<info>Successfully reset password for " . $username . "</info>");
  79. } else {
  80. $output->writeln("<error>Error while resetting password!</error>");
  81. return 1;
  82. }
  83. } else {
  84. $output->writeln("<error>Passwords did not match!</error>");
  85. return 1;
  86. }
  87. } else {
  88. $output->writeln("<error>Interactive input is needed for entering a new password!</error>");
  89. return 1;
  90. }
  91. }
  92. }