9f413992ba0c9fe3e25213180b10353821d711cf
[yaffs-website] / vendor / drupal / console / src / Command / User / PasswordResetCommand.php
1 <?php
2 /**
3  * @file
4  * Contains \Drupal\Console\Command\User\PasswordResetCommand.
5  */
6
7 namespace Drupal\Console\Command\User;
8
9 use Symfony\Component\Console\Input\InputInterface;
10 use Symfony\Component\Console\Input\InputArgument;
11 use Symfony\Component\Console\Output\OutputInterface;
12 use Symfony\Component\Console\Command\Command;
13 use Drupal\Console\Core\Command\Shared\CommandTrait;
14 use Drupal\Core\Database\Connection;
15 use Drupal\Console\Core\Utils\ChainQueue;
16 use Drupal\Console\Command\Shared\ConfirmationTrait;
17 use Drupal\user\Entity\User;
18 use Drupal\Console\Core\Style\DrupalStyle;
19
20 class PasswordResetCommand extends Command
21 {
22     use CommandTrait;
23     use ConfirmationTrait;
24
25     /**
26      * @var Connection
27      */
28     protected $database;
29
30     /**
31      * @var ChainQueue
32      */
33     protected $chainQueue;
34
35     /**
36      * PasswordHashCommand constructor.
37      *
38      * @param Connection $database
39      * @param ChainQueue $chainQueue
40      */
41     public function __construct(
42         Connection $database,
43         ChainQueue $chainQueue
44     ) {
45         $this->database = $database;
46         $this->chainQueue = $chainQueue;
47         parent::__construct();
48     }
49
50     /**
51      * {@inheritdoc}
52      */
53     protected function configure()
54     {
55         $this
56             ->setName('user:password:reset')
57             ->setDescription($this->trans('commands.user.password.reset.description'))
58             ->setHelp($this->trans('commands.user.password.reset.help'))
59             ->addArgument('user', InputArgument::REQUIRED, $this->trans('commands.user.password.reset.options.user-id'))
60             ->addArgument('password', InputArgument::REQUIRED, $this->trans('commands.user.password.reset.options.password'));
61     }
62
63     /**
64      * {@inheritdoc}
65      */
66     protected function execute(InputInterface $input, OutputInterface $output)
67     {
68         $io = new DrupalStyle($input, $output);
69
70         $uid = $input->getArgument('user');
71
72         $user = User::load($uid);
73
74         if (!$user) {
75             $io->error(
76                 sprintf(
77                     $this->trans('commands.user.password.reset.errors.invalid-user'),
78                     $uid
79                 )
80             );
81
82             return 1;
83         }
84
85         $password = $input->getArgument('password');
86         if (!$password) {
87             $io->error(
88                 sprintf(
89                     $this->trans('commands.user.password.reset.errors.empty-password'),
90                     $uid
91                 )
92             );
93
94             return 1;
95         }
96
97         try {
98             $user->setPassword($password);
99             $user->save();
100
101             $schema = $this->database->schema();
102             $flood = $schema->findTables('flood');
103
104             if ($flood) {
105                 $this-$this->chainQueue
106                     ->addCommand('user:login:clear:attempts', ['uid' => $uid]);
107             }
108         } catch (\Exception $e) {
109             $io->error($e->getMessage());
110
111             return 1;
112         }
113
114         $io->success(
115             sprintf(
116                 $this->trans('commands.user.password.reset.messages.reset-successful'),
117                 $uid
118             )
119         );
120     }
121
122     /**
123      * {@inheritdoc}
124      */
125     protected function interact(InputInterface $input, OutputInterface $output)
126     {
127         $io = new DrupalStyle($input, $output);
128
129         $user = $input->getArgument('user');
130         if (!$user) {
131             while (true) {
132                 $user = $io->ask(
133                     $this->trans('commands.user.password.reset.questions.user'),
134                     '',
135                     function ($uid) use ($io) {
136                         if ($uid) {
137                             $uid = (int) $uid;
138                             if (is_int($uid) && $uid > 0) {
139                                 return $uid;
140                             } else {
141                                 $io->error(
142                                     sprintf($this->trans('commands.user.password.reset.questions.invalid-uid'), $uid)
143                                 );
144
145                                 return false;
146                             }
147                         }
148                     }
149                 );
150
151                 if ($user) {
152                     break;
153                 }
154             }
155
156             $input->setArgument('user', $user);
157         }
158
159         $password = $input->getArgument('password');
160         if (!$password) {
161             while (true) {
162                 $password = $io->ask(
163                     $this->trans('commands.user.password.hash.questions.password'),
164                     '',
165                     function ($pass) use ($io) {
166                         if ($pass) {
167                             if (!empty($pass)) {
168                                 return $pass;
169                             } else {
170                                 $io->error(
171                                     sprintf($this->trans('commands.user.password.hash.questions.invalid-pass'), $pass)
172                                 );
173
174                                 return false;
175                             }
176                         }
177                     }
178                 );
179
180                 if ($password) {
181                     break;
182                 }
183             }
184
185
186             $input->setArgument('password', $password);
187         }
188     }
189 }