1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the DoyoUserBundle project. |
5
|
|
|
* |
6
|
|
|
* (c) Anthonius Munthi <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Doyo\UserBundle\Command; |
15
|
|
|
|
16
|
|
|
use Doyo\UserBundle\Manager\UserManagerInterface; |
17
|
|
|
use Symfony\Component\Console\Command\Command; |
18
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
19
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
20
|
|
|
use Symfony\Component\Console\Input\InputOption; |
21
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
22
|
|
|
use Symfony\Component\Console\Question\Question; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @author Matthieu Bontemps <[email protected]> |
26
|
|
|
* @author Thibault Duplessis <[email protected]> |
27
|
|
|
* @author Luis Cordova <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class CreateUserCommand extends Command |
30
|
|
|
{ |
31
|
|
|
protected static $defaultName = 'doyo:user:create'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var UserManagerInterface |
35
|
|
|
*/ |
36
|
|
|
private $userManager; |
37
|
|
|
|
38
|
2 |
|
public function __construct(UserManagerInterface $userManager) |
39
|
|
|
{ |
40
|
2 |
|
parent::__construct(); |
41
|
|
|
|
42
|
2 |
|
$this->userManager = $userManager; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
2 |
|
protected function configure() |
49
|
|
|
{ |
50
|
|
|
$this |
51
|
2 |
|
->setName('doyo:user:create') |
52
|
2 |
|
->setDescription('Create a user.') |
53
|
2 |
|
->setDefinition([ |
54
|
2 |
|
new InputArgument('username', InputArgument::REQUIRED, 'The username'), |
55
|
2 |
|
new InputArgument('email', InputArgument::REQUIRED, 'The email'), |
56
|
2 |
|
new InputArgument('password', InputArgument::REQUIRED, 'The password'), |
57
|
2 |
|
new InputOption('super-admin', null, InputOption::VALUE_NONE, 'Set the user as super admin'), |
58
|
2 |
|
new InputOption('inactive', null, InputOption::VALUE_NONE, 'Set the user as inactive'), |
59
|
|
|
]) |
60
|
2 |
|
->setHelp(<<<'EOT' |
61
|
2 |
|
The <info>doyo:user:create</info> command creates a user: |
62
|
|
|
|
63
|
|
|
<info>php %command.full_name% matthieu</info> |
64
|
|
|
|
65
|
|
|
This interactive shell will ask you for an email and then a password. |
66
|
|
|
|
67
|
|
|
You can alternatively specify the email and password as the second and third arguments: |
68
|
|
|
|
69
|
|
|
<info>php %command.full_name% matthieu [email protected] mypassword</info> |
70
|
|
|
|
71
|
|
|
You can create a super admin via the super-admin flag: |
72
|
|
|
|
73
|
|
|
<info>php %command.full_name% admin --super-admin</info> |
74
|
|
|
|
75
|
|
|
You can create an inactive user (will not be able to log in): |
76
|
|
|
|
77
|
|
|
<info>php %command.full_name% thibault --inactive</info> |
78
|
|
|
|
79
|
|
|
EOT |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
2 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
87
|
|
|
{ |
88
|
2 |
|
$username = $input->getArgument('username'); |
89
|
2 |
|
$email = $input->getArgument('email'); |
90
|
2 |
|
$password = $input->getArgument('password'); |
91
|
2 |
|
$inactive = $input->getOption('inactive'); |
92
|
2 |
|
$superadmin = $input->getOption('super-admin'); |
93
|
2 |
|
$manager = $this->userManager; |
94
|
2 |
|
$user = $manager->createUser(); |
95
|
|
|
|
96
|
2 |
|
$user->setUsername($username) |
|
|
|
|
97
|
2 |
|
->setPlainPassword($password) |
|
|
|
|
98
|
2 |
|
->setEmail($email) |
|
|
|
|
99
|
2 |
|
->setEnabled(!$inactive); |
100
|
|
|
|
101
|
2 |
|
if ($superadmin) { |
102
|
|
|
$user->addRole('ROLE_SUPER_ADMIN'); |
103
|
|
|
} |
104
|
2 |
|
$manager->updateUser($user); |
105
|
|
|
|
106
|
2 |
|
$output->writeln(sprintf('Created user <comment>%s</comment>', $username)); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
1 |
|
protected function interact(InputInterface $input, OutputInterface $output) |
113
|
|
|
{ |
114
|
1 |
|
$questions = []; |
115
|
|
|
|
116
|
1 |
|
if (!$input->getArgument('username')) { |
117
|
1 |
|
$question = new Question('Please choose a username:'); |
118
|
|
|
$question->setValidator(function ($username) { |
119
|
|
|
if (empty($username)) { |
120
|
|
|
throw new \Exception('Username can not be empty'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $username; |
124
|
1 |
|
}); |
125
|
1 |
|
$questions['username'] = $question; |
126
|
|
|
} |
127
|
|
|
|
128
|
1 |
|
if (!$input->getArgument('email')) { |
129
|
1 |
|
$question = new Question('Please choose an email:'); |
130
|
|
|
$question->setValidator(function ($email) { |
131
|
|
|
if (empty($email)) { |
132
|
|
|
throw new \Exception('Email can not be empty'); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $email; |
136
|
1 |
|
}); |
137
|
1 |
|
$questions['email'] = $question; |
138
|
|
|
} |
139
|
|
|
|
140
|
1 |
|
if (!$input->getArgument('password')) { |
141
|
1 |
|
$question = new Question('Please choose a password:'); |
142
|
|
|
$question->setValidator(function ($password) { |
143
|
|
|
if (empty($password)) { |
144
|
|
|
throw new \Exception('Password can not be empty'); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $password; |
148
|
1 |
|
}); |
149
|
1 |
|
$question->setHidden(true); |
150
|
1 |
|
$questions['password'] = $question; |
151
|
|
|
} |
152
|
|
|
|
153
|
1 |
|
foreach ($questions as $name => $question) { |
154
|
1 |
|
$answer = $this->getHelper('question')->ask($input, $output, $question); |
155
|
1 |
|
$input->setArgument($name, $answer); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|