use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
-use Drupal\Console\Command\Shared\ConfirmationTrait;
-use Symfony\Component\Console\Command\Command;
-use Drupal\Console\Core\Command\Shared\CommandTrait;
+use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Database\Connection;
-use Drupal\Console\Core\Style\DrupalStyle;
use Drupal\user\Entity\User;
-class LoginCleanAttemptsCommand extends Command
+class LoginCleanAttemptsCommand extends UserBase
{
- use CommandTrait;
- use ConfirmationTrait;
-
/**
* @var Connection
*/
/**
* LoginCleanAttemptsCommand constructor.
*
- * @param Connection $database
+ * @param Connection $database
+ * @param EntityTypeManagerInterface $entityTypeManager
*/
- public function __construct(Connection $database)
- {
+ public function __construct(
+ Connection $database,
+ EntityTypeManagerInterface $entityTypeManager
+ ) {
$this->database = $database;
- parent::__construct();
+ parent::__construct($entityTypeManager);
}
/**
setName('user:login:clear:attempts')
->setDescription($this->trans('commands.user.login.clear.attempts.description'))
->setHelp($this->trans('commands.user.login.clear.attempts.help'))
- ->addArgument('uid', InputArgument::REQUIRED, $this->trans('commands.user.login.clear.attempts.options.user-id'));
+ ->addArgument(
+ 'user',
+ InputArgument::REQUIRED,
+ $this->trans('commands.user.login.clear.attempts.arguments.user')
+ )
+ ->setAliases(['ulca']);
}
/**
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
- $io = new DrupalStyle($input, $output);
-
- $uid = $input->getArgument('uid');
- // Check if $uid argument is already set.
- if (!$uid) {
- while (true) {
- // Request $uid argument.
- $uid = $io->ask(
- $this->trans('commands.user.login.clear.attempts.questions.uid'),
- 1,
- function ($uid) use ($io) {
- $message = (!is_numeric($uid)) ?
- $this->trans('commands.user.login.clear.attempts.questions.numeric-uid') :
- false;
- // Check if $uid is upper than zero.
- if (!$message && $uid <= 0) {
- $message = $this->trans('commands.user.login.clear.attempts.questions.invalid-uid');
- }
- // Check if message was defined.
- if ($message) {
- $io->error($message);
-
- return false;
- }
- // Return a valid $uid.
- return (int) $uid;
- }
- );
-
- if ($uid) {
- break;
- }
- }
-
- $input->setArgument('uid', $uid);
- }
+ $this->getUserArgument();
}
/**
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
- $io = new DrupalStyle($input, $output);
-
- $uid = $input->getArgument('uid');
- $account = User::load($uid);
+ $user = $input->getArgument('user');
+ $userEntity = $this->getUserEntity($user);
- if (!$account) {
- // Error loading User entity.
- $io->error(
+ if (!$userEntity) {
+ $this->getIo()->error(
sprintf(
$this->trans('commands.user.login.clear.attempts.errors.invalid-user'),
- $uid
+ $user
)
);
$event = 'user.failed_login_user';
// Identifier is created by uid and IP address,
// Then we defined a generic identifier.
- $identifier = "{$account->id()}-";
+ $identifier = "{$userEntity->id()}-";
// Retrieve current database connection.
$schema = $this->database->schema();
$flood = $schema->findTables('flood');
if (!$flood) {
- $io->error(
+ $this->getIo()->error(
$this->trans('commands.user.login.clear.attempts.errors.no-flood')
);
->execute();
// Command executed successful.
- $io->success(
+ $this->getIo()->success(
sprintf(
$this->trans('commands.user.login.clear.attempts.messages.successful'),
- $uid
+ $userEntity->getUsername()
)
);
}