Security update for Core, with self-updated composer
[yaffs-website] / vendor / drupal / console / src / Command / User / CreateCommand.php
index aece1e29aff2e26c06c933179463b29147f91283..10e47cadf58bb9e0fd6bb105a47e75e023079a1c 100644 (file)
@@ -10,21 +10,15 @@ use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Input\InputArgument;
 use Symfony\Component\Console\Input\InputOption;
 use Symfony\Component\Console\Output\OutputInterface;
-use Symfony\Component\Console\Command\Command;
-use Drupal\Console\Core\Command\Shared\CommandTrait;
+use Drupal\Console\Core\Command\Command;
 use Drupal\Core\Database\Connection;
 use Drupal\Core\Entity\EntityTypeManagerInterface;
 use Drupal\Core\Datetime\DateFormatterInterface;
-use Drupal\Console\Core\Utils\ChainQueue;
 use Drupal\Console\Utils\DrupalApi;
-use Drupal\Console\Command\Shared\ConfirmationTrait;
 use Drupal\user\Entity\User;
-use Drupal\Console\Core\Style\DrupalStyle;
 
 class CreateCommand extends Command
 {
-    use CommandTrait;
-    use ConfirmationTrait;
 
     /**
      * @var Connection
@@ -76,11 +70,34 @@ class CreateCommand extends Command
             ->setName('user:create')
             ->setDescription($this->trans('commands.user.create.description'))
             ->setHelp($this->trans('commands.user.create.help'))
-            ->addArgument('username', InputArgument::OPTIONAL, $this->trans('commands.user.create.options.username'))
-            ->addArgument('password', InputArgument::OPTIONAL, $this->trans('commands.user.create.options.password'))
-            ->addOption('roles', null, InputOption::VALUE_OPTIONAL, $this->trans('commands.user.create.options.roles'))
-            ->addOption('email', null, InputOption::VALUE_OPTIONAL, $this->trans('commands.user.create.options.email'))
-            ->addOption('status', null, InputOption::VALUE_OPTIONAL, $this->trans('commands.user.create.options.status'));
+            ->addArgument(
+                'username',
+                InputArgument::OPTIONAL,
+                $this->trans('commands.user.create.options.username')
+            )
+            ->addArgument(
+                'password',
+                InputArgument::OPTIONAL,
+                $this->trans('commands.user.create.options.password')
+            )
+            ->addOption(
+                'roles',
+                null,
+                InputOption::VALUE_OPTIONAL,
+                $this->trans('commands.user.create.options.roles')
+            )
+            ->addOption(
+                'email',
+                null,
+                InputOption::VALUE_OPTIONAL,
+                $this->trans('commands.user.create.options.email')
+            )
+            ->addOption(
+                'status',
+                null,
+                InputOption::VALUE_OPTIONAL,
+                $this->trans('commands.user.create.options.status')
+            )->setAliases(['uc']);
     }
 
     /**
@@ -88,15 +105,19 @@ class CreateCommand extends Command
      */
     protected function execute(InputInterface $input, OutputInterface $output)
     {
-        $io = new DrupalStyle($input, $output);
-
         $username = $input->getArgument('username');
         $password = $input->getArgument('password');
         $roles = $input->getOption('roles');
         $email = $input->getOption('email');
         $status = $input->getOption('status');
 
-        $user = $this->createUser($username, $password, $roles, $email, $status);
+        $user = $this->createUser(
+            $username,
+            $password,
+            $roles,
+            $email,
+            $status
+        );
 
         $tableHeader = ['Field', 'Value'];
 
@@ -119,8 +140,9 @@ class CreateCommand extends Command
                 $user['success']
             );
 
-            $io->table($tableHeader, $tableData);
-            $io->success(
+            $this->getIo()->table($tableHeader, $tableData);
+
+            $this->getIo()->success(
                 sprintf(
                     $this->trans('commands.user.create.messages.user-created'),
                     $user['success']['username']
@@ -131,7 +153,9 @@ class CreateCommand extends Command
         }
 
         if ($user['error']) {
-            $io->error($user['error']['error']);
+            $this->getIo()->error($user['error']['error']);
+
+            return 1;
         }
     }
 
@@ -140,30 +164,28 @@ class CreateCommand extends Command
      */
     protected function interact(InputInterface $input, OutputInterface $output)
     {
-        $io = new DrupalStyle($input, $output);
-
         $username = $input->getArgument('username');
-        while (!$username) {
-            $username = $io->askEmpty(
-                $this->trans('commands.user.create.questions.username'),
-                null
+        if (!$username) {
+            $username = $this->getIo()->ask(
+                $this->trans('commands.user.create.questions.username')
             );
+
+            $input->setArgument('username', $username);
         }
-        $input->setArgument('username', $username);
 
         $password = $input->getArgument('password');
         if (!$password) {
-            $password = $io->askEmpty(
-                $this->trans('commands.user.create.questions.password'),
-                null
+            $password = $this->getIo()->askEmpty(
+                $this->trans('commands.user.create.questions.password')
             );
+
+            $input->setArgument('password', $password);
         }
-        $input->setArgument('password', $password);
 
         $roles = $input->getOption('roles');
         if (!$roles) {
             $systemRoles = $this->drupalApi->getRoles(false, false, false);
-            $roles = $io->choice(
+            $roles = $this->getIo()->choice(
                 $this->trans('commands.user.create.questions.roles'),
                 array_values($systemRoles),
                 null,
@@ -182,32 +204,32 @@ class CreateCommand extends Command
 
         $email = $input->getOption('email');
         if (!$email) {
-            $email = $io->askEmpty(
-                $this->trans('commands.user.create.questions.email'),
-                null
+            $email = $this->getIo()->askEmpty(
+                $this->trans('commands.user.create.questions.email')
             );
+
+            $input->setOption('email', $email);
         }
-        $input->setOption('email', $email);
 
         $status = $input->getOption('status');
         if (!$status) {
-            $status = $io->choice(
+            $status = $this->getIo()->choice(
                 $this->trans('commands.user.create.questions.status'),
                 [0, 1],
                 1
             );
+
+            $input->setOption('status', $status);
         }
-        $input->setOption('status', $status);
     }
 
     private function createUser($username, $password, $roles, $email = null, $status = null)
     {
-        $password = $password?:$this->generatePassword();
         $user = User::create(
             [
                 'name' => $username,
                 'mail' => $email ?: $username . '@example.com',
-                'pass' => $password,
+                'pass' => $password?:user_password(),
                 'status' => $status,
                 'roles' => $roles,
                 'created' => REQUEST_TIME,
@@ -243,16 +265,4 @@ class CreateCommand extends Command
 
         return $result;
     }
-
-    private function generatePassword()
-    {
-        $length = mt_rand(8, 16);
-        $str = '';
-
-        for ($i = 0; $i < $length; $i++) {
-            $str .= chr(mt_rand(32, 126));
-        }
-
-        return $str;
-    }
 }