<?php
namespace App\Command;
use App\Entity\User;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
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 AppCreateAdminCommand extends ContainerAwareCommand
{
private $encoder;
public function __construct(UserPasswordEncoderInterface $encoder)
{
$this->encoder = $encoder;
parent::__construct();
}
protected function configure()
{
$this
->setName('app:createAdmin')
->setDescription('Create admin user')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$em = $this->getContainer()->get('doctrine')->getManager();
$helper = $this->getHelper('question');
$question = new Question('username ? ');
$username = $helper->ask($input, $output, $question);
$question = new Question('password ? ');
$password = $helper->ask($input, $output, $question);
$question = new Question('email ? ');
$email = $helper->ask($input, $output, $question);
$user = new User();
$user->setPassword($this->encoder->encodePassword($user, $password));
$user->setUsername($username);
$user->setEmail($email);
$user->setRoles(["ROLE_SUPER_ADMIN"]);
$em->persist($user);
$em->flush();
$output->write("Admin created: ". $username . "\n");
return 0;
}
}