From 15361f28cddf4dd867086cfbb37fae42dbcda34c Mon Sep 17 00:00:00 2001 From: ExostFlash <120869320+ExostFlash@users.noreply.github.com> Date: Tue, 21 Nov 2023 14:49:29 +0100 Subject: [PATCH] Signup Good --- composer.json | 1 + composer.lock | 2 +- config/packages/security.yaml | 66 +++++++++++++++---------------- src/Controller/UserController.php | 32 ++++++++++++++- src/Form/UserLoginType.php | 9 +---- src/Form/UserSignupType.php | 24 +++++++---- templates/base.html.twig | 2 +- templates/user/signup.html.twig | 9 +++++ 8 files changed, 93 insertions(+), 52 deletions(-) diff --git a/composer.json b/composer.json index 70513a3..3ea9d00 100644 --- a/composer.json +++ b/composer.json @@ -26,6 +26,7 @@ "symfony/mime": "6.1.*", "symfony/monolog-bundle": "^3.0", "symfony/notifier": "6.1.*", + "symfony/password-hasher": "6.1.*", "symfony/process": "6.1.*", "symfony/property-access": "6.1.*", "symfony/property-info": "6.1.*", diff --git a/composer.lock b/composer.lock index 95813c4..165f411 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "8c1abe2e8026fc52bc20a96868b85bcf", + "content-hash": "989072ca54ad28f1eb15250b46306f66", "packages": [ { "name": "doctrine/cache", diff --git a/config/packages/security.yaml b/config/packages/security.yaml index 367af25..0d5d0ee 100644 --- a/config/packages/security.yaml +++ b/config/packages/security.yaml @@ -1,39 +1,39 @@ security: - # https://symfony.com/doc/current/security.html#registering-the-user-hashing-passwords - password_hashers: - Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto' - # https://symfony.com/doc/current/security.html#loading-the-user-the-user-provider - providers: - users_in_memory: { memory: null } - firewalls: - dev: - pattern: ^/(_(profiler|wdt)|css|images|js)/ - security: false - main: - lazy: true - provider: users_in_memory + # https://symfony.com/doc/current/security.html#registering-the-user-hashing-passwords + password_hashers: + Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: "auto" + # https://symfony.com/doc/current/security.html#loading-the-user-the-user-provider + providers: + users_in_memory: { memory: null } + firewalls: + dev: + pattern: ^/(_(profiler|wdt)|css|images|js)/ + security: false + main: + lazy: true + provider: users_in_memory - # activate different ways to authenticate - # https://symfony.com/doc/current/security.html#the-firewall + # activate different ways to authenticate + # https://symfony.com/doc/current/security.html#the-firewall - # https://symfony.com/doc/current/security/impersonating_user.html - # switch_user: true + # https://symfony.com/doc/current/security/impersonating_user.html + # switch_user: true - # Easy way to control access for large sections of your site - # Note: Only the *first* access control that matches will be used - access_control: - # - { path: ^/admin, roles: ROLE_ADMIN } - # - { path: ^/profile, roles: ROLE_USER } + # Easy way to control access for large sections of your site + # Note: Only the *first* access control that matches will be used + access_control: + # - { path: ^/admin, roles: ROLE_ADMIN } + # - { path: ^/profile, roles: ROLE_USER } when@test: - security: - password_hashers: - # By default, password hashers are resource intensive and take time. This is - # important to generate secure password hashes. In tests however, secure hashes - # are not important, waste resources and increase test times. The following - # reduces the work factor to the lowest possible values. - Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: - algorithm: auto - cost: 4 # Lowest possible value for bcrypt - time_cost: 3 # Lowest possible value for argon - memory_cost: 10 # Lowest possible value for argon + security: + password_hashers: + # By default, password hashers are resource intensive and take time. This is + # important to generate secure password hashes. In tests however, secure hashes + # are not important, waste resources and increase test times. The following + # reduces the work factor to the lowest possible values. + Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: + algorithm: auto + cost: 4 # Lowest possible value for bcrypt + time_cost: 3 # Lowest possible value for argon + memory_cost: 10 # Lowest possible value for argon diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index 92372fa..377114a 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -2,7 +2,13 @@ namespace App\Controller; +use App\Entity\User; +use App\Form\UserSignupType; +use App\Form\UserLoginType; + use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Doctrine\ORM\EntityManagerInterface; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; @@ -17,10 +23,32 @@ class UserController extends AbstractController } #[Route('/user/signup', name: 'app_user_signup')] - public function signup(): Response + public function signup(Request $request, EntityManagerInterface $entityManager): Response { + $user_entity = new User(); // Remplacez par votre entité User + $form = $this->createForm(UserSignupType::class, $user_entity); + + $form->handleRequest($request); + if ($form->isSubmitted() && $form->isValid()) { + // Récupération du mot de passe en clair depuis le formulaire + $plainPassword = $form->get('mdp')->getData(); + + // Encodage sécurisé du mot de passe + $hashedPassword = password_hash($plainPassword, PASSWORD_DEFAULT); + + // Définition du mot de passe haché sur l'entité User + $user_entity->setMdp($hashedPassword); + + // Enregistrement de l'utilisateur + $entityManager->persist($user_entity); + $entityManager->flush(); + + // Redirection après l'enregistrement + return $this->redirectToRoute('app_user_login'); + } + return $this->render('user/signup.html.twig', [ - 'controller_name' => 'UserController', + 'form' => $form->createView(), ]); } diff --git a/src/Form/UserLoginType.php b/src/Form/UserLoginType.php index 9d86015..ca4f40c 100644 --- a/src/Form/UserLoginType.php +++ b/src/Form/UserLoginType.php @@ -6,20 +6,15 @@ use App\Entity\User; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; +use Symfony\Component\Form\Extension\Core\Type\PasswordType; class UserLoginType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options): void { $builder - ->add('name') - ->add('fullname') - ->add('grade') ->add('mail') - ->add('mdp') - ->add('address') - ->add('id_resto') - ; + ->add('mdp', PasswordType::class); } public function configureOptions(OptionsResolver $resolver): void diff --git a/src/Form/UserSignupType.php b/src/Form/UserSignupType.php index a6fccb9..961dd54 100644 --- a/src/Form/UserSignupType.php +++ b/src/Form/UserSignupType.php @@ -6,20 +6,28 @@ use App\Entity\User; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; +use Symfony\Component\Form\Extension\Core\Type\PasswordType; class UserSignupType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options): void { $builder - ->add('name') - ->add('fullname') - ->add('grade') - ->add('mail') - ->add('mdp') - ->add('address') - ->add('id_resto') - ; + ->add('name', null, [ + 'attr' => ['placeholder' => 'Entrez votre nom de famille'] + ]) + ->add('fullname', null, [ + 'attr' => ['placeholder' => 'Entrez votre prénom'] + ]) + ->add('mail', null, [ + 'attr' => ['placeholder' => 'exemple@exostflash.ovh'] + ]) + ->add('mdp', PasswordType::class, [ + 'attr' => ['placeholder' => 'Entrez votre mot de passe'] + ]) + ->add('address', null, [ + 'attr' => ['placeholder' => '265 chemin de l\'exemple, 31840 Exemple'] + ]); } public function configureOptions(OptionsResolver $resolver): void diff --git a/templates/base.html.twig b/templates/base.html.twig index fca6a0f..bb1d62a 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -1,5 +1,5 @@ - +