Secure create-user command #123 (#127)

* ask the user for the password #123
* added integration test #123
This commit is contained in:
Simon Schaufelberger
2018-02-10 19:11:30 +01:00
committed by Kevin Papst
parent 5527f29caf
commit 93e1cd5095
10 changed files with 506 additions and 303 deletions

View File

@@ -13,9 +13,11 @@ use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Registry;
use Symfony\Bridge\Doctrine\RegistryInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoder;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
@@ -65,10 +67,10 @@ class CreateUserCommand extends Command
->setName('kimai:create-user')
->setDescription('Create a new user')
->setHelp('This command allows you to create a new user.')
->addArgument('username', InputArgument::REQUIRED, 'New username (must be unique)')
->addArgument('password', InputArgument::REQUIRED, 'Users password')
->addArgument('email', InputArgument::REQUIRED, 'Users email address (must be unique)')
->addArgument('role', InputArgument::OPTIONAL, 'Users role (comma separated list)', User::DEFAULT_ROLE)
->addArgument('username', InputArgument::REQUIRED, 'The username of the user to be created (must be unique)')
->addArgument('email', InputArgument::REQUIRED, 'Email address of the user to be created (must be unique)')
->addArgument('role', InputArgument::OPTIONAL, 'A comma separated list of roles to assign. Examples: "ROLE_USER,ROLE_SUPER_ADMIN"', User::DEFAULT_ROLE)
->addArgument('password', InputArgument::OPTIONAL, 'Password for the user to be created')
;
}
@@ -81,9 +83,14 @@ class CreateUserCommand extends Command
$username = $input->getArgument('username');
$email = $input->getArgument('email');
$password = $input->getArgument('password');
$role = $input->getArgument('role');
if ($input->getArgument('password') !== null) {
$password = $input->getArgument('password');
} else {
$password = $this->askForPassword($input, $output);
}
$role = $role ?: User::DEFAULT_ROLE;
$user = new User();
@@ -103,7 +110,7 @@ class CreateUserCommand extends Command
$value = $error->getInvalidValue();
$io->error(
$error->getPropertyPath()
. " (" . (is_array($value) ? implode(',', $value) : $value) .")"
. ' (' . (is_array($value) ? implode(',', $value) : $value) . ')'
. "\n "
. $error->getMessage()
);
@@ -121,4 +128,31 @@ class CreateUserCommand extends Command
$io->error('Reason: ' . $ex->getMessage());
}
}
/**
* @param InputInterface $input
* @param OutputInterface $output
*
* @return string
*/
protected function askForPassword(InputInterface $input, OutputInterface $output): string
{
/* @var QuestionHelper $helper */
$helper = $this->getHelper('question');
$passwordQuestion = new Question('Please enter the password');
$passwordQuestion->setHidden(true);
$passwordQuestion->setHiddenFallback(false);
$passwordQuestion->setValidator(function (?string $value) {
$password = trim($value);
if (empty($password) || strlen($password) < 6) {
throw new \Exception('The password is too short, must be at least 6 character');
}
return $value;
});
$passwordQuestion->setMaxAttempts(3);
return $helper->ask($input, $output, $passwordQuestion);
}
}