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.

126 lines
4.6KB

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