94fd669f48c3942b586aa2caf6d1c7828962ee62
[yaffs-website] / vendor / drupal / console / src / Command / User / LoginCleanAttemptsCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\User\LoginCleanAttemptsCommand.
6  */
7
8 namespace Drupal\Console\Command\User;
9
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;
19
20 class LoginCleanAttemptsCommand extends Command
21 {
22     use CommandTrait;
23     use ConfirmationTrait;
24
25     /**
26      * @var Connection
27      */
28     protected $database;
29
30     /**
31      * LoginCleanAttemptsCommand constructor.
32      *
33      * @param Connection $database
34      */
35     public function __construct(Connection $database)
36     {
37         $this->database = $database;
38         parent::__construct();
39     }
40
41     /**
42      * {@inheritdoc}
43      */
44     protected function configure()
45     {
46         $this->
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'));
51     }
52
53     /**
54      * {@inheritdoc}
55      */
56     protected function interact(InputInterface $input, OutputInterface $output)
57     {
58         $io = new DrupalStyle($input, $output);
59
60         $uid = $input->getArgument('uid');
61         // Check if $uid argument is already set.
62         if (!$uid) {
63             while (true) {
64                 // Request $uid argument.
65                 $uid = $io->ask(
66                     $this->trans('commands.user.login.clear.attempts.questions.uid'),
67                     1,
68                     function ($uid) use ($io) {
69                         $message = (!is_numeric($uid)) ?
70                         $this->trans('commands.user.login.clear.attempts.questions.numeric-uid') :
71                         false;
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');
75                         }
76                         // Check if message was defined.
77                         if ($message) {
78                             $io->error($message);
79
80                             return false;
81                         }
82                         // Return a valid $uid.
83                         return (int) $uid;
84                     }
85                 );
86
87                 if ($uid) {
88                     break;
89                 }
90             }
91
92             $input->setArgument('uid', $uid);
93         }
94     }
95
96     /**
97      * {@inheritdoc}
98      */
99     protected function execute(InputInterface $input, OutputInterface $output)
100     {
101         $io = new DrupalStyle($input, $output);
102
103         $uid = $input->getArgument('uid');
104         $account = User::load($uid);
105
106         if (!$account) {
107             // Error loading User entity.
108             $io->error(
109                 sprintf(
110                     $this->trans('commands.user.login.clear.attempts.errors.invalid-user'),
111                     $uid
112                 )
113             );
114
115             return 1;
116         }
117
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()}-";
123
124         // Retrieve current database connection.
125         $schema = $this->database->schema();
126         $flood = $schema->findTables('flood');
127
128         if (!$flood) {
129             $io->error(
130                 $this->trans('commands.user.login.clear.attempts.errors.no-flood')
131             );
132
133             return 1;
134         }
135
136         // Clear login attempts.
137         $this->database->delete('flood')
138             ->condition('event', $event)
139             ->condition('identifier', $this->database->escapeLike($identifier) . '%', 'LIKE')
140             ->execute();
141
142         // Command executed successful.
143         $io->success(
144             sprintf(
145                 $this->trans('commands.user.login.clear.attempts.messages.successful'),
146                 $uid
147             )
148         );
149     }
150 }