Skip to content

Validate user data before save #443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 34 additions & 14 deletions src/AppBundle/Command/AddUserCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
* See http://symfony.com/doc/current/cookbook/console/console_command.html
*
* @author Javier Eguiluz <[email protected]>
* @author Yonel Ceruto <[email protected]>
*/
class AddUserCommand extends ContainerAwareCommand
{
Expand All @@ -60,7 +61,7 @@ protected function configure()
->addArgument('username', InputArgument::OPTIONAL, 'The username of the new user')
->addArgument('password', InputArgument::OPTIONAL, 'The plain password of the new user')
->addArgument('email', InputArgument::OPTIONAL, 'The email of the new user')
->addOption('is-admin', null, InputOption::VALUE_NONE, 'If set, the user is created as an administrator')
->addOption('admin', null, InputOption::VALUE_NONE, 'If set, the user is created as an administrator')
;
}

Expand Down Expand Up @@ -175,14 +176,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
$username = $input->getArgument('username');
$plainPassword = $input->getArgument('password');
$email = $input->getArgument('email');
$isAdmin = $input->getOption('is-admin');
$isAdmin = $input->getOption('admin');

// first check if a user with the same username already exists
$existingUser = $this->entityManager->getRepository(User::class)->findOneBy(['username' => $username]);

if (null !== $existingUser) {
throw new \RuntimeException(sprintf('There is already a user registered with the "%s" username.', $username));
}
// make sure to validate the user data is correct
$this->validateUserData($username, $plainPassword, $email);

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

if (strlen(trim($plainPassword)) < 6) {
throw new \Exception('The password must be at least 6 characters long');
throw new \Exception('The password must be at least 6 characters long.');
}

return $plainPassword;
Expand All @@ -237,16 +234,39 @@ public function passwordValidator($plainPassword)
public function emailValidator($email)
{
if (empty($email)) {
throw new \Exception('The email can not be empty');
throw new \Exception('The email can not be empty.');
}

if (false === strpos($email, '@')) {
throw new \Exception('The email should look like a real email');
throw new \Exception('The email should look like a real email.');
}

return $email;
}

private function validateUserData($username, $plainPassword, $email)
{
$userRepository = $this->entityManager->getRepository(User::class);

// first check if a user with the same username already exists.
$existingUser = $userRepository->findOneBy(['username' => $username]);

if (null !== $existingUser) {
throw new \RuntimeException(sprintf('There is already a user registered with the "%s" username.', $username));
}

// validate password and email if is not this input means interactive.
$this->passwordValidator($plainPassword);
$this->emailValidator($email);

// check if a user with the same email already exists.
$existingEmail = $userRepository->findOneBy(['email' => $email]);

if (null !== $existingEmail) {
throw new \RuntimeException(sprintf('There is already a user registered with the "%s" email.', $email));
}
}

/**
* The command help is usually included in the configure() method, but when
* it's too long, it's better to define a separate method to maintain the
Expand All @@ -260,9 +280,9 @@ private function getCommandHelp()
<info>php %command.full_name%</info> <comment>username password email</comment>

By default the command creates regular users. To create administrator users,
add the <comment>--is-admin</comment> option:
add the <comment>--admin</comment> option:

<info>php %command.full_name%</info> username password email <comment>--is-admin</comment>
<info>php %command.full_name%</info> username password email <comment>--admin</comment>

If you omit any of the three required arguments, the command will ask you to
provide the missing values:
Expand Down