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