Skip to content

Instantly share code, notes, and snippets.

@wpottier
Created September 6, 2012 13:17
Show Gist options
  • Save wpottier/3656149 to your computer and use it in GitHub Desktop.
Save wpottier/3656149 to your computer and use it in GitHub Desktop.
CreateUserCommand
<?php
namespace Mon\Site\UserBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\OutputInterface;
use Mon\Site\UserBundle\Entity as Entity;
class CreateUserCommand extends ContainerAwareCommand {
/**
* Configures the current command.
*/
protected function configure () {
$this
->addOption('email', '', InputOption::VALUE_REQUIRED, 'Email')
->setName('users:initialize')
->addOption('password', '', InputOption::VALUE_REQUIRED, 'Password')
->addOption('firstname', '', InputOption::VALUE_REQUIRED, 'Firstname')
->addOption('lastname', '', InputOption::VALUE_REQUIRED, 'Lastname')
;
}
protected function interact (InputInterface $input, OutputInterface $output) {
// Display Header
$dialog = $this->getDialogHelper();
$output->writeln(array(
'<info></info>',
'<info> Interactive user creation </info>',
'<info></info>'
));
$email = $dialog->ask(
$output,
$dialog->getQuestion('Administrator email', $input->getOption('email')),
$input->getOption('email')
);
$password = $dialog->ask(
$output,
$dialog->getQuestion('Administrator password', $input->getOption('password')),
$input->getOption('password')
);
$firstname = $dialog->ask(
$output,
$dialog->getQuestion('Administrator firstname', $input->getOption('firstname')),
$input->getOption('firstname')
);
$lastname = $dialog->ask(
$output,
$dialog->getQuestion('Administrator lastname', $input->getOption('lastname')),
$input->getOption('lastname')
);
// On sauvegarde les paramètres
$input->setOption('password', $password);
$input->setOption('firstname', $firstname);
$input->setOption('lastname', $lastname);
$input->setOption('email', $email);
}
private function createDefaultUser (InputInterface $input, OutputInterface $output) {
// Create database
$arguments = array(
'password' => $input->getOption('password'),
'firstname' => $input->getOption('firstname'),
'lastname' => $input->getOption('lastname'),
'email' => $input->getOption('email')
);
$commandUsersInit = $this->getApplication()->find('glit:users:initialize');
$commandUsersInit->run(new ArrayInput($arguments), $output);
}
/**
* Executes the current command.
*
* This method is not abstract because you can use this class
* as a concrete class. In this case, instead of defining the
* execute() method, you set the code to execute by passing
* a Closure to the setCode() method.
*
* @param InputInterface $input An InputInterface instance
* @param OutputInterface $output An OutputInterface instance
*
* @return integer 0 if everything went fine, or an error code
*/
protected function execute (InputInterface $input, OutputInterface $output) {
/** @var $doctrine \Doctrine\Bundle\DoctrineBundle\Registry */
$doctrine = $this->getContainer()->get('doctrine');
/** @var $em \Doctrine\Common\Persistence\ObjectManager */
$em = $doctrine->getManager();
$user = new Entity\User();
$user->setFirstname($input->getOption('firstname'));
$user->setLastname($input->getOption('lastname'));
$user->setEmail($input->getOption('email'));
$encoder = $this->getEncoderFactory()->getEncoder($user);
$user->setPassword($encoder->encodePassword($input->getOption('password'), $user->getSalt()));
$em->persist($user);
$em->flush();
$output->write('<info>
The user was successfully created !
</info>');
return 0;
}
/**
* @return \Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface
*/
protected function getEncoderFactory () {
return $this->getContainer()->get('security.encoder_factory');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment