Security update for Core, with self-updated composer
[yaffs-website] / vendor / drupal / console / src / Command / User / CreateCommand.php
1 <?php
2 /**
3  * @file
4  * Contains \Drupal\Console\Command\User\CreateCommand.
5  */
6
7 namespace Drupal\Console\Command\User;
8
9 use Symfony\Component\Console\Input\InputInterface;
10 use Symfony\Component\Console\Input\InputArgument;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Drupal\Console\Core\Command\Command;
14 use Drupal\Core\Database\Connection;
15 use Drupal\Core\Entity\EntityTypeManagerInterface;
16 use Drupal\Core\Datetime\DateFormatterInterface;
17 use Drupal\Console\Utils\DrupalApi;
18 use Drupal\user\Entity\User;
19
20 class CreateCommand extends Command
21 {
22
23     /**
24      * @var Connection
25      */
26     protected $database;
27
28     /**
29      * @var EntityTypeManagerInterface
30      */
31     protected $entityTypeManager;
32
33     /**
34      * @var DateFormatterInterface
35      */
36     protected $dateFormatter;
37
38     /**
39      * @var DrupalApi
40      */
41     protected $drupalApi;
42
43     /**
44      * CreateCommand constructor.
45      *
46      * @param Connection                 $database
47      * @param EntityTypeManagerInterface $entityTypeManager
48      * @param DateFormatterInterface     $dateFormatter
49      * @param DrupalApi                  $drupalApi
50      */
51     public function __construct(
52         Connection $database,
53         EntityTypeManagerInterface $entityTypeManager,
54         DateFormatterInterface $dateFormatter,
55         DrupalApi $drupalApi
56     ) {
57         $this->database = $database;
58         $this->entityTypeManager = $entityTypeManager;
59         $this->dateFormatter = $dateFormatter;
60         $this->drupalApi = $drupalApi;
61         parent::__construct();
62     }
63
64     /**
65      * {@inheritdoc}
66      */
67     protected function configure()
68     {
69         $this
70             ->setName('user:create')
71             ->setDescription($this->trans('commands.user.create.description'))
72             ->setHelp($this->trans('commands.user.create.help'))
73             ->addArgument(
74                 'username',
75                 InputArgument::OPTIONAL,
76                 $this->trans('commands.user.create.options.username')
77             )
78             ->addArgument(
79                 'password',
80                 InputArgument::OPTIONAL,
81                 $this->trans('commands.user.create.options.password')
82             )
83             ->addOption(
84                 'roles',
85                 null,
86                 InputOption::VALUE_OPTIONAL,
87                 $this->trans('commands.user.create.options.roles')
88             )
89             ->addOption(
90                 'email',
91                 null,
92                 InputOption::VALUE_OPTIONAL,
93                 $this->trans('commands.user.create.options.email')
94             )
95             ->addOption(
96                 'status',
97                 null,
98                 InputOption::VALUE_OPTIONAL,
99                 $this->trans('commands.user.create.options.status')
100             )->setAliases(['uc']);
101     }
102
103     /**
104      * {@inheritdoc}
105      */
106     protected function execute(InputInterface $input, OutputInterface $output)
107     {
108         $username = $input->getArgument('username');
109         $password = $input->getArgument('password');
110         $roles = $input->getOption('roles');
111         $email = $input->getOption('email');
112         $status = $input->getOption('status');
113
114         $user = $this->createUser(
115             $username,
116             $password,
117             $roles,
118             $email,
119             $status
120         );
121
122         $tableHeader = ['Field', 'Value'];
123
124         $tableFields = [
125             $this->trans('commands.user.create.messages.user-id'),
126             $this->trans('commands.user.create.messages.username'),
127             $this->trans('commands.user.create.messages.password'),
128             $this->trans('commands.user.create.messages.email'),
129             $this->trans('commands.user.create.messages.roles'),
130             $this->trans('commands.user.create.messages.created'),
131             $this->trans('commands.user.create.messages.status'),
132         ];
133
134         if ($user['success']) {
135             $tableData = array_map(
136                 function ($field, $value) {
137                     return [$field, $value];
138                 },
139                 $tableFields,
140                 $user['success']
141             );
142
143             $this->getIo()->table($tableHeader, $tableData);
144
145             $this->getIo()->success(
146                 sprintf(
147                     $this->trans('commands.user.create.messages.user-created'),
148                     $user['success']['username']
149                 )
150             );
151
152             return 0;
153         }
154
155         if ($user['error']) {
156             $this->getIo()->error($user['error']['error']);
157
158             return 1;
159         }
160     }
161
162     /**
163      * {@inheritdoc}
164      */
165     protected function interact(InputInterface $input, OutputInterface $output)
166     {
167         $username = $input->getArgument('username');
168         if (!$username) {
169             $username = $this->getIo()->ask(
170                 $this->trans('commands.user.create.questions.username')
171             );
172
173             $input->setArgument('username', $username);
174         }
175
176         $password = $input->getArgument('password');
177         if (!$password) {
178             $password = $this->getIo()->askEmpty(
179                 $this->trans('commands.user.create.questions.password')
180             );
181
182             $input->setArgument('password', $password);
183         }
184
185         $roles = $input->getOption('roles');
186         if (!$roles) {
187             $systemRoles = $this->drupalApi->getRoles(false, false, false);
188             $roles = $this->getIo()->choice(
189                 $this->trans('commands.user.create.questions.roles'),
190                 array_values($systemRoles),
191                 null,
192                 true
193             );
194
195             $roles = array_map(
196                 function ($role) use ($systemRoles) {
197                     return array_search($role, $systemRoles);
198                 },
199                 $roles
200             );
201
202             $input->setOption('roles', $roles);
203         }
204
205         $email = $input->getOption('email');
206         if (!$email) {
207             $email = $this->getIo()->askEmpty(
208                 $this->trans('commands.user.create.questions.email')
209             );
210
211             $input->setOption('email', $email);
212         }
213
214         $status = $input->getOption('status');
215         if (!$status) {
216             $status = $this->getIo()->choice(
217                 $this->trans('commands.user.create.questions.status'),
218                 [0, 1],
219                 1
220             );
221
222             $input->setOption('status', $status);
223         }
224     }
225
226     private function createUser($username, $password, $roles, $email = null, $status = null)
227     {
228         $user = User::create(
229             [
230                 'name' => $username,
231                 'mail' => $email ?: $username . '@example.com',
232                 'pass' => $password?:user_password(),
233                 'status' => $status,
234                 'roles' => $roles,
235                 'created' => REQUEST_TIME,
236             ]
237         );
238
239         $result = [];
240
241         try {
242             $user->save();
243
244             $result['success'] = [
245                 'user-id' => $user->id(),
246                 'username' => $user->getUsername(),
247                 'password' => $password,
248                 'email' => $user->getEmail(),
249                 'roles' => implode(', ', $roles),
250                 'created' => $this->dateFormatter->format(
251                     $user->getCreatedTime(),
252                     'custom',
253                     'Y-m-d h:i:s'
254                 ),
255                 'status' => $status
256
257             ];
258         } catch (\Exception $e) {
259             $result['error'] = [
260                 'vid' => $user->id(),
261                 'name' => $user->get('name'),
262                 'error' => 'Error: ' . get_class($e) . ', code: ' . $e->getCode() . ', message: ' . $e->getMessage()
263             ];
264         }
265
266         return $result;
267     }
268 }