5 * Contains \Drupal\Console\Command\User\DebugCommand.
8 namespace Drupal\Console\Command\User;
10 use Symfony\Component\Console\Input\InputOption;
11 use Symfony\Component\Console\Input\InputInterface;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Symfony\Component\Console\Command\Command;
14 use Drupal\Console\Core\Command\Shared\CommandTrait;
15 use Drupal\Core\Entity\EntityTypeManagerInterface;
16 use Drupal\Core\Entity\Query\QueryFactory;
17 use Drupal\Console\Core\Style\DrupalStyle;
18 use Drupal\Console\Utils\DrupalApi;
23 * @package Drupal\Console\Command\User
25 class DebugCommand extends Command
30 * @var EntityTypeManagerInterface
32 protected $entityTypeManager;
37 protected $entityQuery;
45 * DebugCommand constructor.
47 * @param EntityTypeManagerInterface $entityTypeManager
48 * @param QueryFactory $entityQuery
49 * @param DrupalApi $drupalApi
51 public function __construct(
52 EntityTypeManagerInterface $entityTypeManager,
53 QueryFactory $entityQuery,
56 $this->entityTypeManager = $entityTypeManager;
57 $this->entityQuery = $entityQuery;
58 $this->drupalApi = $drupalApi;
59 parent::__construct();
65 protected function configure()
68 ->setName('user:debug')
69 ->setDescription($this->trans('commands.user.debug.description'))
73 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
74 $this->trans('commands.user.debug.options.uid')
79 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
80 $this->trans('commands.user.debug.options.username')
85 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
86 $this->trans('commands.user.debug.options.mail')
91 InputOption::VALUE_OPTIONAL | InputOption::VALUE_OPTIONAL,
92 $this->trans('commands.user.debug.options.roles')
97 InputOption::VALUE_OPTIONAL,
98 $this->trans('commands.user.debug.options.limit')
105 protected function execute(InputInterface $input, OutputInterface $output)
107 $io = new DrupalStyle($input, $output);
108 $roles = $input->getOption('roles');
109 $limit = $input->getOption('limit');
111 $uids = $this->splitOption($input->getOption('uid'));
112 $usernames = $this->splitOption($input->getOption('username'));
113 $mails = $this->splitOption($input->getOption('mail'));
115 $userStorage = $this->entityTypeManager->getStorage('user');
116 $systemRoles = $this->drupalApi->getRoles();
118 $query = $this->entityQuery->get('user');
119 $query->condition('uid', 0, '>');
124 if (is_array($uids) && $uids) {
125 $group = $query->andConditionGroup()
126 ->condition('uid', $uids, 'IN');
127 $query->condition($group);
130 // username as option
131 if (is_array($usernames) && $usernames) {
132 $group = $query->andConditionGroup()
133 ->condition('name', $usernames, 'IN');
134 $query->condition($group);
138 if (is_array($mails) && $mails) {
139 $group = $query->andConditionGroup()
140 ->condition('mail', $mails, 'IN');
141 $query->condition($group);
145 $query->condition('roles', is_array($roles)?$roles:[$roles], 'IN');
149 $query->range(0, $limit);
152 $results = $query->execute();
153 $users = $userStorage->loadMultiple($results);
156 $this->trans('commands.user.debug.messages.user-id'),
157 $this->trans('commands.user.debug.messages.username'),
158 $this->trans('commands.user.debug.messages.roles'),
159 $this->trans('commands.user.debug.messages.status'),
163 foreach ($users as $userId => $user) {
165 foreach ($user->getRoles() as $userRole) {
166 $userRoles[] = $systemRoles[$userRole];
169 $status = $user->isActive()?$this->trans('commands.common.status.enabled'):$this->trans('commands.common.status.disabled');
170 $tableRows[] = [$userId, $user->getUsername(), implode(', ', $userRoles), $status];
173 $io->table($tableHeader, $tableRows);
176 //@TODO: this should be in src/Command/Shared/CommandTrait.php
177 public function splitOption($option)
179 if (1 == count($option) && strpos($option[0], " ") >= 1) {
180 return explode(" ", $option[0]);