Command voor aanmaken user
This commit is contained in:
@@ -0,0 +1,66 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Command;
|
||||||
|
|
||||||
|
use App\Tech\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\Console\Style\SymfonyStyle;
|
||||||
|
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
|
||||||
|
|
||||||
|
#[AsCommand(
|
||||||
|
name: 'app:create-user',
|
||||||
|
description: 'Create a new user',
|
||||||
|
)]
|
||||||
|
class CreateUserCommand extends Command
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private EntityManagerInterface $entityManager,
|
||||||
|
private UserPasswordHasherInterface $passwordHasher,
|
||||||
|
) {
|
||||||
|
parent::__construct();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function configure(): void
|
||||||
|
{
|
||||||
|
$this
|
||||||
|
->addArgument('email', InputArgument::REQUIRED, 'Email address')
|
||||||
|
->addArgument('username', InputArgument::REQUIRED, 'Username')
|
||||||
|
->addArgument('password', InputArgument::REQUIRED, 'Plain-text password')
|
||||||
|
->addOption('admin', null, InputOption::VALUE_NONE, 'Grant ROLE_ADMIN');
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||||
|
{
|
||||||
|
$io = new SymfonyStyle($input, $output);
|
||||||
|
|
||||||
|
$email = $input->getArgument('email');
|
||||||
|
$username = $input->getArgument('username');
|
||||||
|
$password = $input->getArgument('password');
|
||||||
|
$isAdmin = $input->getOption('admin');
|
||||||
|
|
||||||
|
$user = new User();
|
||||||
|
$user->setEmail($email);
|
||||||
|
$user->setUsername($username);
|
||||||
|
$user->setPassword($this->passwordHasher->hashPassword($user, $password));
|
||||||
|
$user->setIsVerified(true);
|
||||||
|
$user->setRoles($isAdmin ? ['ROLE_ADMIN'] : []);
|
||||||
|
|
||||||
|
$this->entityManager->persist($user);
|
||||||
|
$this->entityManager->flush();
|
||||||
|
|
||||||
|
$io->success(sprintf(
|
||||||
|
'User "%s" (%s) created%s.',
|
||||||
|
$username,
|
||||||
|
$email,
|
||||||
|
$isAdmin ? ' with ROLE_ADMIN' : '',
|
||||||
|
));
|
||||||
|
|
||||||
|
return Command::SUCCESS;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user