Last active
July 10, 2019 13:59
-
-
Save romaricdrigon/f461615c47f067383ae6d219303e55b9 to your computer and use it in GitHub Desktop.
Commandes pratiques portées du FOSUserBundle: créer un utilisateur, changer un mot de passe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Command; | |
// Adapter App\Entity\User selon la classe réelle de votre utilisateur | |
use App\Entity\User; | |
use Doctrine\ORM\EntityManagerInterface; | |
use Symfony\Component\Console\Command\Command; | |
use Symfony\Component\Console\Input\InputArgument; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Input\InputOption; | |
use Symfony\Component\Console\Output\OutputInterface; | |
use Symfony\Component\Console\Question\Question; | |
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; | |
class ChangePasswordCommand extends Command | |
{ | |
private $passwordEncoder; | |
private $entityManager; | |
public function __construct(UserPasswordEncoderInterface $passwordEncoder, EntityManagerInterface $entityManager) | |
{ | |
parent::__construct(); | |
$this->passwordEncoder = $passwordEncoder; | |
$this->entityManager = $entityManager; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
protected function configure() | |
{ | |
$this | |
->setName('user:change-password') | |
->setDescription('Change a user password.') | |
->addArgument('email', InputArgument::REQUIRED, 'The user email') | |
->addArgument('password', InputArgument::REQUIRED, 'The new password (if blank, will be interactively asked)') | |
; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
protected function execute(InputInterface $input, OutputInterface $output) | |
{ | |
$email = $input->getArgument('email'); | |
// Vous voulez peut-être remplacer "email" par un autre champ (ie, "username") | |
$user = $this->entityManager->getRepository(User::class)->findOneBy([ | |
'email' => $email, | |
]); | |
if (!$email) { | |
throw new \Exception('Unable to find a matching User for given e-mail address'); | |
} | |
$password = $this->passwordEncoder->encodePassword($user, $input->getArgument('password')); | |
$user->setPassword($password); | |
$this->entityManager->persist($user); | |
$this->entityManager->flush(); | |
$output->writeln(sprintf('<comment>Updated user %s password</comment>', $email)); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
protected function interact(InputInterface $input, OutputInterface $output) | |
{ | |
$questions = array(); | |
if (!$input->getArgument('password')) { | |
$question = new Question('Please enter new password:'); | |
$question->setValidator(function ($password) { | |
if (empty($password)) { | |
throw new \Exception('Password can not be empty'); | |
} | |
return $password; | |
}); | |
$question->setHidden(true); | |
$questions['password'] = $question; | |
} | |
foreach ($questions as $name => $question) { | |
$answer = $this->getHelper('question')->ask($input, $output, $question); | |
$input->setArgument($name, $answer); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Command; | |
// Adapter App\Entity\User selon la classe réelle de votre utilisateur | |
use App\Entity\User; | |
use Doctrine\ORM\EntityManagerInterface; | |
use Symfony\Component\Console\Command\Command; | |
use Symfony\Component\Console\Input\InputArgument; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Input\InputOption; | |
use Symfony\Component\Console\Output\OutputInterface; | |
use Symfony\Component\Console\Question\Question; | |
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; | |
class CreateUserCommand extends Command | |
{ | |
private $passwordEncoder; | |
private $entityManager; | |
public function __construct(UserPasswordEncoderInterface $passwordEncoder, EntityManagerInterface $entityManager) | |
{ | |
parent::__construct(); | |
$this->passwordEncoder = $passwordEncoder; | |
$this->entityManager = $entityManager; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
protected function configure() | |
{ | |
$this | |
->setName('user:create') | |
->setDescription('Create a user.') | |
->setDefinition(array( | |
new InputArgument('email', InputArgument::REQUIRED, 'The email'), | |
new InputArgument('password', InputArgument::REQUIRED, 'The password'), | |
new InputOption('super-admin', null, InputOption::VALUE_NONE, 'Set the user as super admin (ROLE_SUPER_ADMIN)'), | |
)) | |
->setHelp(<<<'EOT' | |
The <info>user:create</info> command creates a user: | |
<info>php %command.full_name% [email protected]</info> | |
This interactive shell will ask you for a password. | |
You can create a super admin via the super-admin flag: | |
<info>php %command.full_name% admin --super-admin</info> | |
EOT | |
); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
protected function execute(InputInterface $input, OutputInterface $output) | |
{ | |
$email = $input->getArgument('email'); | |
$password = $input->getArgument('password'); | |
$superadmin = $input->getOption('super-admin'); | |
$user = (new User()) | |
->setEmail($email) | |
->setRoles($superadmin ? ['ROLE_SUPER_ADMIN'] : ['ROLE_USER']) | |
; | |
$password = $this->passwordEncoder->encodePassword($user, $password); | |
$user->setPassword($password); | |
$this->entityManager->persist($user); | |
$this->entityManager->flush(); | |
$output->writeln(sprintf('Created user <comment>%s</comment>', $email)); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
protected function interact(InputInterface $input, OutputInterface $output) | |
{ | |
$questions = array(); | |
if (!$input->getArgument('password')) { | |
$question = new Question('Please choose a password:'); | |
$question->setValidator(function ($password) { | |
if (empty($password)) { | |
throw new \Exception('Password can not be empty'); | |
} | |
return $password; | |
}); | |
$question->setHidden(true); | |
$questions['password'] = $question; | |
} | |
foreach ($questions as $name => $question) { | |
$answer = $this->getHelper('question')->ask($input, $output, $question); | |
$input->setArgument($name, $answer); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment