Security update for Core, with self-updated composer
[yaffs-website] / vendor / drupal / console / src / Command / Debug / UserCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\User\DebugCommand.
6  */
7
8 namespace Drupal\Console\Command\Debug;
9
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;
17
18 /**
19  * Class UserCommand
20  *
21  * @package Drupal\Console\Command\Debug
22  */
23 class UserCommand extends Command
24 {
25     /**
26      * @var EntityTypeManagerInterface
27      */
28     protected $entityTypeManager;
29
30     /**
31      * @var QueryFactory
32      */
33     protected $entityQuery;
34
35     /**
36      * @var DrupalApi
37      */
38     protected $drupalApi;
39
40     /**
41      * DebugCommand constructor.
42      *
43      * @param EntityTypeManagerInterface $entityTypeManager
44      * @param QueryFactory               $entityQuery
45      * @param DrupalApi                  $drupalApi
46      */
47     public function __construct(
48         EntityTypeManagerInterface $entityTypeManager,
49         QueryFactory $entityQuery,
50         DrupalApi $drupalApi
51     ) {
52         $this->entityTypeManager = $entityTypeManager;
53         $this->entityQuery = $entityQuery;
54         $this->drupalApi = $drupalApi;
55         parent::__construct();
56     }
57
58     /**
59      * {@inheritdoc}
60      */
61     protected function configure()
62     {
63         $this
64             ->setName('debug:user')
65             ->setDescription($this->trans('commands.debug.user.description'))
66             ->addOption(
67                 'uid',
68                 null,
69                 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
70                 $this->trans('commands.debug.user.options.uid')
71             )
72             ->addOption(
73                 'username',
74                 null,
75                 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
76                 $this->trans('commands.debug.user.options.username')
77             )
78             ->addOption(
79                 'mail',
80                 null,
81                 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
82                 $this->trans('commands.debug.user.options.mail')
83             )
84             ->addOption(
85                 'roles',
86                 null,
87                 InputOption::VALUE_OPTIONAL | InputOption::VALUE_OPTIONAL,
88                 $this->trans('commands.debug.user.options.roles')
89             )
90             ->addOption(
91                 'limit',
92                 null,
93                 InputOption::VALUE_OPTIONAL,
94                 $this->trans('commands.debug.user.options.limit')
95             )->setAliases(['dus']);
96     }
97
98     /**
99      * {@inheritdoc}
100      */
101     protected function execute(InputInterface $input, OutputInterface $output)
102     {
103         $roles = $input->getOption('roles');
104         $limit = $input->getOption('limit');
105
106         $uids = $this->splitOption($input->getOption('uid'));
107         $usernames = $this->splitOption($input->getOption('username'));
108         $mails = $this->splitOption($input->getOption('mail'));
109
110         $userStorage = $this->entityTypeManager->getStorage('user');
111         $systemRoles = $this->drupalApi->getRoles();
112
113         $query = $this->entityQuery->get('user');
114         $query->condition('uid', 0, '>');
115         $query->sort('uid');
116
117
118         // uid as option
119         if (is_array($uids) && $uids) {
120             $group = $query->andConditionGroup()
121                 ->condition('uid', $uids, 'IN');
122             $query->condition($group);
123         }
124
125         // username as option
126         if (is_array($usernames) && $usernames) {
127             $group = $query->andConditionGroup()
128                 ->condition('name', $usernames, 'IN');
129             $query->condition($group);
130         }
131
132         // mail as option
133         if (is_array($mails) && $mails) {
134             $group = $query->andConditionGroup()
135                 ->condition('mail', $mails, 'IN');
136             $query->condition($group);
137         }
138
139         if ($roles) {
140             $query->condition('roles', is_array($roles)?$roles:[$roles], 'IN');
141         }
142
143         if ($limit) {
144             $query->range(0, $limit);
145         }
146
147         $results = $query->execute();
148         $users = $userStorage->loadMultiple($results);
149
150         $tableHeader = [
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'),
155         ];
156
157         $tableRows = [];
158         foreach ($users as $userId => $user) {
159             $userRoles = [];
160             foreach ($user->getRoles() as $userRole) {
161                 if ($systemRoles[$userRole]) {
162                     $userRoles[] = $systemRoles[$userRole];
163                 }
164             }
165
166             $status = $user->isActive()?$this->trans('commands.common.status.enabled'):$this->trans('commands.common.status.disabled');
167             $tableRows[] = [
168                 $userId,
169                 $user->getUsername(),
170                 implode(', ', $userRoles),
171                 $status
172             ];
173         }
174
175         $this->getIo()->table($tableHeader, $tableRows);
176     }
177
178     //@TODO: this should be in src/Command/Shared/CommandTrait.php
179     public function splitOption($option)
180     {
181         if (1 == count($option) && strpos($option[0], " ") >= 1) {
182             return explode(" ", $option[0]);
183         } else {
184             return $option;
185         }
186     }
187 }