Security update for Core, with self-updated composer
[yaffs-website] / vendor / drupal / console / src / Command / User / DeleteCommand.php
index 3a4b88d25675eadfe48b176de53e978f3341c191..15f39158731215cecbf036759fda86929fe79739 100644 (file)
@@ -10,11 +10,8 @@ namespace Drupal\Console\Command\User;
 use Symfony\Component\Console\Input\InputOption;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Output\OutputInterface;
-use Symfony\Component\Console\Command\Command;
-use Drupal\Console\Core\Command\Shared\CommandTrait;
 use Drupal\Core\Entity\EntityTypeManagerInterface;
 use Drupal\Core\Entity\Query\QueryFactory;
-use Drupal\Console\Core\Style\DrupalStyle;
 use Drupal\Console\Utils\DrupalApi;
 
 /**
@@ -22,15 +19,8 @@ use Drupal\Console\Utils\DrupalApi;
  *
  * @package Drupal\Console\Command\User
  */
-class DeleteCommand extends Command
+class DeleteCommand extends UserBase
 {
-    use CommandTrait;
-
-    /**
-     * @var EntityTypeManagerInterface
-     */
-    protected $entityTypeManager;
-
     /**
      * @var QueryFactory
      */
@@ -53,10 +43,9 @@ class DeleteCommand extends Command
         QueryFactory $entityQuery,
         DrupalApi $drupalApi
     ) {
-        $this->entityTypeManager = $entityTypeManager;
         $this->entityQuery = $entityQuery;
         $this->drupalApi = $drupalApi;
-        parent::__construct();
+        parent::__construct($entityTypeManager);
     }
 
     /**
@@ -68,17 +57,17 @@ class DeleteCommand extends Command
             ->setName('user:delete')
             ->setDescription($this->trans('commands.user.delete.description'))
             ->addOption(
-                'user-id',
+                'user',
                 null,
                 InputOption::VALUE_OPTIONAL,
-                $this->trans('commands.user.delete.options.user-id')
+                $this->trans('commands.user.delete.options.user')
             )
             ->addOption(
                 'roles',
                 null,
                 InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL,
                 $this->trans('commands.user.delete.options.roles')
-            );
+            )->setAliases(['ud']);
     }
 
     /**
@@ -86,22 +75,13 @@ class DeleteCommand extends Command
      */
     protected function interact(InputInterface $input, OutputInterface $output)
     {
-        $io = new DrupalStyle($input, $output);
-
-        $userId = $input->getOption('user-id');
-        if (!$userId) {
-            $userId = $io->askEmpty(
-                $this->trans('commands.user.delete.questions.user-id'),
-                null
-            );
-            $input->setOption('user-id', $userId);
-        }
+        $user = $this->getUserOption();
 
         $roles = $input->getOption('roles');
 
-        if (!$userId && !$roles) {
+        if (!$user && !$roles) {
             $systemRoles = $this->drupalApi->getRoles(false, false, false);
-            $roles = $io->choice(
+            $roles = $this->getIo()->choice(
                 $this->trans('commands.user.delete.questions.roles'),
                 array_values($systemRoles),
                 null,
@@ -124,31 +104,26 @@ class DeleteCommand extends Command
      */
     protected function execute(InputInterface $input, OutputInterface $output)
     {
-        $io = new DrupalStyle($input, $output);
-
-        $userId = $input->getOption('user-id');
-
-        if ($userId && $userId <= 1) {
-            $io->error(
-                sprintf(
-                    $this->trans('commands.user.delete.errors.invalid-user-id'),
-                    $userId
-                )
-            );
+        $user = $input->getOption('user');
 
-            return 1;
-        }
+        if ($user) {
+            $userEntity = $this->getUserEntity($user);
+            if (!$userEntity) {
+                $this->getIo()->error(
+                    sprintf(
+                        $this->trans('commands.user.delete.errors.invalid-user'),
+                        $user
+                    )
+                );
 
-        if ($userId) {
-            $user = $this->entityTypeManager
-                ->getStorage('user')
-                ->load($userId);
+                return 1;
+            }
 
-            if (!$user) {
-                $io->error(
+            if ($userEntity->id() <= 1) {
+                $this->getIo()->error(
                     sprintf(
                         $this->trans('commands.user.delete.errors.invalid-user'),
-                        $userId
+                        $user
                     )
                 );
 
@@ -156,15 +131,17 @@ class DeleteCommand extends Command
             }
 
             try {
-                $user->delete();
-                $io->info(
+                $userEntity->delete();
+                $this->getIo()->info(
                     sprintf(
                         $this->trans('commands.user.delete.messages.user-deleted'),
-                        $user->getUsername()
+                        $userEntity->getUsername()
                     )
                 );
+
+                return 0;
             } catch (\Exception $e) {
-                $io->error($e->getMessage());
+                $this->getIo()->error($e->getMessage());
 
                 return 1;
             }
@@ -173,14 +150,17 @@ class DeleteCommand extends Command
         $roles = $input->getOption('roles');
 
         if ($roles) {
-            $userStorage = $this->entityTypeManager->getStorage('user');
+            $roles = is_array($roles)?$roles:[$roles];
 
-            $query = $this->entityQuery->get('user');
-            $query->condition('roles', is_array($roles)?$roles:[$roles], 'IN');
-            $query->condition('uid', 1, '>');
+            $query = $this->entityQuery
+                ->get('user')
+                ->condition('roles', array_values($roles), 'IN')
+                ->condition('uid', 1, '>');
             $results = $query->execute();
 
-            $users = $userStorage->loadMultiple($results);
+            $users = $this->entityTypeManager
+                ->getStorage('user')
+                ->loadMultiple($results);
 
             $tableHeader = [
               $this->trans('commands.user.debug.messages.user-id'),
@@ -188,26 +168,28 @@ class DeleteCommand extends Command
             ];
 
             $tableRows = [];
-            foreach ($users as $userId => $user) {
+            foreach ($users as $user => $userEntity) {
                 try {
-                    $user->delete();
-                    $tableRows['success'][] = [$userId, $user->getUsername()];
+                    $userEntity->delete();
+                    $tableRows['success'][] = [$user, $userEntity->getUsername()];
                 } catch (\Exception $e) {
-                    $tableRows['error'][] = [$userId, $user->getUsername()];
-                    $io->error($e->getMessage());
+                    $tableRows['error'][] = [$user, $userEntity->getUsername()];
+                    $this->getIo()->error($e->getMessage());
 
                     return 1;
                 }
             }
 
             if ($tableRows['success']) {
-                $io->table($tableHeader, $tableRows['success']);
-                $io->success(
+                $this->getIo()->table($tableHeader, $tableRows['success']);
+                $this->getIo()->success(
                     sprintf(
                         $this->trans('commands.user.delete.messages.users-deleted'),
                         count($tableRows['success'])
                     )
                 );
+
+                return 0;
             }
         }
     }