CoreFiles by mySites.guru
ChangeUserPasswordCommand.php176 lines, 3,787 bytes, from Joomla_4.0.0-alpha12-Alpha-Full_Package.zip
Raw History Compare with 4.0.0-alpha11
md5
af3ee3edc0c8c0e14959226aee0d061f
sha1
7dfc6637a4ec8faf32956a1809cbe83af8cdfc46
sha256
e92357b496c60c3f0950214472a0fa7dd923be1b651893c29f4caa6632d3e154
<?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.