Security update for Core, with self-updated composer
[yaffs-website] / vendor / drupal / console / src / Command / Create / UsersCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Create\UsersCommand.
6  */
7
8 namespace Drupal\Console\Command\Create;
9
10 use Symfony\Component\Console\Input\InputArgument;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use Drupal\Console\Core\Command\Command;
15 use Drupal\Console\Command\Shared\CreateTrait;
16 use Drupal\Console\Utils\Create\UserData;
17 use Drupal\Console\Utils\DrupalApi;
18
19 /**
20  * Class UsersCommand
21  *
22  * @package Drupal\Console\Command\Create
23  */
24 class UsersCommand extends Command
25 {
26     use CreateTrait;
27
28     /**
29      * @var DrupalApi
30      */
31     protected $drupalApi;
32     /**
33      * @var UserData
34      */
35     protected $createUserData;
36
37     /**
38      * UsersCommand constructor.
39      *
40      * @param DrupalApi $drupalApi
41      * @param UserData  $createUserData
42      */
43     public function __construct(
44         DrupalApi $drupalApi,
45         UserData $createUserData
46     ) {
47         $this->drupalApi = $drupalApi;
48         $this->createUserData = $createUserData;
49         parent::__construct();
50     }
51
52     /**
53      * {@inheritdoc}
54      */
55     protected function configure()
56     {
57         $this
58             ->setName('create:users')
59             ->setDescription($this->trans('commands.create.users.description'))
60             ->addArgument(
61                 'roles',
62                 InputArgument::IS_ARRAY,
63                 $this->trans('commands.create.users.arguments.roles')
64             )
65             ->addOption(
66                 'limit',
67                 null,
68                 InputOption::VALUE_OPTIONAL,
69                 $this->trans('commands.create.users.options.limit')
70             )
71             ->addOption(
72                 'password',
73                 null,
74                 InputOption::VALUE_OPTIONAL,
75                 $this->trans('commands.create.users.options.password')
76             )
77             ->addOption(
78                 'time-range',
79                 null,
80                 InputOption::VALUE_OPTIONAL,
81                 $this->trans('commands.create.users.options.time-range')
82             )->setAliases(['cru']);
83     }
84
85     /**
86      * {@inheritdoc}
87      */
88     protected function interact(InputInterface $input, OutputInterface $output)
89     {
90         $rids = $input->getArgument('roles');
91         if (!$rids) {
92             $roles = $this->drupalApi->getRoles();
93             $rids = $this->getIo()->choice(
94                 $this->trans('commands.create.users.questions.roles'),
95                 array_values($roles),
96                 null,
97                 true
98             );
99
100             $rids = array_map(
101                 function ($role) use ($roles) {
102                     return array_search($role, $roles);
103                 },
104                 $rids
105             );
106
107             $input->setArgument('roles', $rids);
108         }
109
110         $limit = $input->getOption('limit');
111         if (!$limit) {
112             $limit = $this->getIo()->ask(
113                 $this->trans('commands.create.users.questions.limit'),
114                 10
115             );
116             $input->setOption('limit', $limit);
117         }
118
119         $password = $input->getOption('password');
120         if (!$password) {
121             $password = $this->getIo()->ask(
122                 $this->trans('commands.create.users.questions.password'),
123                 5
124             );
125
126             $input->setOption('password', $password);
127         }
128
129         $timeRange = $input->getOption('time-range');
130         if (!$timeRange) {
131             $timeRanges = $this->getTimeRange();
132
133             $timeRange = $this->getIo()->choice(
134                 $this->trans('commands.create.nodes.questions.time-range'),
135                 array_values($timeRanges)
136             );
137
138             $input->setOption('time-range', array_search($timeRange, $timeRanges));
139         }
140     }
141
142     /**
143      * {@inheritdoc}
144      */
145     protected function execute(InputInterface $input, OutputInterface $output)
146     {
147         $roles = $input->getArgument('roles');
148         $limit = $input->getOption('limit')?:25;
149         $password = $input->getOption('password');
150         $timeRange = $input->getOption('time-range')?:31536000;
151
152         if (!$roles) {
153             $roles = $this->drupalApi->getRoles();
154         }
155
156         $result = $this->createUserData->create(
157             $roles,
158             $limit,
159             $password,
160             $timeRange
161         );
162
163         $tableHeader = [
164           $this->trans('commands.create.users.messages.user-id'),
165           $this->trans('commands.create.users.messages.username'),
166           $this->trans('commands.create.users.messages.roles'),
167           $this->trans('commands.create.users.messages.created'),
168         ];
169
170         if ($result['success']) {
171             $this->getIo()->table($tableHeader, $result['success']);
172
173             $this->getIo()->success(
174                 sprintf(
175                     $this->trans('commands.create.users.messages.created-users'),
176                     count($result['success'])
177                 )
178             );
179         }
180
181         if (isset($result['error'])) {
182             foreach ($result['error'] as $error) {
183                 $this->getIo()->error(
184                     sprintf(
185                         $this->trans('commands.create.users.messages.error'),
186                         $error
187                     )
188                 );
189             }
190         }
191
192         return 0;
193     }
194 }