1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* (c) FSi sp. z o.o. <[email protected]> |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace FSi\Bundle\AdminSecurityBundle\Command; |
13
|
|
|
|
14
|
|
|
use Exception; |
15
|
|
|
use FSi\Bundle\AdminSecurityBundle\Event\AdminSecurityEvents; |
16
|
|
|
use FSi\Bundle\AdminSecurityBundle\Event\UserEvent; |
17
|
|
|
use FSi\Bundle\AdminSecurityBundle\Security\User\UserInterface; |
18
|
|
|
use Symfony\Component\Console\Command\Command; |
19
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper; |
20
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
21
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
22
|
|
|
use Symfony\Component\Console\Input\InputOption; |
23
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
24
|
|
|
use Symfony\Component\Console\Question\Question; |
25
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
26
|
|
|
|
27
|
|
|
class CreateUserCommand extends Command |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var EventDispatcherInterface |
31
|
|
|
*/ |
32
|
|
|
private $eventDispatcher; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $userClass; |
38
|
|
|
|
39
|
|
|
public function __construct( |
40
|
|
|
EventDispatcherInterface $eventDispatcher, |
41
|
|
|
string $userClass, |
42
|
|
|
$name = null |
43
|
|
|
) { |
44
|
|
|
parent::__construct($name); |
45
|
|
|
|
46
|
|
|
$this->eventDispatcher = $eventDispatcher; |
47
|
|
|
$this->userClass = $userClass; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function configure(): void |
51
|
|
|
{ |
52
|
|
|
$this |
53
|
|
|
->setName('fsi:user:create') |
54
|
|
|
->setDescription('Create a user.') |
55
|
|
|
->setDefinition([ |
56
|
|
|
new InputArgument('email', InputArgument::REQUIRED, 'The email'), |
57
|
|
|
new InputArgument('password', InputArgument::REQUIRED, 'The password'), |
58
|
|
|
new InputArgument('role', InputArgument::REQUIRED, 'Role'), |
59
|
|
|
new InputOption('inactive', null, InputOption::VALUE_NONE, 'Set the user as inactive'), |
60
|
|
|
new InputOption('enforce-password-change', null, InputOption::VALUE_NONE, 'Enforce user to change password during next login'), |
61
|
|
|
]) |
62
|
|
|
->setHelp(<<<EOT |
63
|
|
|
The <info>fsi:user:create</info> command creates a user: |
64
|
|
|
|
65
|
|
|
<info>php app/console fsi:user:create</info> |
66
|
|
|
|
67
|
|
|
This interactive shell will ask you for an email and then a password. |
68
|
|
|
|
69
|
|
|
You can alternatively specify the email, password and role as the first, second and third arguments: |
70
|
|
|
|
71
|
|
|
<info>php app/console fsi:user:create [email protected] mypassword ROLE_ADMIN</info> |
72
|
|
|
|
73
|
|
|
EOT |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): void |
78
|
|
|
{ |
79
|
|
|
$email = $input->getArgument('email'); |
80
|
|
|
$password = $input->getArgument('password'); |
81
|
|
|
$role = $input->getArgument('role'); |
82
|
|
|
|
83
|
|
|
/* @var $user UserInterface */ |
84
|
|
|
$user = new $this->userClass(); |
85
|
|
|
$user->setEmail($email); |
86
|
|
|
$user->setPlainPassword($password); |
87
|
|
|
$user->addRole($role); |
88
|
|
|
if (!$input->getOption('inactive')) { |
89
|
|
|
$user->setEnabled(true); |
90
|
|
|
} |
91
|
|
|
if ($input->getOption('enforce-password-change')) { |
92
|
|
|
$user->enforcePasswordChange(true); |
93
|
|
|
} |
94
|
|
|
$this->eventDispatcher->dispatch(AdminSecurityEvents::USER_CREATED, new UserEvent($user)); |
95
|
|
|
|
96
|
|
|
$output->writeln(sprintf('Created user <comment>%s</comment>', $email)); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
protected function interact(InputInterface $input, OutputInterface $output): void |
100
|
|
|
{ |
101
|
|
|
if (!$input->getArgument('email')) { |
102
|
|
|
$this->askEmail($input, $output); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if (!$input->getArgument('password')) { |
106
|
|
|
$this->askPassword($input, $output); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if (!$input->getArgument('role')) { |
110
|
|
|
$this->askRole($input, $output); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
private function askEmail(InputInterface $input, OutputInterface $output): void |
115
|
|
|
{ |
116
|
|
|
$question = new Question('Please choose an email:'); |
117
|
|
|
$question->setValidator(function (string $email): string { |
118
|
|
|
if (empty($email)) { |
119
|
|
|
throw new Exception('Email can not be empty'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $email; |
123
|
|
|
}); |
124
|
|
|
|
125
|
|
|
$email = $this->getQuestionHelper()->ask($input, $output, $question); |
126
|
|
|
$input->setArgument('email', $email); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
private function askPassword(InputInterface $input, OutputInterface $output): void |
130
|
|
|
{ |
131
|
|
|
$question = new Question('Please choose a password:'); |
132
|
|
|
$question->setValidator(function (string $password): string { |
133
|
|
|
if (empty($password)) { |
134
|
|
|
throw new Exception('Password can not be empty'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $password; |
138
|
|
|
}); |
139
|
|
|
|
140
|
|
|
$password = $this->getQuestionHelper()->ask($input, $output, $question); |
141
|
|
|
$input->setArgument('password', $password); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
private function askRole(InputInterface $input, OutputInterface $output): void |
145
|
|
|
{ |
146
|
|
|
$question = new Question('Please choose a role:'); |
147
|
|
|
$question->setValidator(function (string $password): string { |
148
|
|
|
if (empty($password)) { |
149
|
|
|
throw new Exception('Role can not be empty'); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $password; |
153
|
|
|
}); |
154
|
|
|
|
155
|
|
|
$role = $this->getQuestionHelper()->ask($input, $output, $question); |
156
|
|
|
$input->setArgument('role', $role); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
private function getQuestionHelper(): QuestionHelper |
160
|
|
|
{ |
161
|
|
|
return $this->getHelper('question'); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|