From ecbc54751516ef5a0cf6b401389a573525ea8842 Mon Sep 17 00:00:00 2001
From: Camille Simiand <camille.simiand@tetras-libre.fr>
Date: Fri, 28 Jan 2022 12:40:21 +0100
Subject: [PATCH] Add send email to notify new editors

---
 src/Controller/CapsuleEditorController.php    | 40 +++++++++++++++++--
 src/Controller/RegistrationController.php     |  3 +-
 src/Entity/User.php                           |  5 +++
 src/Security/EmailVerifier.php                |  9 +++--
 .../capsule/editors/email_editor.html.twig    | 21 ++++++++++
 .../editors/email_pending_editor.html.twig    | 25 ++++++++++++
 .../{ => editors}/list_editors.html.twig      |  0
 translations/messages.en.yaml                 | 15 ++++++-
 translations/messages.fr.yaml                 | 15 ++++++-
 9 files changed, 121 insertions(+), 12 deletions(-)
 create mode 100644 templates/capsule/editors/email_editor.html.twig
 create mode 100644 templates/capsule/editors/email_pending_editor.html.twig
 rename templates/capsule/{ => editors}/list_editors.html.twig (100%)

diff --git a/src/Controller/CapsuleEditorController.php b/src/Controller/CapsuleEditorController.php
index d228f0a..70508ba 100644
--- a/src/Controller/CapsuleEditorController.php
+++ b/src/Controller/CapsuleEditorController.php
@@ -10,14 +10,25 @@ use App\Form\CapsuleEditorsFormType;
 use App\Repository\CapsuleEditorRepository;
 use App\Repository\CapsulePendingEditorRepository;
 use App\Repository\UserRepository;
