5 * Contains \Drupal\Console\Command\User\LoginCleanAttemptsCommand.
8 namespace Drupal\Console\Command\User;
10 use Symfony\Component\Console\Input\InputInterface;
11 use Symfony\Component\Console\Input\InputArgument;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Drupal\Console\Command\Shared\ConfirmationTrait;
14 use Symfony\Component\Console\Command\Command;
15 use Drupal\Console\Core\Command\Shared\CommandTrait;
16 use Drupal\Core\Database\Connection;
17 use Drupal\Console\Core\Style\DrupalStyle;
18 use Drupal\user\Entity\User;
20 class LoginCleanAttemptsCommand extends Command
23 use ConfirmationTrait;
31 * LoginCleanAttemptsCommand constructor.
33 * @param Connection $database
35 public function __construct(Connection $database)
37 $this->database = $database;
38 parent::__construct();
44 protected function configure()
47 setName('user:login:clear:attempts')
48 ->setDescription($this->trans('commands.user.login.clear.attempts.description'))
49 ->setHelp($this->trans('commands.user.login.clear.attempts.help'))
50 ->addArgument('uid', InputArgument::REQUIRED, $this->trans('commands.user.login.clear.attempts.options.user-id'));
56 protected function interact(InputInterface $input, OutputInterface $output)
58 $io = new DrupalStyle($input, $output);
60 $uid = $input->getArgument('uid');
61 // Check if $uid argument is already set.
64 // Request $uid argument.
66 $this->trans('commands.user.login.clear.attempts.questions.uid'),
68 function ($uid) use ($io) {
69 $message = (!is_numeric($uid)) ?
70 $this->trans('commands.user.login.clear.attempts.questions.numeric-uid') :
72 // Check if $uid is upper than zero.
73 if (!$message && $uid <= 0) {
74 $message = $this->trans('commands.user.login.clear.attempts.questions.invalid-uid');
76 // Check if message was defined.
82 // Return a valid $uid.
92 $input->setArgument('uid', $uid);
99 protected function execute(InputInterface $input, OutputInterface $output)
101 $io = new DrupalStyle($input, $output);
103 $uid = $input->getArgument('uid');
104 $account = User::load($uid);
107 // Error loading User entity.
110 $this->trans('commands.user.login.clear.attempts.errors.invalid-user'),
118 // Define event name and identifier.
119 $event = 'user.failed_login_user';
120 // Identifier is created by uid and IP address,
121 // Then we defined a generic identifier.
122 $identifier = "{$account->id()}-";
124 // Retrieve current database connection.
125 $schema = $this->database->schema();
126 $flood = $schema->findTables('flood');
130 $this->trans('commands.user.login.clear.attempts.errors.no-flood')
136 // Clear login attempts.
137 $this->database->delete('flood')
138 ->condition('event', $event)
139 ->condition('identifier', $this->database->escapeLike($identifier) . '%', 'LIKE')
142 // Command executed successful.
145 $this->trans('commands.user.login.clear.attempts.messages.successful'),