Skip to content

Commit 12ff0ab

Browse files
committed
minor #443 Validate user data before save (yceruto)
This PR was squashed before being merged into the master branch (closes #443). Discussion ---------- Validate user data before save 1. I've added a `validateUserData` method to check for existent user email (this improves a better messages exception) and validate plainPassword and email in non-interactive mode (i.e. `$ app:add-user username password email`). 2. Renamed the option `--is-admin` to just `--admin`, Imho `is-admin` feels more like a question rather than an instruction. (now `$ app:add-user username password email --admin`) Any suggestion is welcome! Commits ------- 7fc2fc1 Validate user data before save
2 parents e11a6e9 + 7fc2fc1 commit 12ff0ab

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

src/AppBundle/Command/AddUserCommand.php

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
* See http://symfony.com/doc/current/cookbook/console/console_command.html
3636
*
3737
* @author Javier Eguiluz <[email protected]>
38+
* @author Yonel Ceruto <[email protected]>
3839
*/
3940
class AddUserCommand extends ContainerAwareCommand
4041
{
@@ -60,7 +61,7 @@ protected function configure()
6061
->addArgument('username', InputArgument::OPTIONAL, 'The username of the new user')
6162
->addArgument('password', InputArgument::OPTIONAL, 'The plain password of the new user')
6263
->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')
6465
;
6566
}
6667

@@ -175,14 +176,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
175176
$username = $input->getArgument('username');
176177
$plainPassword = $input->getArgument('password');
177178
$email = $input->getArgument('email');
178-
$isAdmin = $input->getOption('is-admin');
179+
$isAdmin = $input->getOption('admin');
179180

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);
186183

187184
// create the user and encode its password
188185
$user = new User();
@@ -218,11 +215,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
218215
public function passwordValidator($plainPassword)
219216
{
220217
if (empty($plainPassword)) {
221-
throw new \Exception('The password can not be empty');
218+
throw new \Exception('The password can not be empty.');
222219
}
223220

224221
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.');
226223
}
227224

228225
return $plainPassword;
@@ -237,16 +234,39 @@ public function passwordValidator($plainPassword)
237234
public function emailValidator($email)
238235
{
239236
if (empty($email)) {
240-
throw new \Exception('The email can not be empty');
237+
throw new \Exception('The email can not be empty.');
241238
}
242239

243240
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.');
245242
}
246243

247244
return $email;
248245
}
249246

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+
250270
/**
251271
* The command help is usually included in the configure() method, but when
252272
* it's too long, it's better to define a separate method to maintain the
@@ -260,9 +280,9 @@ private function getCommandHelp()
260280
<info>php %command.full_name%</info> <comment>username password email</comment>
261281
262282
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:
264284
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>
266286
267287
If you omit any of the three required arguments, the command will ask you to
268288
provide the missing values:

0 commit comments

Comments
 (0)