35
35
* See http://symfony.com/doc/current/cookbook/console/console_command.html
36
36
*
37
37
* @author Javier Eguiluz <[email protected] >
38
+ * @author Yonel Ceruto <[email protected] >
38
39
*/
39
40
class AddUserCommand extends ContainerAwareCommand
40
41
{
@@ -60,7 +61,7 @@ protected function configure()
60
61
->addArgument ('username ' , InputArgument::OPTIONAL , 'The username of the new user ' )
61
62
->addArgument ('password ' , InputArgument::OPTIONAL , 'The plain password of the new user ' )
62
63
->addArgument ('email ' , InputArgument::OPTIONAL , 'The email of the new user ' )
63
- ->addOption ('is- admin ' , null , InputOption::VALUE_NONE , 'If set, the user is created as an administrator ' )
64
+ ->addOption ('admin ' , null , InputOption::VALUE_NONE , 'If set, the user is created as an administrator ' )
64
65
;
65
66
}
66
67
@@ -175,14 +176,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
175
176
$ username = $ input ->getArgument ('username ' );
176
177
$ plainPassword = $ input ->getArgument ('password ' );
177
178
$ email = $ input ->getArgument ('email ' );
178
- $ isAdmin = $ input ->getOption ('is- admin ' );
179
+ $ isAdmin = $ input ->getOption ('admin ' );
179
180
180
- // first check if a user with the same username already exists
181
- $ existingUser = $ this ->entityManager ->getRepository (User::class)->findOneBy (['username ' => $ username ]);
182
-
183
- if (null !== $ existingUser ) {
184
- throw new \RuntimeException (sprintf ('There is already a user registered with the "%s" username. ' , $ username ));
185
- }
181
+ // make sure to validate the user data is correct
182
+ $ this ->validateUserData ($ username , $ plainPassword , $ email );
186
183
187
184
// create the user and encode its password
188
185
$ user = new User ();
@@ -218,11 +215,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
218
215
public function passwordValidator ($ plainPassword )
219
216
{
220
217
if (empty ($ plainPassword )) {
221
- throw new \Exception ('The password can not be empty ' );
218
+ throw new \Exception ('The password can not be empty. ' );
222
219
}
223
220
224
221
if (strlen (trim ($ plainPassword )) < 6 ) {
225
- throw new \Exception ('The password must be at least 6 characters long ' );
222
+ throw new \Exception ('The password must be at least 6 characters long. ' );
226
223
}
227
224
228
225
return $ plainPassword ;
@@ -237,16 +234,39 @@ public function passwordValidator($plainPassword)
237
234
public function emailValidator ($ email )
238
235
{
239
236
if (empty ($ email )) {
240
- throw new \Exception ('The email can not be empty ' );
237
+ throw new \Exception ('The email can not be empty. ' );
241
238
}
242
239
243
240
if (false === strpos ($ email , '@ ' )) {
244
- throw new \Exception ('The email should look like a real email ' );
241
+ throw new \Exception ('The email should look like a real email. ' );
245
242
}
246
243
247
244
return $ email ;
248
245
}
249
246
247
+ private function validateUserData ($ username , $ plainPassword , $ email )
248
+ {
249
+ $ userRepository = $ this ->entityManager ->getRepository (User::class);
250
+
251
+ // first check if a user with the same username already exists.
252
+ $ existingUser = $ userRepository ->findOneBy (['username ' => $ username ]);
253
+
254
+ if (null !== $ existingUser ) {
255
+ throw new \RuntimeException (sprintf ('There is already a user registered with the "%s" username. ' , $ username ));
256
+ }
257
+
258
+ // validate password and email if is not this input means interactive.
259
+ $ this ->passwordValidator ($ plainPassword );
260
+ $ this ->emailValidator ($ email );
261
+
262
+ // check if a user with the same email already exists.
263
+ $ existingEmail = $ userRepository ->findOneBy (['email ' => $ email ]);
264
+
265
+ if (null !== $ existingEmail ) {
266
+ throw new \RuntimeException (sprintf ('There is already a user registered with the "%s" email. ' , $ email ));
267
+ }
268
+ }
269
+
250
270
/**
251
271
* The command help is usually included in the configure() method, but when
252
272
* it's too long, it's better to define a separate method to maintain the
@@ -260,9 +280,9 @@ private function getCommandHelp()
260
280
<info>php %command.full_name%</info> <comment>username password email</comment>
261
281
262
282
By default the command creates regular users. To create administrator users,
263
- add the <comment>--is- admin</comment> option:
283
+ add the <comment>--admin</comment> option:
264
284
265
- <info>php %command.full_name%</info> username password email <comment>--is- admin</comment>
285
+ <info>php %command.full_name%</info> username password email <comment>--admin</comment>
266
286
267
287
If you omit any of the three required arguments, the command will ask you to
268
288
provide the missing values:
0 commit comments