[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Console/ -> AddUserToGroupCommand.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\Access\Access;
  13  use Joomla\CMS\User\User;
  14  use Joomla\CMS\User\UserHelper;
  15  use Joomla\Console\Command\AbstractCommand;
  16  use Joomla\Database\DatabaseAwareTrait;
  17  use Joomla\Database\DatabaseInterface;
  18  use Symfony\Component\Console\Command\Command;
  19  use Symfony\Component\Console\Exception\InvalidOptionException;
  20  use Symfony\Component\Console\Input\InputInterface;
  21  use Symfony\Component\Console\Input\InputOption;
  22  use Symfony\Component\Console\Output\OutputInterface;
  23  use Symfony\Component\Console\Question\ChoiceQuestion;
  24  use Symfony\Component\Console\Style\SymfonyStyle;
  25  
  26  // phpcs:disable PSR1.Files.SideEffects
  27  \defined('JPATH_PLATFORM') or die;
  28  // phpcs:enable PSR1.Files.SideEffects
  29  
  30  /**
  31   * Console command to add a user to group
  32   *
  33   * @since  4.0.0
  34   */
  35  class AddUserToGroupCommand extends AbstractCommand
  36  {
  37      use DatabaseAwareTrait;
  38  
  39      /**
  40       * The default command name
  41       *
  42       * @var    string
  43       * @since  4.0.0
  44       */
  45      protected static $defaultName = 'user:addtogroup';
  46  
  47      /**
  48       * SymfonyStyle Object
  49       * @var   object
  50       * @since 4.0.0
  51       */
  52      private $ioStyle;
  53  
  54      /**
  55       * Stores the Input Object
  56       * @var   object
  57       * @since 4.0.0
  58       */
  59      private $cliInput;
  60  
  61      /**
  62       * The username
  63       *
  64       * @var    string
  65       *
  66       * @since  4.0.0
  67       */
  68      private $username;
  69  
  70      /**
  71       * The usergroups
  72       *
  73       * @var    array
  74       *
  75       * @since  4.0.0
  76       */
  77      private $userGroups = [];
  78  
  79      /**
  80       * Command constructor.
  81       *
  82       * @param   DatabaseInterface  $db  The database
  83       *
  84       * @since   4.2.0
  85       */
  86      public function __construct(DatabaseInterface $db)
  87      {
  88          parent::__construct();
  89  
  90          $this->setDatabase($db);
  91      }
  92  
  93      /**
  94       * Internal function to execute the command.
  95       *
  96       * @param   InputInterface   $input   The input to inject into the command.
  97       * @param   OutputInterface  $output  The output to inject into the command.
  98       *
  99       * @return  integer  The command exit code
 100       *
 101       * @since   4.0.0
 102       */
 103      protected function doExecute(InputInterface $input, OutputInterface $output): int
 104      {
 105          $this->configureIO($input, $output);
 106          $this->ioStyle->title('Add User To Group');
 107          $this->username = $this->getStringFromOption('username', 'Please enter a username');
 108  
 109          $userId = $this->getUserId($this->username);
 110  
 111          if (empty($userId)) {
 112              $this->ioStyle->error("The user " . $this->username . " does not exist!");
 113  
 114              return Command::FAILURE;
 115          }
 116  
 117          // Fetch user
 118          $user = User::getInstance($userId);
 119  
 120          $this->userGroups = $this->getGroups($user);
 121  
 122          $db    = $this->getDatabase();
 123          $query = $db->getQuery(true)
 124              ->select($db->quoteName('title'))
 125              ->from($db->quoteName('#__usergroups'))
 126              ->where($db->quoteName('id') . ' = :userGroup');
 127  
 128          foreach ($this->userGroups as $userGroup) {
 129              $query->bind(':userGroup', $userGroup);
 130              $db->setQuery($query);
 131  
 132              $result = $db->loadResult();
 133  
 134              if (UserHelper::addUserToGroup($user->id, $userGroup)) {
 135                  $this->ioStyle->success("Added '" . $user->username . "' to group '" . $result . "'!");
 136              } else {
 137                  $this->ioStyle->error("Can't add '" . $user->username . "' to group '" . $result . "'!");
 138  
 139                  return Command::FAILURE;
 140              }
 141          }
 142  
 143          return Command::SUCCESS;
 144      }
 145  
 146  
 147      /**
 148       * Method to get a value from option
 149       *
 150       * @param   User  $user  a UserInstance
 151       *
 152       * @return  array
 153       *
 154       * @since   4.0.0
 155       */
 156      protected function getGroups($user): array
 157      {
 158          $groups = $this->getApplication()->getConsoleInput()->getOption('group');
 159  
 160          $db = $this->getDatabase();
 161  
 162          $groupList = [];
 163  
 164          // Group names have been supplied as input arguments
 165          if ($groups) {
 166              $groups = explode(',', $groups);
 167  
 168              foreach ($groups as $group) {
 169                  $groupId = $this->getGroupId($group);
 170  
 171                  if (empty($groupId)) {
 172                      $this->ioStyle->error("Invalid group name '" . $group . "'");
 173                      throw new InvalidOptionException("Invalid group name " . $group);
 174                  }
 175  
 176                  $groupList[] = $this->getGroupId($group);
 177              }
 178  
 179              return $groupList;
 180          }
 181  
 182          $userGroups = Access::getGroupsByUser($user->id, false);
 183  
 184          // Generate select list for user
 185          $query = $db->getQuery(true)
 186              ->select($db->quoteName('title'))
 187              ->from($db->quoteName('#__usergroups'))
 188              ->whereNotIn($db->quoteName('id'), $userGroups)
 189              ->order($db->quoteName('id') . ' ASC');
 190          $db->setQuery($query);
 191  
 192          $list = $db->loadColumn();
 193  
 194          $choice = new ChoiceQuestion(
 195              'Please select a usergroup (separate multiple groups with a comma)',
 196              $list
 197          );
 198          $choice->setMultiselect(true);
 199  
 200          $answer = (array) $this->ioStyle->askQuestion($choice);
 201  
 202          foreach ($answer as $group) {
 203              $groupList[] = $this->getGroupId($group);
 204          }
 205  
 206          return $groupList;
 207      }
 208  
 209      /**
 210       * Method to get groupId by groupName
 211       *
 212       * @param   string  $groupName  name of group
 213       *
 214       * @return  integer
 215       *
 216       * @since   4.0.0
 217       */
 218      protected function getGroupId($groupName)
 219      {
 220          $db    = $this->getDatabase();
 221          $query = $db->getQuery(true)
 222              ->select($db->quoteName('id'))
 223              ->from($db->quoteName('#__usergroups'))
 224              ->where($db->quoteName('title') . '= :groupName')
 225              ->bind(':groupName', $groupName);
 226          $db->setQuery($query);
 227  
 228          return $db->loadResult();
 229      }
 230  
 231      /**
 232       * Method to get a user object
 233       *
 234       * @param   string  $username  username
 235       *
 236       * @return  object
 237       *
 238       * @since   4.0.0
 239       */
 240      protected function getUserId($username)
 241      {
 242          $db    = $this->getDatabase();
 243          $query = $db->getQuery(true)
 244              ->select($db->quoteName('id'))
 245              ->from($db->quoteName('#__users'))
 246              ->where($db->quoteName('username') . '= :username')
 247              ->bind(':username', $username);
 248          $db->setQuery($query);
 249  
 250          return $db->loadResult();
 251      }
 252  
 253      /**
 254       * Method to get a value from option
 255       *
 256       * @param   string  $option    set the option name
 257       *
 258       * @param   string  $question  set the question if user enters no value to option
 259       *
 260       * @return  string
 261       *
 262       * @since   4.0.0
 263       */
 264      protected function getStringFromOption($option, $question): string
 265      {
 266          $answer = (string) $this->getApplication()->getConsoleInput()->getOption($option);
 267  
 268          while (!$answer) {
 269              $answer = (string) $this->ioStyle->ask($question);
 270          }
 271  
 272          return $answer;
 273      }
 274  
 275      /**
 276       * Configure the IO.
 277       *
 278       * @param   InputInterface   $input   The input to inject into the command.
 279       * @param   OutputInterface  $output  The output to inject into the command.
 280       *
 281       * @return  void
 282       *
 283       * @since   4.0.0
 284       */
 285      private function configureIO(InputInterface $input, OutputInterface $output)
 286      {
 287          $this->cliInput = $input;
 288          $this->ioStyle = new SymfonyStyle($input, $output);
 289      }
 290  
 291      /**
 292       * Configure the command.
 293       *
 294       * @return  void
 295       *
 296       * @since   4.0.0
 297       */
 298      protected function configure(): void
 299      {
 300          $help = "<info>%command.name%</info> adds a user to a group
 301          \nUsage: <info>php %command.full_name%</info>";
 302  
 303          $this->setDescription('Add a user to a group');
 304          $this->addOption('username', null, InputOption::VALUE_OPTIONAL, 'username');
 305          $this->addOption('group', null, InputOption::VALUE_OPTIONAL, 'group');
 306          $this->setHelp($help);
 307      }
 308  }


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