login form added

This commit is contained in:
Joren de Graaf
2026-07-25 22:45:13 +02:00
parent 919ef80e19
commit 1401125706
3 changed files with 107 additions and 1 deletions
+3
View File
@@ -1,4 +1,6 @@
security:
role_hierarchy:
ROLE_ADMIN: ROLE_USER
# https://symfony.com/doc/current/security.html#registering-the-user-hashing-passwords
password_hashers:
Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto'
@@ -42,6 +44,7 @@ security:
- { path: ^/_profiler, roles: PUBLIC_ACCESS }
- { path: ^/_wdt, roles: PUBLIC_ACCESS }
- { path: ^/, roles: ROLE_USER }
when@test:
security:
password_hashers:
+1 -1
View File
@@ -24,7 +24,7 @@ spec:
containers:
- name: symfony
image: symfony-k3s:dev-2
image: symfony-k3s:dev-5
imagePullPolicy: Never
ports:
+103
View File
@@ -0,0 +1,103 @@
<?php
declare(strict_types=1);
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;
#[AsCommand(
name: 'app:user:create',
description: 'Creates a user account.',
)]
final class CreateUserCommand extends Command
{
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly UserPasswordHasherInterface $passwordHasher,
) {
parent::__construct();
}
protected function configure(): void
{
$this
->addArgument(
'email',
InputArgument::REQUIRED,
'Email address'
)
->addArgument(
'password',
InputArgument::REQUIRED,
'Plain-text password'
)
->addOption(
'admin',
null,
InputOption::VALUE_NONE,
'Assign ROLE_ADMIN'
);
}
protected function execute(
InputInterface $input,
OutputInterface $output,
): int {
$email = strtolower(trim(
(string) $input->getArgument('email')
));
$plainPassword = (string) $input->getArgument('password');
$existingUser = $this->entityManager
->getRepository(User::class)
->findOneBy(['email' => $email]);
if ($existingUser !== null) {
$output->writeln(
sprintf(
'<error>User "%s" already exists.</error>',
$email
)
);
return Command::FAILURE;
}
$user = new User();
$user->setEmail($email);
$user->setRoles(
$input->getOption('admin')
? ['ROLE_ADMIN']
: ['ROLE_USER']
);
$user->setPassword(
$this->passwordHasher->hashPassword(
$user,
$plainPassword
)
);
$this->entityManager->persist($user);
$this->entityManager->flush();
$output->writeln(
sprintf(
'<info>User "%s" created successfully.</info>',
$email
)
);
return Command::SUCCESS;
}
}