Version 1
[yaffs-website] / vendor / drupal / console / src / Command / User / PasswordHashCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\User\PasswordHashCommand.
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 Symfony\Component\Console\Command\Command;
14 use Drupal\Console\Core\Command\Shared\CommandTrait;
15 use Drupal\Core\Password\PasswordInterface;
16 use Drupal\Console\Command\Shared\ConfirmationTrait;
17 use Drupal\Console\Core\Style\DrupalStyle;
18
19 class PasswordHashCommand extends Command
20 {
21     use CommandTrait;
22     use ConfirmationTrait;
23
24     /**
25      * @var PasswordInterface
26      */
27     protected $password;
28
29     /**
30      * PasswordHashCommand constructor.
31      *
32      * @param PasswordInterface $password
33      */
34     public function __construct(PasswordInterface $password)
35     {
36         $this->password = $password;
37         parent::__construct();
38     }
39
40     /**
41      * {@inheritdoc}
42      */
43     protected function configure()
44     {
45         $this
46             ->setName('user:password:hash')
47             ->setDescription($this->trans('commands.user.password.hash.description'))
48             ->setHelp($this->trans('commands.user.password.hash.help'))
49             ->addArgument('password', InputArgument::IS_ARRAY, $this->trans('commands.user.password.hash.options.password'));
50     }
51
52     /**
53      * {@inheritdoc}
54      */
55     protected function execute(InputInterface $input, OutputInterface $output)
56     {
57         $io = new DrupalStyle($input, $output);
58
59         $passwords = $input->getArgument('password');
60
61         $tableHeader = [
62             $this->trans('commands.user.password.hash.messages.password'),
63             $this->trans('commands.user.password.hash.messages.hash'),
64         ];
65
66         $tableRows = [];
67         foreach ($passwords as $password) {
68             $tableRows[] = [
69                 $password,
70                 $this->password->hash($password),
71             ];
72         }
73
74         $io->table($tableHeader, $tableRows, 'compact');
75     }
76
77     /**
78      * {@inheritdoc}
79      */
80     protected function interact(InputInterface $input, OutputInterface $output)
81     {
82         $io = new DrupalStyle($input, $output);
83
84         $passwords = $input->getArgument('password');
85         if (!$passwords) {
86             $passwords = [];
87             while (true) {
88                 $password = $io->ask(
89                     $this->trans('commands.user.password.hash.questions.password'),
90                     '',
91                     function ($pass) use ($passwords, $io) {
92                         if (!empty($pass) || count($passwords) >= 1) {
93                             if ($pass == '') {
94                                 return true;
95                             }
96
97                             return $pass;
98                         } else {
99                             $io->error(
100                                 sprintf($this->trans('commands.user.password.hash.questions.invalid-pass'), $pass)
101                             );
102
103                             return false;
104                         }
105                     }
106                 );
107
108                 if ($password && !is_string($password)) {
109                     break;
110                 }
111
112                 if (is_string($password)) {
113                     $passwords[] = $password;
114                 }
115             }
116
117             $input->setArgument('password', $passwords);
118         }
119     }
120 }