Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / User / DeleteCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\User\DeleteCommand.
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 DeleteCommand
22  *
23  * @package Drupal\Console\Command\User
24  */
25 class DeleteCommand 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      * DeleteCommand 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:delete')
69             ->setDescription($this->trans('commands.user.delete.description'))
70             ->addOption(
71                 'user-id',
72                 null,
73                 InputOption::VALUE_OPTIONAL,
74                 $this->trans('commands.user.delete.options.user-id')
75             )
76             ->addOption(
77                 'roles',
78                 null,
79                 InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL,
80                 $this->trans('commands.user.delete.options.roles')
81             );
82     }
83
84     /**
85      * {@inheritdoc}
86      */
87     protected function interact(InputInterface $input, OutputInterface $output)
88     {
89         $io = new DrupalStyle($input, $output);
90
91         $userId = $input->getOption('user-id');
92         if (!$userId) {
93             $userId = $io->askEmpty(
94                 $this->trans('commands.user.delete.questions.user-id'),
95                 null
96             );
97             $input->setOption('user-id', $userId);
98         }
99
100         $roles = $input->getOption('roles');
101
102         if (!$userId && !$roles) {
103             $systemRoles = $this->drupalApi->getRoles(false, false, false);
104             $roles = $io->choice(
105                 $this->trans('commands.user.delete.questions.roles'),
106                 array_values($systemRoles),
107                 null,
108                 true
109             );
110
111             $roles = array_map(
112                 function ($role) use ($systemRoles) {
113                     return array_search($role, $systemRoles);
114                 },
115                 $roles
116             );
117
118             $input->setOption('roles', $roles);
119         }
120     }
121
122     /**
123      * {@inheritdoc}
124      */
125     protected function execute(InputInterface $input, OutputInterface $output)
126     {
127         $io = new DrupalStyle($input, $output);
128
129         $userId = $input->getOption('user-id');
130
131         if ($userId && $userId <= 1) {
132             $io->error(
133                 sprintf(
134                     $this->trans('commands.user.delete.errors.invalid-user-id'),
135                     $userId
136                 )
137             );
138
139             return 1;
140         }
141
142         if ($userId) {
143             $user = $this->entityTypeManager
144                 ->getStorage('user')
145                 ->load($userId);
146
147             if (!$user) {
148                 $io->error(
149                     sprintf(
150                         $this->trans('commands.user.delete.errors.invalid-user'),
151                         $userId
152                     )
153                 );
154
155                 return 1;
156             }
157
158             try {
159                 $user->delete();
160                 $io->info(
161                     sprintf(
162                         $this->trans('commands.user.delete.messages.user-deleted'),
163                         $user->getUsername()
164                     )
165                 );
166             } catch (\Exception $e) {
167                 $io->error($e->getMessage());
168
169                 return 1;
170             }
171         }
172
173         $roles = $input->getOption('roles');
174
175         if ($roles) {
176             $userStorage = $this->entityTypeManager->getStorage('user');
177
178             $query = $this->entityQuery->get('user');
179             $query->condition('roles', is_array($roles)?$roles:[$roles], 'IN');
180             $query->condition('uid', 1, '>');
181             $results = $query->execute();
182
183             $users = $userStorage->loadMultiple($results);
184
185             $tableHeader = [
186               $this->trans('commands.user.debug.messages.user-id'),
187               $this->trans('commands.user.debug.messages.username'),
188             ];
189
190             $tableRows = [];
191             foreach ($users as $userId => $user) {
192                 try {
193                     $user->delete();
194                     $tableRows['success'][] = [$userId, $user->getUsername()];
195                 } catch (\Exception $e) {
196                     $tableRows['error'][] = [$userId, $user->getUsername()];
197                     $io->error($e->getMessage());
198
199                     return 1;
200                 }
201             }
202
203             if ($tableRows['success']) {
204                 $io->table($tableHeader, $tableRows['success']);
205                 $io->success(
206                     sprintf(
207                         $this->trans('commands.user.delete.messages.users-deleted'),
208                         count($tableRows['success'])
209                     )
210                 );
211             }
212         }
213     }
214 }