[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Console/ -> ChangeUserPasswordCommand.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2019 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\Console;
  11  
  12  use Joomla\CMS\User\User;
  13  use Joomla\CMS\User\UserHelper;
  14  use Joomla\Console\Command\AbstractCommand;
  15  use Symfony\Component\Console\Command\Command;
  16  use Symfony\Component\Console\Input\InputInterface;
  17  use Symfony\Component\Console\Input\InputOption;
  18  use Symfony\Component\Console\Output\OutputInterface;
  19  use Symfony\Component\Console\Style\SymfonyStyle;
  20  
  21  // phpcs:disable PSR1.Files.SideEffects
  22  \defined('JPATH_PLATFORM') or die;
  23  // phpcs:enable PSR1.Files.SideEffects
  24  
  25  /**
  26   * Console command  to change a user's password
  27   *
  28   * @since  4.0.0
  29   */
  30  class ChangeUserPasswordCommand extends AbstractCommand
  31  {
  32      /**
  33       * The default command name
  34       *
  35       * @var    string
  36       * @since  4.0.0
  37       */
  38      protected static $defaultName = 'user:reset-password';
  39  
  40      /**
  41       * SymfonyStyle Object
  42       * @var   object
  43       * @since 4.0.0
  44       */
  45      private $ioStyle;
  46  
  47      /**
  48       * Stores the Input Object
  49       * @var   object
  50       * @since 4.0.0
  51       */
  52      private $cliInput;
  53  
  54      /**
  55       * The username
  56       *
  57       * @var    string
  58       *
  59       * @since  4.0.0
  60       */
  61      private $username;
  62  
  63      /**
  64       * The password
  65       *
  66       * @var    string
  67       *
  68       * @since  4.0.0
  69       */
  70      private $password;
  71  
  72      /**
  73       * Internal function to execute the command.
  74       *
  75       * @param   InputInterface   $input   The input to inject into the command.
  76       * @param   OutputInterface  $output  The output to inject into the command.
  77       *
  78       * @return  integer  The command exit code
  79       *
  80       * @since   4.0.0
  81       */
  82      protected function doExecute(InputInterface $input, OutputInterface $output): int
  83      {
  84          $this->configureIO($input, $output);
  85          $this->ioStyle->title('Change Password');
  86          $this->username = $this->getStringFromOption('username', 'Please enter a username');
  87  
  88          $userId = UserHelper::getUserId($this->username);
  89  
  90          if (empty($userId)) {
  91              $this->ioStyle->error("The user " . $this->username . " does not exist!");
  92  
  93              return Command::FAILURE;
  94          }
  95  
  96          $user = User::getInstance($userId);
  97          $this->password = $this->getStringFromOption('password', 'Please enter a new password');
  98  
  99          $user->password = UserHelper::hashPassword($this->password);
 100  
 101          if (!$user->save(true)) {
 102              $this->ioStyle->error($user->getError());
 103  
 104              return Command::FAILURE;
 105          }
 106  
 107          $this->ioStyle->success("Password changed!");
 108  
 109          return Command::SUCCESS;
 110      }
 111  
 112      /**
 113       * Method to get a value from option
 114       *
 115       * @param   string  $option    set the option name
 116       *
 117       * @param   string  $question  set the question if user enters no value to option
 118       *
 119       * @return  string
 120       *
 121       * @since   4.0.0
 122       */
 123      protected function getStringFromOption($option, $question): string
 124      {
 125          $answer = (string) $this->cliInput->getOption($option);
 126  
 127          while (!$answer) {
 128              if ($option === 'password') {
 129                  $answer = (string) $this->ioStyle->askHidden($question);
 130              } else {
 131                  $answer = (string) $this->ioStyle->ask($question);
 132              }
 133          }
 134  
 135          return $answer;
 136      }
 137  
 138      /**
 139       * Configure the IO.
 140       *
 141       * @param   InputInterface   $input   The input to inject into the command.
 142       * @param   OutputInterface  $output  The output to inject into the command.
 143       *
 144       * @return  void
 145       *
 146       * @since   4.0.0
 147       */
 148      private function configureIO(InputInterface $input, OutputInterface $output)
 149      {
 150          $this->cliInput = $input;
 151          $this->ioStyle = new SymfonyStyle($input, $output);
 152      }
 153  
 154      /**
 155       * Configure the command.
 156       *
 157       * @return  void
 158       *
 159       * @since   4.0.0
 160       */
 161      protected function configure(): void
 162      {
 163          $help = "<info>%command.name%</info> will change a user's password
 164          \nUsage: <info>php %command.full_name%</info>";
 165  
 166          $this->addOption('username', null, InputOption::VALUE_OPTIONAL, 'username');
 167          $this->addOption('password', null, InputOption::VALUE_OPTIONAL, 'password');
 168          $this->setDescription("Change a user's password");
 169          $this->setHelp($help);
 170      }
 171  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer