From 8ddbf9c5d6afd6f5ab72070e8a21907df07e39d0 Mon Sep 17 00:00:00 2001 From: Olivier PARPAILLON Date: Tue, 19 Nov 2024 13:07:07 +0100 Subject: [PATCH] authentication done --- config/packages/security.yaml | 12 ++++- src/Controller/LoginController.php | 40 ++++++++++++++++ src/Controller/RegistrationController.php | 15 ++++-- src/Form/RegistrationFormType.php | 46 +++++++++++-------- templates/auth/login.html.twig | 33 +++++++++++++ .../{registration => auth}/register.html.twig | 9 ++-- templates/main/header.html.twig | 13 +++--- 7 files changed, 134 insertions(+), 34 deletions(-) create mode 100644 src/Controller/LoginController.php create mode 100644 templates/auth/login.html.twig rename templates/{registration => auth}/register.html.twig (60%) diff --git a/config/packages/security.yaml b/config/packages/security.yaml index 8b052ba..058188d 100644 --- a/config/packages/security.yaml +++ b/config/packages/security.yaml @@ -7,7 +7,7 @@ security: # used to reload user from session & other features (e.g. switch_user) app_user_provider: entity: - class: App\Entity\User + class: App\Entity\Participant property: email # used to reload user from session & other features (e.g. switch_user) # used to reload user from session & other features (e.g. switch_user) @@ -18,6 +18,16 @@ security: main: lazy: true provider: app_user_provider + form_login: + login_path: app_login + check_path: app_login + enable_csrf: true + default_target_path: home + always_use_default_target_path: true + logout: + path: app_logout + # where to redirect after logout + # target: app_any_route # activate different ways to authenticate # https://symfony.com/doc/current/security.html#the-firewall diff --git a/src/Controller/LoginController.php b/src/Controller/LoginController.php new file mode 100644 index 0000000..5ff415e --- /dev/null +++ b/src/Controller/LoginController.php @@ -0,0 +1,40 @@ +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); + } +} diff --git a/src/Controller/RegistrationController.php b/src/Controller/RegistrationController.php index 3b24f86..915e886 100644 --- a/src/Controller/RegistrationController.php +++ b/src/Controller/RegistrationController.php @@ -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, ]); } diff --git a/src/Form/RegistrationFormType.php b/src/Form/RegistrationFormType.php index 6c92c0b..c9ac6c7 100644 --- a/src/Form/RegistrationFormType.php +++ b/src/Form/RegistrationFormType.php @@ -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, ]), ], diff --git a/templates/auth/login.html.twig b/templates/auth/login.html.twig new file mode 100644 index 0000000..d775802 --- /dev/null +++ b/templates/auth/login.html.twig @@ -0,0 +1,33 @@ +{% extends 'main/base.html.twig' %} +{% block head %} + + + {% block stylesheets %} + {{ encore_entry_link_tags('app') }} + {% endblock %} + +{% endblock %} + +{% block content %} +
+
+

Connectez-vous !

+
+ + + + + + +
+ + + + +
+
+
+
+{% endblock %} diff --git a/templates/registration/register.html.twig b/templates/auth/register.html.twig similarity index 60% rename from templates/registration/register.html.twig rename to templates/auth/register.html.twig index 5cd77ec..9e2b65f 100644 --- a/templates/registration/register.html.twig +++ b/templates/auth/register.html.twig @@ -10,14 +10,17 @@ {% endblock %} {% block content %} -
+
-

Sortir !

-

Bienvenue sur notre application web référencant tout type d'évènements à travers le monde. N'hésitez pas à vous inscrire sur notre plateforme et y invitez vos amis afins de participer à des évènements simplement et qui vous conviennent !

+

Sortir !

+

Bienvenue sur notre application web référencant tout type d'évènements à travers le monde. N'hésitez pas à vous inscrire sur notre plateforme et y invitez vos amis afins de participer à des évènements simplement et qui vous conviennent !

S'inscrire

{{ form_start(registrationForm) }} + {{ form_row(registrationForm.prenom) }} + {{ form_row(registrationForm.nom) }} + {{ form_row(registrationForm.telephone) }} {{ form_row(registrationForm.email) }} {{ form_row(registrationForm.plainPassword) }} diff --git a/templates/main/header.html.twig b/templates/main/header.html.twig index 369df9a..f76085d 100644 --- a/templates/main/header.html.twig +++ b/templates/main/header.html.twig @@ -27,13 +27,12 @@
  • Sortie
  • ToDo
  • À propos
  • - {#
  • Contact
  • #} -{# {% if app.user %}#} -{#
  • Se déconnecter
  • #} -{# {% else %}#} -{#
  • S'inscrire
  • #} -{#
  • Se connecter
  • #} -{# {% endif %}#} + {% if app.user %} +
  • Se déconnecter
  • + {% else %} +
  • S'inscrire
  • +
  • Se connecter
  • + {% endif %}