diff options
author | Jeremy Benoist <jeremy.benoist@gmail.com> | 2016-01-10 12:49:43 +0100 |
---|---|---|
committer | Jeremy Benoist <jeremy.benoist@gmail.com> | 2016-01-21 11:03:27 +0100 |
commit | 7ce895bf5e0531b96b2cf0692b9da3814fb742f8 (patch) | |
tree | 0727a190800024fed1b5de08562de8dd815b194b /src | |
parent | e72a943ad244f8b6349e31eb9a148808045b224b (diff) | |
download | wallabag-7ce895bf5e0531b96b2cf0692b9da3814fb742f8.tar.gz wallabag-7ce895bf5e0531b96b2cf0692b9da3814fb742f8.tar.zst wallabag-7ce895bf5e0531b96b2cf0692b9da3814fb742f8.zip |
Use HTML email for 2FA
Related to #1490
Diffstat (limited to 'src')
6 files changed, 62 insertions, 39 deletions
diff --git a/src/Wallabag/UserBundle/Mailer/AuthCodeMailer.php b/src/Wallabag/UserBundle/Mailer/AuthCodeMailer.php index be370e71..c04720ae 100644 --- a/src/Wallabag/UserBundle/Mailer/AuthCodeMailer.php +++ b/src/Wallabag/UserBundle/Mailer/AuthCodeMailer.php | |||
@@ -20,11 +20,11 @@ class AuthCodeMailer implements AuthCodeMailerInterface | |||
20 | private $mailer; | 20 | private $mailer; |
21 | 21 | ||
22 | /** | 22 | /** |
23 | * Translator for email content. | 23 | * Twig to render the html's email. |
24 | * | 24 | * |
25 | * @var TranslatorInterface | 25 | * @var \Twig_Environment |
26 | */ | 26 | */ |
27 | private $translator; | 27 | private $twig; |
28 | 28 | ||
29 | /** | 29 | /** |
30 | * Sender email address. | 30 | * Sender email address. |
@@ -50,16 +50,16 @@ class AuthCodeMailer implements AuthCodeMailerInterface | |||
50 | /** | 50 | /** |
51 | * Initialize the auth code mailer with the SwiftMailer object. | 51 | * Initialize the auth code mailer with the SwiftMailer object. |
52 | * | 52 | * |
53 | * @param \Swift_Mailer $mailer | 53 | * @param \Swift_Mailer $mailer |
54 | * @param TranslatorInterface $translator | 54 | * @param \Twig_Environment $twig |
55 | * @param string $senderEmail | 55 | * @param string $senderEmail |
56 | * @param string $senderName | 56 | * @param string $senderName |
57 | * @param string $supportUrl | 57 | * @param string $supportUrl |
58 | */ | 58 | */ |
59 | public function __construct(\Swift_Mailer $mailer, TranslatorInterface $translator, $senderEmail, $senderName, $supportUrl) | 59 | public function __construct(\Swift_Mailer $mailer, \Twig_Environment $twig, $senderEmail, $senderName, $supportUrl) |
60 | { | 60 | { |
61 | $this->mailer = $mailer; | 61 | $this->mailer = $mailer; |
62 | $this->translator = $translator; | 62 | $this->twig = $twig; |
63 | $this->senderEmail = $senderEmail; | 63 | $this->senderEmail = $senderEmail; |
64 | $this->senderName = $senderName; | 64 | $this->senderName = $senderName; |
65 | $this->supportUrl = $supportUrl; | 65 | $this->supportUrl = $supportUrl; |
@@ -72,20 +72,27 @@ class AuthCodeMailer implements AuthCodeMailerInterface | |||
72 | */ | 72 | */ |
73 | public function sendAuthCode(TwoFactorInterface $user) | 73 | public function sendAuthCode(TwoFactorInterface $user) |
74 | { | 74 | { |
75 | $template = $this->twig->loadTemplate('@WallabagUserBundle/Resources/views/TwoFactor/email_auth_code.html.twig'); | ||
76 | |||
77 | $subject = $template->renderBlock('subject', array()); | ||
78 | $bodyHtml = $template->renderBlock('body_html', [ | ||
79 | 'user' => $user->getName(), | ||
80 | 'code' => $user->getEmailAuthCode(), | ||
81 | 'support' => $this->supportUrl, | ||
82 | ]); | ||
83 | $bodyText = $template->renderBlock('body_text', [ | ||
84 | 'user' => $user->getName(), | ||
85 | 'code' => $user->getEmailAuthCode(), | ||
86 | 'support' => $this->supportUrl, | ||
87 | ]); | ||
88 | |||
75 | $message = new \Swift_Message(); | 89 | $message = new \Swift_Message(); |
76 | $message | 90 | $message |
77 | ->setTo($user->getEmail()) | 91 | ->setTo($user->getEmail()) |
78 | ->setFrom($this->senderEmail, $this->senderName) | 92 | ->setFrom($this->senderEmail, $this->senderName) |
79 | ->setSubject($this->translator->trans('auth_code.mailer.subject', array(), 'wallabag_user')) | 93 | ->setSubject($subject) |
80 | ->setBody($this->translator->trans( | 94 | ->setBody($bodyText, 'text/plain') |
81 | 'auth_code.mailer.body', | 95 | ->addPart($bodyHtml, 'text/html') |
82 | [ | ||
83 | '%user%' => $user->getName(), | ||
84 | '%code%' => $user->getEmailAuthCode(), | ||
85 | '%support%' => $this->supportUrl, | ||
86 | ], | ||
87 | 'wallabag_user' | ||
88 | )) | ||
89 | ; | 96 | ; |
90 | 97 | ||
91 | $this->mailer->send($message); | 98 | $this->mailer->send($message); |
diff --git a/src/Wallabag/UserBundle/Resources/config/services.yml b/src/Wallabag/UserBundle/Resources/config/services.yml index 9109b6a3..aa2fd8b9 100644 --- a/src/Wallabag/UserBundle/Resources/config/services.yml +++ b/src/Wallabag/UserBundle/Resources/config/services.yml | |||
@@ -3,7 +3,7 @@ services: | |||
3 | class: Wallabag\UserBundle\Mailer\AuthCodeMailer | 3 | class: Wallabag\UserBundle\Mailer\AuthCodeMailer |
4 | arguments: | 4 | arguments: |
5 | - "@mailer" | 5 | - "@mailer" |
6 | - "@translator" | 6 | - "@twig" |
7 | - "%scheb_two_factor.email.sender_email%" | 7 | - "%scheb_two_factor.email.sender_email%" |
8 | - "%scheb_two_factor.email.sender_name%" | 8 | - "%scheb_two_factor.email.sender_name%" |
9 | - "%wallabag_support_url%" | 9 | - "%wallabag_support_url%" |
diff --git a/src/Wallabag/UserBundle/Resources/translations/wallabag_user.en.yml b/src/Wallabag/UserBundle/Resources/translations/wallabag_user.en.yml index f806d1d6..7298bb28 100644 --- a/src/Wallabag/UserBundle/Resources/translations/wallabag_user.en.yml +++ b/src/Wallabag/UserBundle/Resources/translations/wallabag_user.en.yml | |||
@@ -1,10 +1,9 @@ | |||
1 | # Two factor mail | 1 | # Two factor mail |
2 | auth_code.mailer.subject: 'Wallabag authentication Code' | 2 | auth_code.mailer.subject: 'Wallabag authentication Code' |
3 | auth_code.mailer.body: | | 3 | auth_code.mailer.body.hello: "Hi %user%," |
4 | Hi %user%, | 4 | auth_code.mailer.body.content: | |
5 | |||
6 | Since you enable two factor authentication on your wallabag account and you just logged in from a new device (computer, phone, etc.), we send you a code to validate your connection. | 5 | Since you enable two factor authentication on your wallabag account and you just logged in from a new device (computer, phone, etc.), we send you a code to validate your connection. |
7 | Here is the code: %code% | 6 | Here is the code: %code% |
8 | 7 | auth_code.mailer.body.signature: | | |
9 | Please don't hesitate to contact us if you have any problems: %support% | 8 | Please don't hesitate to contact us if you have any problems: %support% |
10 | The wallabag team | 9 | The wallabag team |
diff --git a/src/Wallabag/UserBundle/Resources/translations/wallabag_user.fr.yml b/src/Wallabag/UserBundle/Resources/translations/wallabag_user.fr.yml index 386b2d9e..0c492d04 100644 --- a/src/Wallabag/UserBundle/Resources/translations/wallabag_user.fr.yml +++ b/src/Wallabag/UserBundle/Resources/translations/wallabag_user.fr.yml | |||
@@ -1,10 +1,9 @@ | |||
1 | # Two factor mail | 1 | # Two factor mail |
2 | auth_code.mailer.subject: "Code d'authentification wallabag" | 2 | auth_code.mailer.subject: "Code d'authentification wallabag" |
3 | auth_code.mailer.body: | | 3 | auth_code.mailer.body.hello: "Bonjour %user%," |
4 | Bonjour %user%, | 4 | auth_code.mailer.body.content: | |
5 | |||
6 | Comme vous avez activé la double authentification sur votre compte wallabag et que vous venez de vous connecter depuis un nouvel appareil (ordinateur, téléphone, etc.), nous vous envoyons un code pour valider votre connexion. | 5 | Comme vous avez activé la double authentification sur votre compte wallabag et que vous venez de vous connecter depuis un nouvel appareil (ordinateur, téléphone, etc.), nous vous envoyons un code pour valider votre connexion. |
7 | Voici le code à renseigner: %code% | 6 | Voici le code à renseigner: %code% |
8 | 7 | auth_code.mailer.body.signature: | | |
9 | Si vous avez un problème de connexion, n'hésitez pas à contacter le support: %support% | 8 | Si vous avez un problème de connexion, n'hésitez pas à contacter le support: %support% |
10 | L'équipe wallabag | 9 | L'équipe wallabag |
diff --git a/src/Wallabag/UserBundle/Resources/views/TwoFactor/email_auth_code.html.twig b/src/Wallabag/UserBundle/Resources/views/TwoFactor/email_auth_code.html.twig new file mode 100644 index 00000000..b726d142 --- /dev/null +++ b/src/Wallabag/UserBundle/Resources/views/TwoFactor/email_auth_code.html.twig | |||
@@ -0,0 +1,18 @@ | |||
1 | {% block subject %} | ||
2 | {{ "auth_code.mailer.subject"|trans({}, 'wallabag_user') }} | ||
3 | {% endblock %} | ||
4 | |||
5 | {% block body_html %} | ||
6 | <p><b>{{ "auth_code.mailer.body.hello"|trans({'%user%': user}, 'wallabag_user') }}</b></p> | ||
7 | |||
8 | <p>{{ "auth_code.mailer.body.content"|trans({'%code%': code}, 'wallabag_user') }}</p> | ||
9 | |||
10 | <p>{{ "auth_code.mailer.body.signature"|trans({'%support%': support}, 'wallabag_user') }}</p> | ||
11 | {% endblock %} | ||
12 | |||
13 | {% block body_text %} | ||
14 | {{ "auth_code.mailer.body.hello"|trans({'%user%': user}, 'wallabag_user') }} | ||
15 | |||
16 | {{ "auth_code.mailer.body.content"|trans({'%code%': code}, 'wallabag_user') }} | ||
17 | {{ "auth_code.mailer.body.signature"|trans({'%support%': support}, 'wallabag_user') }} | ||
18 | {% endblock %} | ||
diff --git a/src/Wallabag/UserBundle/Tests/Mailer/AuthCodeMailerTest.php b/src/Wallabag/UserBundle/Tests/Mailer/AuthCodeMailerTest.php index 61e960f9..857cdcbe 100644 --- a/src/Wallabag/UserBundle/Tests/Mailer/AuthCodeMailerTest.php +++ b/src/Wallabag/UserBundle/Tests/Mailer/AuthCodeMailerTest.php | |||
@@ -27,7 +27,7 @@ class AuthCodeMailerTest extends \PHPUnit_Framework_TestCase | |||
27 | { | 27 | { |
28 | protected $mailer; | 28 | protected $mailer; |
29 | protected $spool; | 29 | protected $spool; |
30 | protected $translator; | 30 | protected $twig; |
31 | 31 | ||
32 | protected function setUp() | 32 | protected function setUp() |
33 | { | 33 | { |
@@ -38,12 +38,11 @@ class AuthCodeMailerTest extends \PHPUnit_Framework_TestCase | |||
38 | ); | 38 | ); |
39 | $this->mailer = new \Swift_Mailer($transport); | 39 | $this->mailer = new \Swift_Mailer($transport); |
40 | 40 | ||
41 | $this->translator = new Translator('en'); | 41 | $this->twig = new \Twig_Environment(new \Twig_Loader_Array(array('@WallabagUserBundle/Resources/views/TwoFactor/email_auth_code.html.twig' => ' |
42 | $this->translator->addLoader('array', new ArrayLoader()); | 42 | {% block subject %}subject{% endblock %} |
43 | $this->translator->addResource('array', array( | 43 | {% block body_html %}html body{% endblock %} |
44 | 'auth_code.mailer.subject' => 'auth_code subject', | 44 | {% block body_text %}text body{% endblock %} |
45 | 'auth_code.mailer.body' => 'Hi %user%, here is the code: %code% and the support: %support%', | 45 | '))); |
46 | ), 'en', 'wallabag_user'); | ||
47 | } | 46 | } |
48 | 47 | ||
49 | public function testSendEmail() | 48 | public function testSendEmail() |
@@ -56,7 +55,7 @@ class AuthCodeMailerTest extends \PHPUnit_Framework_TestCase | |||
56 | 55 | ||
57 | $authCodeMailer = new AuthCodeMailer( | 56 | $authCodeMailer = new AuthCodeMailer( |
58 | $this->mailer, | 57 | $this->mailer, |
59 | $this->translator, | 58 | $this->twig, |
60 | 'nobody@test.io', | 59 | 'nobody@test.io', |
61 | 'wallabag test', | 60 | 'wallabag test', |
62 | 'http://0.0.0.0' | 61 | 'http://0.0.0.0' |
@@ -69,7 +68,8 @@ class AuthCodeMailerTest extends \PHPUnit_Framework_TestCase | |||
69 | $msg = $this->spool->getMessages()[0]; | 68 | $msg = $this->spool->getMessages()[0]; |
70 | $this->assertArrayHasKey('test@wallabag.io', $msg->getTo()); | 69 | $this->assertArrayHasKey('test@wallabag.io', $msg->getTo()); |
71 | $this->assertEquals(array('nobody@test.io' => 'wallabag test'), $msg->getFrom()); | 70 | $this->assertEquals(array('nobody@test.io' => 'wallabag test'), $msg->getFrom()); |
72 | $this->assertEquals('auth_code subject', $msg->getSubject()); | 71 | $this->assertEquals('subject', $msg->getSubject()); |
73 | $this->assertContains('Hi Bob, here is the code: 666666 and the support: http://0.0.0.0', $msg->toString()); | 72 | $this->assertContains('text body', $msg->toString()); |
73 | $this->assertContains('html body', $msg->toString()); | ||
74 | } | 74 | } |
75 | } | 75 | } |