Skip to content

Commit

Permalink
Allow unblocking a blocked user. (#4018)
Browse files Browse the repository at this point in the history
* Allow unblocking a blocked user.

* Warning unblocked user message.
  • Loading branch information
GuidoRobertone authored and enzolutions committed Apr 24, 2019
1 parent 7f914f7 commit 3f87907
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
5 changes: 5 additions & 0 deletions config/services/user.yml
Expand Up @@ -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 }
102 changes: 102 additions & 0 deletions src/Command/User/UnblockCommand.php
@@ -0,0 +1,102 @@
<?php
/**
* @file
* Contains \Drupal\Console\Command\User\UnblockCommand.
*/

namespace Drupal\Console\Command\User;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;

class UnblockCommand extends UserBase
{
/**
* UnblockCommand constructor.
*
* @param EntityTypeManagerInterface $entityTypeManager
*/
public function __construct(
EntityTypeManagerInterface $entityTypeManager
) {
parent::__construct($entityTypeManager);
}

/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->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;
}
}
}

0 comments on commit 3f87907

Please sign in to comment.