+use Symfony\Bridge\Twig\Mime\TemplatedEmail;
 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\Mailer\MailerInterface;
 use Symfony\Component\Routing\Annotation\Route;
 use Symfony\Contracts\Translation\TranslatorInterface;
 
 class CapsuleEditorController extends AbstractController
 {
+    private TranslatorInterface $translator;
+    private MailerInterface $mailer;
+
+    public function __construct(MailerInterface $mailer, TranslatorInterface $translator)
+    {
+        $this->mailer = $mailer;
+        $this->translator = $translator;
+    }
+
     /**
      * @Route("/capsule/{capsule_id}/editors", name="edit_capsule_editors")
      */
@@ -29,14 +40,15 @@ class CapsuleEditorController extends AbstractController
         CapsuleEditorRepository $capsule_editor_repository,
         UserRepository $user_repository
     ): Response {
-        $form = $this->createForm(CapsuleEditorsFormType::class);
-        $form->handleRequest($request);
         $current_user = $this->getUser();
 
         if (! $current_user instanceof User) {
             return $this->redirectToRoute('app_logout');
         }
 
+        $form = $this->createForm(CapsuleEditorsFormType::class);
+        $form->handleRequest($request);
+
         $entity_manager = $this->getDoctrine()->getManager();
         $capsule_repository = $entity_manager->getRepository(Capsule::class);
         $capsule = $capsule_repository->find($capsule_id);
@@ -97,6 +109,17 @@ class CapsuleEditorController extends AbstractController
                 $entity_manager->persist($pending_editor);
                 $entity_manager->flush();
 
+                $email = (new TemplatedEmail())
+                    ->to($editor_email)
+                    ->subject($this->translator->trans('editors.add.pending_editor.email.title'))
+                    ->htmlTemplate('capsule/editors/email_pending_editor.html.twig')
+                    ->context([
+                        'user' => $current_user,
+                        'capsule' => $capsule
+                    ]);
+
+                $this->mailer->send($email);
+
                 $this->addFlash(
                     'success',
                     $translator->trans(
@@ -133,6 +156,17 @@ class CapsuleEditorController extends AbstractController
             $entity_manager->persist($capsule_editor);
             $entity_manager->flush();
 
+            $email = (new TemplatedEmail())
+                ->to($editor_email)
+                ->subject($this->translator->trans('editors.add.user.email.title'))
+                ->htmlTemplate('capsule/editors/email_editor.html.twig')
+                ->context([
+                    'user' => $current_user,
+                    'capsule' => $capsule
+                ]);
+
+                $this->mailer->send($email);
+
             $this->addFlash(
                 'success',
                 $translator->trans(
@@ -149,7 +183,7 @@ class CapsuleEditorController extends AbstractController
             ]);
         }
 
-        return $this->render('capsule/list_editors.html.twig', [
+        return $this->render('capsule/editors/list_editors.html.twig', [
             'userPermissionsCapsuleForm' => $form->createView(),
             'capsule_name' => $capsule->getName(),
             'editors' => $current_capsule_editors_users,
diff --git a/src/Controller/RegistrationController.php b/src/Controller/RegistrationController.php
index e4f1245..d5df813 100644
--- a/src/Controller/RegistrationController.php
+++ b/src/Controller/RegistrationController.php
@@ -64,7 +64,8 @@ class RegistrationController extends AbstractController
             // generate a signed url and email it to the user
             $this->emailVerifier->sendEmailConfirmation(
                 'app_verify_email',
-                $user,
+                $user->getId(),
+                $user->getEmail(),
                 (new TemplatedEmail())
                     ->to($user->getEmail())
                     ->subject('Please Confirm your Email')
diff --git a/src/Entity/User.php b/src/Entity/User.php
index 143f7d9..5e2fa78 100644
--- a/src/Entity/User.php
+++ b/src/Entity/User.php
@@ -225,6 +225,11 @@ class User implements UserInterface, LegacyPasswordAuthenticatedUserInterface
         $this->salt = $salt;
     }
 
+    public function getFullName(): string
+    {
+        return $this->firstName . " " . $this->lastName;
+    }
+
     /**
      * @return Collection<Capsule>
      */
diff --git a/src/Security/EmailVerifier.php b/src/Security/EmailVerifier.php
index 84ee1df..ea4389f 100644
--- a/src/Security/EmailVerifier.php
+++ b/src/Security/EmailVerifier.php
@@ -28,14 +28,15 @@ class EmailVerifier
 
     public function sendEmailConfirmation(
         string $verifyEmailRouteName,
-        User $user,
+        int $user_id,
+        string $email_address,
         TemplatedEmail $email
     ): void {
         $signatureComponents = $this->verifyEmailHelper->generateSignature(
             $verifyEmailRouteName,
-            (string) $user->getId(),
-            $user->getEmail(),
-            ['id' => $user->getId()]
+            (string) $user_id,
+            $email_address,
+            ['id' => $user_id]
         );
 
         $context = $email->getContext();
diff --git a/templates/capsule/editors/email_editor.html.twig b/templates/capsule/editors/email_editor.html.twig
new file mode 100644
index 0000000..c28c681
--- /dev/null
+++ b/templates/capsule/editors/email_editor.html.twig
@@ -0,0 +1,21 @@
+<!DOCTYPE html>
+<html>
+    <head>
+        <meta charset="UTF-8">
+        <title>
+            {{ 'editors.add.user.email.title'|trans }}
+        </title>
+    </head>
+
+    <body>
+        <div class="container d-flex flex-row justify-content-center">
+            <div class="col-6">
+                <p class="alert">
+                    {{ 'editors.add.user.email.text'|trans({'%user_name%': user.getFullName(), '%capsule_name%': capsule.getName()}) }}
+                </p>
+
+                <p>{{ 'general.greeting'|trans }}</p>
+            </div>
+        </div>
+    </body>
+</html>
\ No newline at end of file
diff --git a/templates/capsule/editors/email_pending_editor.html.twig b/templates/capsule/editors/email_pending_editor.html.twig
new file mode 100644
index 0000000..8fcaa41
--- /dev/null
+++ b/templates/capsule/editors/email_pending_editor.html.twig
@@ -0,0 +1,25 @@
+<!DOCTYPE html>
+<html>
+<head>
+    <meta charset="UTF-8">
+    <title>
+        {{ 'editors.add.pending_editor.email.title'|trans }}
+    </title>
+</head>
+
+<body>
+<div class="container d-flex flex-row justify-content-center">
+    <div class="col-6">
+        <p class="alert">
+            {{ 'editors.add.pending_editor.email.text'|trans({'%user_name%': user.getFullName(), '%capsule_name%': capsule.getName()}) }} :
+
+            <a href="{{ 'editors.add.pending_editor.email.link'|trans }}">
+                {{ 'editors.add.pending_editor.email.link_name'|trans }}
+            </a>
+        </p>
+
+        <p>{{ 'general.greeting'|trans }}</p>
+    </div>
+</div>
+</body>
+</html>
\ No newline at end of file
diff --git a/templates/capsule/list_editors.html.twig b/templates/capsule/editors/list_editors.html.twig
similarity index 100%
rename from templates/capsule/list_editors.html.twig
rename to templates/capsule/editors/list_editors.html.twig
diff --git a/translations/messages.en.yaml b/translations/messages.en.yaml
index bf29b4e..bcd1ccd 100644
--- a/translations/messages.en.yaml
+++ b/translations/messages.en.yaml
@@ -120,6 +120,17 @@ editors:
         success: The user user_email has been added to pending editor list.
           He will receive an email to invite him register on MemoRekall and to inform him he has been added as an editor of this capsule.
         already_added: The user user_email has already been added to pending editor list
+        email:
+          title: Invitation to edit a MemoRekall capsule
+          text: You have been add by %user_name% as editor of the capsule "%capsule_name%".
+            In order to access and edit it, you first need to register on MemoRekall. Please follow this link to
+          link: https://project.memorekall.com/register/
+          link_name: register
       user:
-        success: The user user_email is now an editor of the capsule capsule_name
-        already_added: The user user_email is already an editor of this capsule
\ No newline at end of file
+        success: The user user_email is now an editor of the capsule capsule_name.
+          He will receive an email to inform him he has been added as an editor of this capsule.
+        already_added: The user user_email is already an editor of this capsule
+        email:
+          title: New capsule on your list
+          text: You have been add by %user_name% as editor of the capsule "%capsule_name%".
+            You can now access and edit it. You will find the capsule in your capsule list.
\ No newline at end of file
diff --git a/translations/messages.fr.yaml b/translations/messages.fr.yaml
index 9d2ebcf..15bad03 100644
--- a/translations/messages.fr.yaml
+++ b/translations/messages.fr.yaml
@@ -118,6 +118,17 @@ editors:
         success: L'utilisateur user_email a bien été ajouté à la liste des editeurs en attente
           Il recevera un e-mail l'invitant à créer un compte MemoRekall et l'informant qu'il a été ajouté en tant qu'éditeur de la capsule.
         already_added: L'utilisateur user_email a déjà été ajouté à la liste des éditeurs en attente
+        email:
+          title: Invitation pour éditer une capsule sur MemoRekall
+          text: Vous avez été ajouté par %user_name% en tant qu'éditeur de la capsule %capsule_name%.
+            Avant de pouvoir y accéder et la modifier, vous devez d'abord créer un compte sur MemoRekall. Veuillez suivre ce lien pour
+          link: https://project.memorekall.com/register/
+          link_name: créer votre compte
       user:
-        success: L'utilisateur user_email est maintenant éditeur de la capsule capsule_name
-        already_added: L'utilisateur user_email est déjà editeur de la capsule
\ No newline at end of file
+        success: L'utilisateur user_email est maintenant éditeur de la capsule capsule_name.
+          Il recevera un e-mail l'informant qu'il a été ajouté en tant qu'éditeur de la capsule.
+        already_added: L'utilisateur user_email est déjà editeur de la capsule
+        email:
+          title: Nouvelle capsule dans votre liste
+          text: Vous avez été ajouté par %user_name% en tant qu'éditeur de la capsule "%capsule_name%".
+            Vous pouvez maintenant y accéder et l'éditer. Vous la retrouverez dans la liste de vos capsules.
\ No newline at end of file
-- 
GitLab