[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Console/ -> ListUserCommand.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\Factory;
  13  use Joomla\Console\Command\AbstractCommand;
  14  use Joomla\Database\DatabaseAwareTrait;
  15  use Joomla\Database\DatabaseInterface;
  16  use Symfony\Component\Console\Command\Command;
  17  use Symfony\Component\Console\Input\InputInterface;
  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 list existing users
  27   *
  28   * @since  4.0.0
  29   */
  30  class ListUserCommand extends AbstractCommand
  31  {
  32      use DatabaseAwareTrait;
  33  
  34      /**
  35       * The default command name
  36       *
  37       * @var    string
  38       * @since  4.0.0
  39       */
  40      protected static $defaultName = 'user:list';
  41  
  42      /**
  43       * SymfonyStyle Object
  44       * @var object
  45       * @since 4.0.0
  46       */
  47      private $ioStyle;
  48  
  49      /**
  50       * Command constructor.
  51       *
  52       * @param   DatabaseInterface  $db  The database
  53       *
  54       * @since   4.2.0
  55       */
  56      public function __construct(DatabaseInterface $db)
  57      {
  58          parent::__construct();
  59  
  60          $this->setDatabase($db);
  61      }
  62  
  63      /**
  64       * Internal function to execute the command.
  65       *
  66       * @param   InputInterface   $input   The input to inject into the command.
  67       * @param   OutputInterface  $output  The output to inject into the command.
  68       *
  69       * @return  integer  The command exit code
  70       *
  71       * @since   4.0.0
  72       */
  73      protected function doExecute(InputInterface $input, OutputInterface $output): int
  74      {
  75          $db = $this->getDatabase();
  76  
  77          $this->configureIO($input, $output);
  78          $this->ioStyle->title('List Users');
  79  
  80          $groupsQuery = $db->getQuery(true)
  81              ->select($db->quoteName(['title', 'id']))
  82              ->from($db->quoteName('#__usergroups'));
  83  
  84          $groups = $db->setQuery($groupsQuery)->loadAssocList('id', 'title');
  85  
  86          $query = $db->getQuery(true);
  87          $query->select($db->quoteName(['u.id', 'u.username', 'u.name', 'u.email', 'u.block']))
  88              ->select($query->groupConcat($query->castAs('CHAR', $db->quoteName('g.group_id'))) . ' AS ' . $db->quoteName('groups'))
  89              ->innerJoin($db->quoteName('#__user_usergroup_map', 'g'), $db->quoteName('g.user_id') . ' = ' . $db->quoteName('u.id'))
  90              ->from($db->quoteName('#__users', 'u'))
  91              ->group($db->quoteName('u.id'));
  92          $db->setQuery($query);
  93  
  94          $users = [];
  95  
  96          foreach ($db->loadAssocList() as $user) {
  97              $user["groups"] = array_map(
  98                  function ($groupId) use ($groups) {
  99                      return $groups[$groupId];
 100                  },
 101                  explode(",", $user["groups"])
 102              );
 103  
 104              $user["groups"] = implode(", ", $user["groups"]);
 105              $users[] = $user;
 106          }
 107  
 108          $this->ioStyle->table(['ID', 'Username', 'Name', 'Email', 'Blocked', 'Groups'], $users);
 109  
 110          return Command::SUCCESS;
 111      }
 112  
 113      /**
 114       * Configure the IO.
 115       *
 116       * @param   InputInterface   $input   The input to inject into the command.
 117       * @param   OutputInterface  $output  The output to inject into the command.
 118       *
 119       * @return  void
 120       *
 121       * @since   4.0.0
 122       */
 123      private function configureIO(InputInterface $input, OutputInterface $output)
 124      {
 125          $this->ioStyle = new SymfonyStyle($input, $output);
 126      }
 127  
 128      /**
 129       * Configure the command.
 130       *
 131       * @return  void
 132       *
 133       * @since   4.0.0
 134       */
 135      protected function configure(): void
 136      {
 137          $help = "<info>%command.name%</info> will list all users
 138          \nUsage: <info>php %command.full_name%</info>";
 139  
 140          $this->setDescription('List all users');
 141          $this->setHelp($help);
 142      }
 143  }


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