[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Console/ -> RemoveUserFromGroupCommand.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 remove a user from a group
  32   *
  33   * @since  4.0.0
  34   */
  35  class RemoveUserFromGroupCommand 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:removefromgroup';
  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 = array();
  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('Remove User From Group');
 107          $this->username = $this->getStringFromOption('username', 'Please enter a username');
 108  
 109          $userId = UserHelper::getUserId($this->username);
 110  
 111          if (empty($userId)) {
 112              $this->ioStyle->error("The user " . $this->username . " does not exist!");
 113  
 114              return 1;
 115          }
 116  
 117          $user = User::getInstance($userId);
 118  
 119          $this->userGroups = $this->getGroups($user);
 120  
 121          $db    = $this->getDatabase();
 122          $query = $db->getQuery(true)
 123              ->select($db->quoteName('title'))
 124              ->from($db->quoteName('#__usergroups'))
 125              ->where($db->quoteName('id') . ' = :userGroup');
 126  
 127          foreach ($this->userGroups as $userGroup) {
 128              $query->bind(':userGroup', $userGroup);
 129              $db->setQuery($query);
 130  
 131              $result = $db->loadResult();
 132  
 133              if (Access::checkGroup($userGroup, 'core.admin')) {
 134                  $queryUser = $db->getQuery(true);
 135                  $queryUser->select('COUNT(*)')
 136                      ->from($db->quoteName('#__users', 'u'))
 137                      ->leftJoin(
 138                          $db->quoteName('#__user_usergroup_map', 'g'),
 139                          '(' . $db->quoteName('u.id') . ' = ' . $db->quoteName('g.user_id') . ')'
 140                      )
 141                      ->where($db->quoteName('g.group_id') . " = :groupId")
 142                      ->where($db->quoteName('u.block') . " = 0")
 143                      ->bind(':groupId', $userGroup);
 144  
 145                  $db->setQuery($queryUser);
 146                  $activeSuperUser = $db->loadResult();
 147  
 148                  if ($activeSuperUser < 2) {
 149                      $this->ioStyle->error("Can't remove user '" . $user->username . "' from group '" . $result . "'! "
 150                          . $result . " needs at least one active user!");
 151  
 152                      return Command::FAILURE;
 153                  }
 154              }
 155  
 156              if (\count(Access::getGroupsByUser($user->id, false)) < 2) {
 157                  $this->ioStyle->error("Can't remove '" . $user->username . "' from group '" . $result
 158                      . "'! Every user needs to be a member of at least one group");
 159  
 160                  return Command::FAILURE;
 161              }
 162  
 163              if (!UserHelper::removeUserFromGroup($user->id, $userGroup)) {
 164                  $this->ioStyle->error("Can't remove '" . $user->username . "' from group '" . $result . "'!");
 165  
 166                  return Command::FAILURE;
 167              }
 168  
 169              $this->ioStyle->success("Removed '" . $user->username . "' from group '" . $result . "'!");
 170          }
 171  
 172          return Command::SUCCESS;
 173      }
 174  
 175      /**
 176       * Method to get a value from option
 177       *
 178       * @param   object  $user  user object
 179       *
 180       * @return  array
 181       *
 182       * @since   4.0.0
 183       */
 184      protected function getGroups($user): array
 185      {
 186          $option     = $this->getApplication()->getConsoleInput()->getOption('group');
 187          $db         = $this->getDatabase();
 188          $userGroups = Access::getGroupsByUser($user->id, false);
 189  
 190          if (!$option) {
 191              $query = $db->getQuery(true)
 192                  ->select($db->quoteName('title'))
 193                  ->from($db->quoteName('#__usergroups'))
 194                  ->whereIn($db->quoteName('id'), $userGroups);
 195              $db->setQuery($query);
 196  
 197              $result = $db->loadColumn();
 198  
 199              $choice = new ChoiceQuestion(
 200                  'Please select a usergroup (separate multiple groups with a comma)',
 201                  $result
 202              );
 203              $choice->setMultiselect(true);
 204  
 205              $answer = (array) $this->ioStyle->askQuestion($choice);
 206  
 207              $groupList = [];
 208  
 209              foreach ($answer as $group) {
 210                  $groupList[] = $this->getGroupId($group);
 211              }
 212  
 213              return $groupList;
 214          }
 215  
 216          $groupList = [];
 217          $option = explode(',', $option);
 218  
 219          foreach ($option as $group) {
 220              $groupId = $this->getGroupId($group);
 221  
 222              if (empty($groupId)) {
 223                  $this->ioStyle->error("Invalid group name '" . $group . "'");
 224                  throw new InvalidOptionException("Invalid group name " . $group);
 225              }
 226  
 227              $groupList[] = $this->getGroupId($group);
 228          }
 229  
 230          return $groupList;
 231      }
 232  
 233      /**
 234       * Method to get groupId by groupName
 235       *
 236       * @param   string  $groupName  name of group
 237       *
 238       * @return  integer
 239       *
 240       * @since   4.0.0
 241       */
 242      protected function getGroupId($groupName)
 243      {
 244          $db    = $this->getDatabase();
 245          $query = $db->getQuery(true)
 246              ->select($db->quoteName('id'))
 247              ->from($db->quoteName('#__usergroups'))
 248              ->where($db->quoteName('title') . '= :groupName')
 249              ->bind(':groupName', $groupName);
 250          $db->setQuery($query);
 251  
 252          return $db->loadResult();
 253      }
 254  
 255      /**
 256       * Method to get a value from option
 257       *
 258       * @param   string  $option    set the option name
 259       *
 260       * @param   string  $question  set the question if user enters no value to option
 261       *
 262       * @return  string
 263       *
 264       * @since   4.0.0
 265       */
 266      protected function getStringFromOption($option, $question): string
 267      {
 268          $answer = (string) $this->getApplication()->getConsoleInput()->getOption($option);
 269  
 270          while (!$answer) {
 271              $answer = (string) $this->ioStyle->ask($question);
 272          }
 273  
 274          return $answer;
 275      }
 276  
 277      /**
 278       * Configure the IO.
 279       *
 280       * @param   InputInterface   $input   The input to inject into the command.
 281       * @param   OutputInterface  $output  The output to inject into the command.
 282       *
 283       * @return  void
 284       *
 285       * @since   4.0.0
 286       */
 287      private function configureIO(InputInterface $input, OutputInterface $output)
 288      {
 289          $this->cliInput = $input;
 290          $this->ioStyle = new SymfonyStyle($input, $output);
 291      }
 292  
 293      /**
 294       * Configure the command.
 295       *
 296       * @return  void
 297       *
 298       * @since   4.0.0
 299       */
 300      protected function configure(): void
 301      {
 302          $help = "<info>%command.name%</info> removes a user from a group
 303          \nUsage: <info>php %command.full_name%</info>";
 304  
 305          $this->setDescription('Remove a user from a group');
 306          $this->addOption('username', null, InputOption::VALUE_OPTIONAL, 'username');
 307          $this->addOption('group', null, InputOption::VALUE_OPTIONAL, 'group');
 308          $this->setHelp($help);
 309      }
 310  }


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