- md5
- af3ee3edc0c8c0e14959226aee0d061f
- sha1
- 7dfc6637a4ec8faf32956a1809cbe83af8cdfc46
- sha256
- e92357b496c60c3f0950214472a0fa7dd923be1b651893c29f4caa6632d3e154
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\Console;
\defined('JPATH_PLATFORM') or die;
use Joomla\CMS\User\UserHelper;
use Joomla\Console\Command\AbstractCommand;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Joomla\CMS\User\User;
/**
* Console command to change a user's password
*
* @since 4.0.0
*/
class ChangeUserPasswordCommand extends AbstractCommand
{
/**
* The default command name
*
* @var string
* @since 4.0.0
*/
protected static $defaultName = 'user:reset-password';
/**
* SymfonyStyle Object
* @var object
* @since 4.0.0
*/
private $ioStyle;
/**
* Stores the Input Object
* @var object
* @since 4.0.0
*/
private $cliInput;
/**
* The username
*
* @var string
*
* @since 4.0.0
*/
private $username;
/**
* The password
*
* @var string
*
* @since 4.0.0
*/
private $password;
/**
* Internal function to execute the command.
*
* @param InputInterface $input The input to inject into the command.
* @param OutputInterface $output The output to inject into the command.
*
* @return integer The command exit code
*
* @since 4.0.0
*/
protected function doExecute(InputInterface $input, OutputInterface $output): int
{
$this->configureIO($input, $output);
$this->username = $this->getStringFromOption('username', 'Please enter a username');
$this->ioStyle->title('Change password');
$userId = UserHelper::getUserId($this->username);
if (empty($userId))
{
$this->ioStyle->error("The user " . $this->username . " does not exist!");
return 1;
}
$user = User::getInstance($userId);
$this->password = $this->getStringFromOption('password', 'Please enter a new password');
$user->password = UserHelper::hashPassword($this->password);
if (!$user->save(true))
{
$this->ioStyle->error($user->getError());
return 1;
}
$this->ioStyle->success("Password changed!");
return 0;
}
/**
* Method to get a value from option
*
* @param string $option set the option name
*
* @param string $question set the question if user enters no value to option
*
* @return string
*
* @since 4.0.0
*/
protected function getStringFromOption($option, $question): string
{
$answer = (string) $this->cliInput->getOption($option);
while (!$answer)
{
if ($option === 'password')
{
$answer = (string) $this->ioStyle->askHidden($question);
}
else
{
$answer = (string) $this->ioStyle->ask($question);
}
}
return $answer;
}
/**
* Configure the IO.
*
* @param InputInterface $input The input to inject into the command.
* @param OutputInterface $output The output to inject into the command.
*
* @return void
*
* @since 4.0.0
*/
private function configureIO(InputInterface $input, OutputInterface $output)
{
$this->cliInput = $input;
$this->ioStyle = new SymfonyStyle($input, $output);
}
/**
* Configure the command.
*
* @return void
*
* @since 4.0.0
*/
protected function configure(): void
{
$this->addOption('username', null, InputOption::VALUE_OPTIONAL, 'username');
$this->addOption('password', null, InputOption::VALUE_OPTIONAL, 'password');
$this->setDescription("Changes a user's password");
$this->setHelp(
<<<EOF
The <info>%command.name%</info> command changes the user's password
<info>php %command.full_name%</info>
EOF
);
}
}
No other release has this exact content. See every version of this file.