From 3f87907a85f5ccdbaf0b218353fa4735e4a684b7 Mon Sep 17 00:00:00 2001 From: "Ing. Guido Robertone" Date: Wed, 24 Apr 2019 16:09:37 -0300 Subject: [PATCH] Allow unblocking a blocked user. (#4018) * Allow unblocking a blocked user. * Warning unblocked user message. --- config/services/user.yml | 5 ++ src/Command/User/UnblockCommand.php | 102 ++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 src/Command/User/UnblockCommand.php diff --git a/config/services/user.yml b/config/services/user.yml index a40f94b41..372beb7d7 100644 --- a/config/services/user.yml +++ b/config/services/user.yml @@ -34,3 +34,8 @@ services: arguments: ['@database', '@entity_type.manager', '@date.formatter', '@console.drupal_api'] tags: - { name: drupal.command } + console.user_unblock: + class: Drupal\Console\Command\User\UnblockCommand + arguments: ['@entity_type.manager'] + tags: + - { name: drupal.command } diff --git a/src/Command/User/UnblockCommand.php b/src/Command/User/UnblockCommand.php new file mode 100644 index 000000000..63d95b1ac --- /dev/null +++ b/src/Command/User/UnblockCommand.php @@ -0,0 +1,102 @@ +setName('user:unblock') + ->setDescription($this->trans('commands.user.unblock.description')) + ->setHelp($this->trans('commands.user.unblock.help')) + ->addArgument( + 'user', + InputArgument::REQUIRED, + $this->trans('commands.user.unblock.options.user') + ) + ->setAliases(['uu']); + } + + /** + * {@inheritdoc} + */ + protected function interact(InputInterface $input, OutputInterface $output) + { + $this->getUserArgument(); + } + + /** + * {@inheritdoc} + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $user = $input->getArgument('user'); + + $userEntity = $this->getUserEntity($user); + + if (!$userEntity) { + $this->getIo()->error( + sprintf( + $this->trans('commands.user.unblock.errors.invalid-user'), + $user + ) + ); + + return 1; + } + + if (!$userEntity->isBlocked()) { + $this->getIo()->warning( + sprintf( + $this->trans('commands.user.unblock.warnings.unblocked-user'), + $user + ) + ); + + return 1; + } + + try { + $userEntity->activate(); + + $userEntity->save(); + + $this->getIo()->success( + sprintf( + $this->trans('commands.user.unblock.messages.unblock-successful'), + $user + ) + ); + + return 0; + } catch (\Exception $e) { + $this->getIo()->error($e->getMessage()); + + return 1; + } + } +}