AppUserCreateCommand.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace App\Command;
  3. use Symfony\Component\Console\Command\Command;
  4. use Symfony\Component\Console\Input\InputArgument;
  5. use Symfony\Component\Console\Input\InputInterface;
  6. use Symfony\Component\Console\Input\InputOption;
  7. use Symfony\Component\Console\Output\OutputInterface;
  8. use Symfony\Component\Console\Question\ChoiceQuestion;
  9. use Symfony\Component\Console\Question\Question;
  10. use App\Service\UserManager;
  11. use Symfony\Component\Console\Attribute\AsCommand;
  12. use Symfony\Component\Console\Style\SymfonyStyle;
  13. #[AsCommand(
  14. name: 'app:user:create',
  15. description: 'Créer un utilisateur',
  16. )]
  17. class AppUserCreateCommand extends Command
  18. {
  19. public function __construct(protected UserManager $userManager)
  20. {
  21. parent::__construct();
  22. }
  23. protected function configure(): void
  24. {
  25. $this
  26. ->addArgument('argument', InputArgument::OPTIONAL, 'Argument description')
  27. ->addOption('option', null, InputOption::VALUE_NONE, 'Option description')
  28. ;
  29. }
  30. protected function execute(InputInterface $input, OutputInterface $output) : int
  31. {
  32. $io = new SymfonyStyle($input, $output);
  33. $username = $io->askQuestion(new Question('Nom (username) de l\'utilisateur '));
  34. $prenom = $io->askQuestion(new Question('Prénom de l\'utilisateur '));
  35. $nom = $io->askQuestion(new Question('Nom de l\'utilisateur '));
  36. $mail = $io->askQuestion(new Question('Email ', 'bidule@truc.chose'));
  37. $password = $io->askHidden("Mot de passe");
  38. $roles = $io->askQuestion((new ChoiceQuestion('Roles ', array('ROLE_SUPER_ADMIN', 'ROLE_ADMIN', 'ROLE_USER', 'ROLE_MODERATEUR'), '0'))->setMultiselect(true));
  39. $this->userManager->createUser(
  40. "$username",
  41. "$password",
  42. "$nom",
  43. "$prenom",
  44. "$mail",
  45. $roles,
  46. true);
  47. $io->success('Nom du pélo : '.$username);
  48. return COMMAND::SUCCESS;
  49. }
  50. }