PiedWeb /
CMS
| 1 | <?php |
||
| 2 | |||
| 3 | namespace PiedWeb\CMSBundle\Command; |
||
| 4 | |||
| 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\Output\OutputInterface; |
||
| 10 | use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; |
||
| 11 | |||
| 12 | class UserCreateCommand extends Command |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var EntityManagerInterface |
||
| 16 | */ |
||
| 17 | private $em; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | private $userClass; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var UserPasswordEncoderInterface |
||
| 26 | */ |
||
| 27 | private $passwordEncoder; |
||
| 28 | |||
| 29 | public function __construct( |
||
| 30 | EntityManagerInterface $em, |
||
| 31 | UserPasswordEncoderInterface $passwordEncoder, |
||
| 32 | $userClass |
||
| 33 | ) { |
||
| 34 | $this->em = $em; |
||
| 35 | $this->passwordEncoder = $passwordEncoder; |
||
| 36 | $this->userClass = $userClass; |
||
| 37 | |||
| 38 | parent::__construct(); |
||
| 39 | } |
||
| 40 | |||
| 41 | protected function configure() |
||
| 42 | { |
||
| 43 | $this |
||
| 44 | ->setName('piedweb:user:create') |
||
| 45 | ->setDescription('Create a new user') |
||
| 46 | ->addArgument('email', InputArgument::REQUIRED) |
||
| 47 | ->addArgument('password', InputArgument::REQUIRED) |
||
| 48 | ->addArgument('role', InputArgument::REQUIRED); |
||
| 49 | } |
||
| 50 | |||
| 51 | protected function createUser($email, $password, $role) |
||
| 52 | { |
||
| 53 | $userClass = $this->userClass; |
||
| 54 | $user = new $userClass(); |
||
| 55 | $user->setEmail($email); |
||
| 56 | $user->setPassword($this->passwordEncoder->encodePassword($user, $password)); |
||
| 57 | $user->setRoles([$role]); |
||
| 58 | |||
| 59 | $this->em->persist($user); |
||
| 60 | $this->em->flush(); |
||
| 61 | } |
||
| 62 | |||
| 63 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 64 | { |
||
| 65 | $this->createUser($input->getArgument('email'), $input->getArgument('password'), $input->getArgument('role')); |
||
| 66 | |||
| 67 | $output->writeln('<info>User `'.$input->getArgument('email').'` created with success.</info>'); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 68 | |||
| 69 | return 0; |
||
| 70 | } |
||
| 71 | } |
||
| 72 |