|
- <?php
-
- namespace Lc\SovBundle\Command;
-
-
- use Lc\SovBundle\IModel\User\UserInterface;
- use Lc\SovBundle\Manager\EntityManager;
- use Symfony\Component\Console\Command\Command;
- 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\Security\Core\Encoder\UserPasswordEncoderInterface;
-
- class CreateUserCommand extends Command
- {
- private $passwordEncoder;
- private $em;
-
- public function __construct(UserPasswordEncoderInterface $passwordEncoder, EntityManager $entityManager)
- {
- parent::__construct();
-
- $this->passwordEncoder = $passwordEncoder;
- $this->em = $entityManager;
- }
-
- /**
- * {@inheritdoc}
- */
- protected function configure()
- {
- $this
- ->setName('user:create')
- ->setDescription('Create a user.')
- ->setDefinition(array(
- new InputArgument('email', InputArgument::REQUIRED, 'The email'),
- new InputArgument('password', InputArgument::REQUIRED, 'The password'),
- new InputArgument('role', InputArgument::REQUIRED, 'ROLE'),
- ))
- ->setHelp('');
- }
-
- /**
- * {@inheritdoc}
- */
- protected function execute(InputInterface $input, OutputInterface $output)
- {
-
- $email = $input->getArgument('email');
- $password = $input->getArgument('password');
- $role = $input->getArgument('role');
-
- $user = $this->em->new(UserInterface::class);
- $user->setEmail($email);
- $user->setRoles(array($role));
-
- $password = $this->passwordEncoder->encodePassword($user, $password);
-
- $user->setPassword($password);
-
- $this->em->create($user);
- $this->em->flush();
-
- $output->writeln(sprintf('Created user <comment>%s</comment>', $email));
- return Command::SUCCESS;
- }
-
- /**
- * {@inheritdoc}
- */
- protected function interact(InputInterface $input, OutputInterface $output)
- {
- $questions = array();
-
- if (!$input->getArgument('email')) {
- $question = new Question('Email : ');
- $question->setValidator(function ($password) {
- if (empty($password)) {
- throw new \Exception('Email can not be empty');
- }
-
- return $password;
- });
- $questions['email'] = $question;
- }
-
- if (!$input->getArgument('password')) {
- $question = new Question('Password : ');
- $question->setValidator(function ($password) {
- if (empty($password)) {
- throw new \Exception('Password can not be empty');
- }
-
- return $password;
- });
- $question->setHidden(true);
- $questions['password'] = $question;
- }
-
- if (!$input->getArgument('role')) {
- $question = new Question('Rôle [admin/superadmin/user] : ');
- $question->setValidator(function ($role) {
- if ($role == 'admin') {
- $role = 'ROLE_ADMIN';
- } else if ($role == 'superadmin') {
- $role = 'ROLE_SUPER_ADMIN';
- } else if ($role == 'user') {
- $role = 'ROLE_USER';
- } else {
- throw new \Exception('Choose a role');
-
- }
- return $role;
- });
- $questions['role'] = $question;
- }
-
-
- foreach ($questions as $name => $question) {
- $answer = $this->getHelper('question')->ask($input, $output, $question);
- $input->setArgument($name, $answer);
- }
- }
- }
|