From cdec4b1bbd9efc1bce01263a923322edd70e6dcf Mon Sep 17 00:00:00 2001
From: Camille Simiand <camille.simiand@tetras-libre.fr>
Date: Tue, 8 Mar 2022 18:07:26 +0100
Subject: [PATCH] Route email confirmation

---
 src/Controller/UserController.php     | 11 +++++++++--
 templates/user/edit_profile.html.twig |  1 -
 templates/user/update_email.html.twig |  2 +-
 3 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php
index f10d1eb..4122df2 100755
--- a/src/Controller/UserController.php
+++ b/src/Controller/UserController.php
@@ -70,6 +70,13 @@ class UserController extends AbstractController
 
         if ($form->isSubmitted() && $form->isValid()) {
             if ($current_user->getEmail() !== $form->get('email')->getData()) {
+                $last_pending_email_address = $this->pending_email_address_repository->findOneBy(
+                    ['user' => $current_user->getId()]
+                );
+                if ($last_pending_email_address !== null) {
+                    $this->entity_manager->remove($last_pending_email_address);
+                }
+
                 $pending_email_address = new PendingEmailAddress();
                 $pending_email_address->setEmail($form->get('email')->getData());
                 $pending_email_address->setUser($current_user);
@@ -146,7 +153,7 @@ class UserController extends AbstractController
         ]);
     }
 
-    #[Route('/edit/email/', name:'verify_new_email_address')]
+    #[Route('/edit/email', name:'verify_new_email_address')]
     public function verifyNewEmailAddress(): Response
     {
         $current_user = $this->getUser();
@@ -157,7 +164,7 @@ class UserController extends AbstractController
 
         $pending_email_address = $this->pending_email_address_repository->findOneBy(['user' => $current_user->getId()]);
         if (! $pending_email_address instanceof PendingEmailAddress) {
-            throw new \Exception('Not found');
+            throw new \Exception('Pending email address not found');
         }
 
         $current_user->setEmail($pending_email_address->getEmail());
diff --git a/templates/user/edit_profile.html.twig b/templates/user/edit_profile.html.twig
index 57a8376..d916e3c 100644
--- a/templates/user/edit_profile.html.twig
+++ b/templates/user/edit_profile.html.twig
@@ -23,5 +23,4 @@
         {{ form_row(editUserProfileForm.update, {'row_attr': {'class' : 'form-group d-flex flex-column m-auto mb-4 col-2'}}) }}
         {{ form_end(editUserProfileForm) }}
     </div>
-
 {% endblock %}
\ No newline at end of file
diff --git a/templates/user/update_email.html.twig b/templates/user/update_email.html.twig
index 082e02f..6be99db 100644
--- a/templates/user/update_email.html.twig
+++ b/templates/user/update_email.html.twig
@@ -5,7 +5,7 @@
 <p>
     {{ 'user.edit.email.text'|trans }}:
     <br><br>
-    <a href="{{ path('verify_new_email_address') }}">
+    <a href="{{ url('verify_new_email_address') }}">
         {{ 'user.edit.email.confirm_email'|trans }}
     </a>
     {{ 'general.link_expire'|trans
-- 
GitLab