Last active
July 10, 2019 13:59
-
-
Save romaricdrigon/f461615c47f067383ae6d219303e55b9 to your computer and use it in GitHub Desktop.
Revisions
-
romaricdrigon revised this gist
May 10, 2019 . 2 changed files with 3 additions and 17 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -15,14 +15,7 @@ class ChangePasswordCommand extends Command { private $passwordEncoder; private $entityManager; public function __construct(UserPasswordEncoderInterface $passwordEncoder, EntityManagerInterface $entityManager) @@ -42,7 +35,7 @@ protected function configure() ->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)') ; } @@ -53,7 +46,7 @@ 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, ]); This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -15,14 +15,7 @@ class CreateUserCommand extends Command { private $passwordEncoder; private $entityManager; public function __construct(UserPasswordEncoderInterface $passwordEncoder, EntityManagerInterface $entityManager) @@ -44,7 +37,7 @@ protected function configure() ->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: -
romaricdrigon created this gist
May 10, 2019 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,100 @@ <?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 { /** * @var UserPasswordEncoderInterface */ private $passwordEncoder; /** * @var EntityManagerInterface */ 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') ; } /** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $email = $input->getArgument('email'); // Vous voulez peut-être remplacé "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 hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,112 @@ <?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 { /** * @var UserPasswordEncoderInterface */ private $passwordEncoder; /** * @var EntityManagerInterface */ 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'), )) ->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); } } }