diff --git a/src/Controller/ProfileController.php b/src/Controller/ProfileController.php index 3a14faa..e33a8e6 100644 --- a/src/Controller/ProfileController.php +++ b/src/Controller/ProfileController.php @@ -13,6 +13,7 @@ use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; use Symfony\Component\Routing\Attribute\Route; class ProfileController extends AbstractController @@ -50,12 +51,19 @@ class ProfileController extends AbstractController } #[Route('profile/{id}/edit', name: 'app_profile_edit', methods: ['GET', 'POST'])] - public function edit(Request $request, UserApp $userApp,): Response + public function edit(Request $request, UserApp $userApp, UserPasswordHasherInterface $passwordHasher): Response { $form = $this->createForm(UserAppType::class, $userApp); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { + $plainPassword = $form->get('password')->getData(); + + if (!empty($plainPassword)) { + $hashedPassword = $passwordHasher->hashPassword($userApp, $plainPassword); + $userApp->setPassword($hashedPassword); + } + $this->entityManager->flush(); return $this->redirectToRoute('app_profile', [], Response::HTTP_SEE_OTHER); diff --git a/src/Form/UserAppType.php b/src/Form/UserAppType.php index 3af7796..d6ffcb4 100644 --- a/src/Form/UserAppType.php +++ b/src/Form/UserAppType.php @@ -4,6 +4,7 @@ namespace App\Form; use App\Entity\UserApp; use Symfony\Component\Form\AbstractType; +use Symfony\Component\Form\Extension\Core\Type\PasswordType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -13,12 +14,19 @@ class UserAppType extends AbstractType { $builder ->add('nickname') - ->add('password') ->add('firstName') ->add('lastName') ->add('tel') ->add('address') ->add('mail') + ->add('password', PasswordType::class, [ + 'mapped' => false, + 'required' => false, + 'attr' => [ + 'autocomplete' => 'new-password' + ], + 'label' => 'Nouveau mot de passe', + ]) ; } diff --git a/templates/profile/edit.html.twig b/templates/profile/edit.html.twig index d95b157..cac89e5 100644 --- a/templates/profile/edit.html.twig +++ b/templates/profile/edit.html.twig @@ -16,7 +16,11 @@ {{ form_row(form.tel, {'attr': {'class': 'w-full p-3 border rounded-md'}}) }} {{ form_row(form.address, {'attr': {'class': 'w-full p-3 border rounded-md'}}) }} {{ form_row(form.mail, {'attr': {'class': 'w-full p-3 border rounded-md'}}) }} - {{ form_row(form.password, {'attr': {'class': 'w-full p-3 border rounded-md'}}) }} + {{ form_row(form.password, { + 'attr': {'class': 'w-full p-3 border rounded-md'}, + 'label': 'Mot de passe (laissez vide pour ne pas changer)', + }) }} +
{{ form_widget(form) }}