Last active
April 21, 2025 08:46
-
-
Save herveGuigoz/8ed013c5e376046ff0c32b69763b297a to your computer and use it in GitHub Desktop.
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 characters
<?php | |
namespace App\Command; | |
use App\Entity\User; | |
use Doctrine\ORM\EntityManagerInterface; | |
use Symfony\Component\Console\Attribute\AsCommand; | |
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\PasswordHasher\Hasher\UserPasswordHasherInterface; | |
/** | |
* Command to create a new user in the application. | |
* | |
* Usage: php bin/console app:create-user [email protected] password [--admin] | |
* | |
* The --admin flag is optional and will grant the user administrative privileges. | |
* This command is useful for creating initial users or administrators without | |
* going through the registration process. | |
*/ | |
#[AsCommand( | |
name: 'app:create-user', | |
description: 'Creates a new user with an optional admin role.', | |
)] | |
class CreateUserCommand extends Command | |
{ | |
private EntityManagerInterface $entityManager; | |
private UserPasswordHasherInterface $passwordHasher; | |
public function __construct(EntityManagerInterface $entityManager, UserPasswordHasherInterface $passwordHasher) | |
{ | |
parent::__construct(); | |
$this->entityManager = $entityManager; | |
$this->passwordHasher = $passwordHasher; | |
} | |
protected function configure(): void | |
{ | |
$this | |
->addArgument('email', InputArgument::REQUIRED, 'User email') | |
->addArgument('password', InputArgument::REQUIRED, 'User password') | |
->addOption('admin', null, InputOption::VALUE_NONE, 'Set this user as an admin'); | |
} | |
protected function execute(InputInterface $input, OutputInterface $output): int | |
{ | |
$email = $input->getArgument('email'); | |
$password = $input->getArgument('password'); | |
$isAdmin = $input->getOption('admin'); | |
// Create user instance | |
$user = new User(); | |
$user->setEmail($email); | |
// Hash password | |
$hashedPassword = $this->passwordHasher->hashPassword($user, $password); | |
$user->setPassword($hashedPassword); | |
// Set roles | |
$roles = ['ROLE_USER']; | |
if ($isAdmin) { | |
$roles[] = 'ROLE_ADMIN'; | |
} | |
$user->setRoles($roles); | |
// Persist and save user | |
$this->entityManager->persist($user); | |
$this->entityManager->flush(); | |
$output->writeln(sprintf('<info>User "%s" created successfully.</info>', $email)); | |
if ($isAdmin) { | |
$output->writeln('<comment>User has admin privileges.</comment>'); | |
} | |
return Command::SUCCESS; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment