authentication done

This commit is contained in:
Olivier PARPAILLON
2024-11-19 13:07:07 +01:00
parent 51fcbba8b2
commit 8ddbf9c5d6
7 changed files with 134 additions and 34 deletions

View File

@@ -0,0 +1,40 @@
<?php
namespace App\Controller;
use App\Entity\Participant;
use App\Form\RegistrationFormType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class LoginController extends AbstractController
{
#[Route(path: '/login', name: 'app_login')]
public function login(AuthenticationUtils $authenticationUtils, Security $security, Request $request): Response
{
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
$user = new Participant();
$formUser = $this->createForm(RegistrationFormType::class, $user);
$formUser->handleRequest($request);
return $this->render('auth/login.html.twig', [
'last_username' => $lastUsername,
'error' => $error,
'formUser' => $formUser,
]);
}
#[Route(path: '/logout', name: 'app_logout')]
public function logout(Security $security): ?Response
{
// throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
return $security->logout(false);
}
}

View File

@@ -6,6 +6,7 @@ use App\Entity\Participant;
use App\Form\RegistrationFormType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
@@ -14,7 +15,7 @@ use Symfony\Component\Routing\Attribute\Route;
class RegistrationController extends AbstractController
{
#[Route('/register', name: 'app_register')]
public function register(Request $request, UserPasswordHasherInterface $userPasswordHasher, EntityManagerInterface $entityManager): Response
public function register(Request $request, UserPasswordHasherInterface $userPasswordHasher, EntityManagerInterface $entityManager, Security $security,): Response
{
$user = new Participant();
$form = $this->createForm(RegistrationFormType::class, $user);
@@ -26,16 +27,24 @@ class RegistrationController extends AbstractController
// encode the plain password
$user->setPassword($userPasswordHasher->hashPassword($user, $plainPassword));
$user->setRoles(['ROLE_USER']);
$user->setActif(false);
if ($user->getRoles() == 'ROLE_ADMIN') {
$user->setAdministrateur(true);
} else {
$user->setAdministrateur(false);
}
$entityManager->persist($user);
$entityManager->flush();
// do anything else you need here, like send an email
return $this->redirectToRoute('home');
// return $this->redirectToRoute('home');
return $security->login($user, 'form_login', 'main');
}
return $this->render('registration/register.html.twig', [
return $this->render('auth/register.html.twig', [
'registrationForm' => $form,
]);
}

View File

@@ -4,13 +4,12 @@ namespace App\Form;
use App\Entity\Participant;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\IsTrue;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
@@ -28,26 +27,34 @@ class RegistrationFormType extends AbstractType
],
'constraints' => [
new NotBlank([
'message' => 'Please enter a password',
'message' => 'Please enter an email address',
]),
],
])
// ->add('username', TextType::class, [
// 'label' => 'Nom d\'utilisateur',
// 'label_attr' => ['class' => 'text-gray-700 font-bold'],
// 'attr' => [
// 'class' => 'w-full mb-4 px-4 py-2 border-2 border-gray-300 rounded-lg focus:outline-none focus:border-blue-500',
// 'placeholder' => 'Nom d\'utilisateur',
// ],
// ])
// ->add('agreeTerms', CheckboxType::class, [
// 'mapped' => false,
// 'constraints' => [
// new IsTrue([
// 'message' => 'You should agree to our terms.',
// ]),
// ],
// ])
->add('prenom', TextType::class, [
'label' => 'Prénom',
'label_attr' => ['class' => 'text-gray-700 font-bold'],
'attr' => [
'class' => 'w-full mb-4 px-4 py-2 border-2 border-gray-300 rounded-lg focus:outline-none focus:border-blue-500',
'placeholder' => 'Prénom',
],
])
->add('nom', TextType::class, [
'label' => 'Nom',
'label_attr' => ['class' => 'text-gray-700 font-bold'],
'attr' => [
'class' => 'w-full mb-4 px-4 py-2 border-2 border-gray-300 rounded-lg focus:outline-none focus:border-blue-500',
'placeholder' => 'Nom',
],
])
->add('telephone', IntegerType::class, [
'label' => 'Numéro de téléphone',
'label_attr' => ['class' => 'text-gray-700 font-bold'],
'attr' => [
'class' => 'w-full mb-4 px-4 py-2 border-2 border-gray-300 rounded-lg focus:outline-none focus:border-blue-500',
'placeholder' => 'Numéro de téléphone',
],
])
->add('plainPassword', PasswordType::class, [
// instead of being set onto the object directly,
// this is read and encoded in the controller
@@ -66,7 +73,6 @@ class RegistrationFormType extends AbstractType
new Length([
'min' => 6,
'minMessage' => 'Your password should be at least {{ limit }} characters',
// max length allowed by Symfony for security reasons
'max' => 4096,
]),
],