5 * Contains \Drupal\Console\Command\User\DebugCommand.
8 namespace Drupal\Console\Command\Debug;
10 use Symfony\Component\Console\Input\InputOption;
11 use Symfony\Component\Console\Input\InputInterface;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Drupal\Console\Core\Command\Command;
14 use Drupal\Core\Entity\EntityTypeManagerInterface;
15 use Drupal\Core\Entity\Query\QueryFactory;
16 use Drupal\Console\Utils\DrupalApi;
21 * @package Drupal\Console\Command\Debug
23 class UserCommand extends Command
26 * @var EntityTypeManagerInterface
28 protected $entityTypeManager;
33 protected $entityQuery;
41 * DebugCommand constructor.
43 * @param EntityTypeManagerInterface $entityTypeManager
44 * @param QueryFactory $entityQuery
45 * @param DrupalApi $drupalApi
47 public function __construct(
48 EntityTypeManagerInterface $entityTypeManager,
49 QueryFactory $entityQuery,
52 $this->entityTypeManager = $entityTypeManager;
53 $this->entityQuery = $entityQuery;
54 $this->drupalApi = $drupalApi;
55 parent::__construct();
61 protected function configure()
64 ->setName('debug:user')
65 ->setDescription($this->trans('commands.debug.user.description'))
69 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
70 $this->trans('commands.debug.user.options.uid')
75 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
76 $this->trans('commands.debug.user.options.username')
81 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
82 $this->trans('commands.debug.user.options.mail')
87 InputOption::VALUE_OPTIONAL | InputOption::VALUE_OPTIONAL,
88 $this->trans('commands.debug.user.options.roles')
93 InputOption::VALUE_OPTIONAL,
94 $this->trans('commands.debug.user.options.limit')
95 )->setAliases(['dus']);
101 protected function execute(InputInterface $input, OutputInterface $output)
103 $roles = $input->getOption('roles');
104 $limit = $input->getOption('limit');
106 $uids = $this->splitOption($input->getOption('uid'));
107 $usernames = $this->splitOption($input->getOption('username'));
108 $mails = $this->splitOption($input->getOption('mail'));
110 $userStorage = $this->entityTypeManager->getStorage('user');
111 $systemRoles = $this->drupalApi->getRoles();
113 $query = $this->entityQuery->get('user');
114 $query->condition('uid', 0, '>');
119 if (is_array($uids) && $uids) {
120 $group = $query->andConditionGroup()
121 ->condition('uid', $uids, 'IN');
122 $query->condition($group);
125 // username as option
126 if (is_array($usernames) && $usernames) {
127 $group = $query->andConditionGroup()
128 ->condition('name', $usernames, 'IN');
129 $query->condition($group);
133 if (is_array($mails) && $mails) {
134 $group = $query->andConditionGroup()
135 ->condition('mail', $mails, 'IN');
136 $query->condition($group);
140 $query->condition('roles', is_array($roles)?$roles:[$roles], 'IN');
144 $query->range(0, $limit);
147 $results = $query->execute();
148 $users = $userStorage->loadMultiple($results);
151 $this->trans('commands.debug.user.messages.user-id'),
152 $this->trans('commands.debug.user.messages.username'),
153 $this->trans('commands.debug.user.messages.roles'),
154 $this->trans('commands.debug.user.messages.status'),
158 foreach ($users as $userId => $user) {
160 foreach ($user->getRoles() as $userRole) {
161 if ($systemRoles[$userRole]) {
162 $userRoles[] = $systemRoles[$userRole];
166 $status = $user->isActive()?$this->trans('commands.common.status.enabled'):$this->trans('commands.common.status.disabled');
169 $user->getUsername(),
170 implode(', ', $userRoles),
175 $this->getIo()->table($tableHeader, $tableRows);
178 //@TODO: this should be in src/Command/Shared/CommandTrait.php
179 public function splitOption($option)
181 if (1 == count($option) && strpos($option[0], " ") >= 1) {
182 return explode(" ", $option[0]);