You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 line
5.0KB

  1. <?php
  2. namespace App\Command;
  3. // Adapter App\Entity\User selon la classe réelle de votre utilisateur
  4. use App\Entity\User;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Component\Console\Command\Command;
  7. use Symfony\Component\Console\Input\InputArgument;
  8. use Symfony\Component\Console\Input\InputInterface;
  9. use Symfony\Component\Console\Input\InputOption;
  10. use Symfony\Component\Console\Output\OutputInterface;
  11. use Symfony\Component\Console\Question\Question;
  12. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  13. class CreateUserCommand extends Command
  14. {
  15. private $passwordEncoder;
  16. private $entityManager;
  17. public function __construct(UserPasswordEncoderInterface $passwordEncoder, EntityManagerInterface $entityManager)
  18. {
  19. parent::__construct();
  20. $this->passwordEncoder = $passwordEncoder;
  21. $this->entityManager = $entityManager;
  22. }
  23. /**
  24. * {@inheritdoc}
  25. */
  26. protected function configure()
  27. {
  28. $this
  29. ->setName('user:create')
  30. ->setDescription('Create a user.')
  31. ->setDefinition(array(
  32. new InputArgument('email', InputArgument::REQUIRED, 'The email'),
  33. new InputArgument('password', InputArgument::REQUIRED, 'The password'),
  34. new InputArgument('role', InputArgument::REQUIRED, 'ROLE'),
  35. ))
  36. ->setHelp(<<<'EOT'
  37. The <info>user:create</info> command creates a user:
  38. <info>php %command.full_name% romaric@netinfluence.ch</info>
  39. This interactive shell will ask you for a password.
  40. You can create a super admin via the super-admin flag:
  41. <info>php %command.full_name% admin --super-admin</info>
  42. EOT
  43. );
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. protected function execute(InputInterface $input, OutputInterface $output)
  49. {
  50. $email = $input->getArgument('email');
  51. $password = $input->getArgument('password');
  52. $role = $input->getArgument('role');
  53. $user = (new User())
  54. ->setEmail($email)
  55. ->setRoles(array($role))
  56. ;
  57. $password = $this->passwordEncoder->encodePassword($user, $password);
  58. $user->setPassword($password);
  59. $this->entityManager->persist($user);
  60. $this->entityManager->flush();
  61. $output->writeln(sprintf('Created user <comment>%s</comment>', $email));
  62. return Command::SUCCESS;
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. protected function interact(InputInterface $input, OutputInterface $output)
  68. {
  69. $questions = array();
  70. if (!$input->getArgument('email')) {
  71. $question = new Question('Email : ');
  72. $question->setValidator(function ($password) {
  73. if (empty($password)) {
  74. throw new \Exception('Email can not be empty');
  75. }
  76. return $password;
  77. });
  78. $questions['email'] = $question;
  79. }
  80. if (!$input->getArgument('password')) {
  81. $question = new Question('Password : ');
  82. $question->setValidator(function ($password) {
  83. if (empty($password)) {
  84. throw new \Exception('Password can not be empty');
  85. }
  86. return $password;
  87. });
  88. $question->setHidden(true);
  89. $questions['password'] = $question;
  90. }
  91. if (!$input->getArgument('role')) {
  92. $question = new Question('Rôle [admin/superadmin/user] : ');
  93. $question->setValidator(function ($role) {
  94. if ($role == 'admin') {
  95. $role = 'ROLE_ADMIN';
  96. }else if ($role == 'superadmin') {
  97. $role = 'ROLE_SUPER_ADMIN';
  98. }else if ($role == 'user') {
  99. $role = 'ROLE_USER';
  100. }else{
  101. throw new \Exception('Choose a role');
  102. }
  103. return $role;
  104. });
  105. $questions['role'] = $question;
  106. }
  107. foreach ($questions as $name => $question) {
  108. $answer = $this->getHelper('question')->ask($input, $output, $question);
  109. $input->setArgument($name, $answer);
  110. }
  111. }
  112. }