diff options
author | Jeremy Benoist <jeremy.benoist@gmail.com> | 2015-12-29 09:59:46 +0100 |
---|---|---|
committer | Jeremy Benoist <jeremy.benoist@gmail.com> | 2015-12-29 11:17:06 +0100 |
commit | 23ff8d36199c0cddb5bae4a5010cb71f861eeef8 (patch) | |
tree | 0da3a5941c85f80408e49db63ecd3b3ba5347a40 /src/Wallabag/UserBundle | |
parent | c997cfcc9c161241a6398b0942a1a869688d807a (diff) | |
download | wallabag-23ff8d36199c0cddb5bae4a5010cb71f861eeef8.tar.gz wallabag-23ff8d36199c0cddb5bae4a5010cb71f861eeef8.tar.zst wallabag-23ff8d36199c0cddb5bae4a5010cb71f861eeef8.zip |
Add custom email for 2FA
Related #1490
Diffstat (limited to 'src/Wallabag/UserBundle')
7 files changed, 242 insertions, 0 deletions
diff --git a/src/Wallabag/UserBundle/DependencyInjection/Configuration.php b/src/Wallabag/UserBundle/DependencyInjection/Configuration.php new file mode 100644 index 00000000..4223f8db --- /dev/null +++ b/src/Wallabag/UserBundle/DependencyInjection/Configuration.php | |||
@@ -0,0 +1,17 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\UserBundle\DependencyInjection; | ||
4 | |||
5 | use Symfony\Component\Config\Definition\Builder\TreeBuilder; | ||
6 | use Symfony\Component\Config\Definition\ConfigurationInterface; | ||
7 | |||
8 | class Configuration implements ConfigurationInterface | ||
9 | { | ||
10 | public function getConfigTreeBuilder() | ||
11 | { | ||
12 | $treeBuilder = new TreeBuilder(); | ||
13 | $rootNode = $treeBuilder->root('wallabag_user'); | ||
14 | |||
15 | return $treeBuilder; | ||
16 | } | ||
17 | } | ||
diff --git a/src/Wallabag/UserBundle/DependencyInjection/WallabagUserExtension.php b/src/Wallabag/UserBundle/DependencyInjection/WallabagUserExtension.php new file mode 100644 index 00000000..c12a8937 --- /dev/null +++ b/src/Wallabag/UserBundle/DependencyInjection/WallabagUserExtension.php | |||
@@ -0,0 +1,25 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\UserBundle\DependencyInjection; | ||
4 | |||
5 | use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
6 | use Symfony\Component\Config\FileLocator; | ||
7 | use Symfony\Component\HttpKernel\DependencyInjection\Extension; | ||
8 | use Symfony\Component\DependencyInjection\Loader; | ||
9 | |||
10 | class WallabagUserExtension extends Extension | ||
11 | { | ||
12 | public function load(array $configs, ContainerBuilder $container) | ||
13 | { | ||
14 | $configuration = new Configuration(); | ||
15 | $config = $this->processConfiguration($configuration, $configs); | ||
16 | |||
17 | $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); | ||
18 | $loader->load('services.yml'); | ||
19 | } | ||
20 | |||
21 | public function getAlias() | ||
22 | { | ||
23 | return 'wallabag_user'; | ||
24 | } | ||
25 | } | ||
diff --git a/src/Wallabag/UserBundle/Mailer/AuthCodeMailer.php b/src/Wallabag/UserBundle/Mailer/AuthCodeMailer.php new file mode 100644 index 00000000..f1960070 --- /dev/null +++ b/src/Wallabag/UserBundle/Mailer/AuthCodeMailer.php | |||
@@ -0,0 +1,93 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\UserBundle\Mailer; | ||
4 | |||
5 | use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface; | ||
6 | use Scheb\TwoFactorBundle\Mailer\AuthCodeMailerInterface; | ||
7 | use Symfony\Component\Translation\DataCollectorTranslator; | ||
8 | |||
9 | /** | ||
10 | * Custom mailer for TwoFactorBundle email. | ||
11 | * It adds a custom template to the email so user won't get a lonely authentication code but a complete email. | ||
12 | */ | ||
13 | class AuthCodeMailer implements AuthCodeMailerInterface | ||
14 | { | ||
15 | /** | ||
16 | * SwiftMailer. | ||
17 | * | ||
18 | * @var \Swift_Mailer | ||
19 | */ | ||
20 | private $mailer; | ||
21 | |||
22 | /** | ||
23 | * Translator for email content. | ||
24 | * | ||
25 | * @var DataCollectorTranslator | ||
26 | */ | ||
27 | private $translator; | ||
28 | |||
29 | /** | ||
30 | * Sender email address. | ||
31 | * | ||
32 | * @var string | ||
33 | */ | ||
34 | private $senderEmail; | ||
35 | |||
36 | /** | ||
37 | * Sender name. | ||
38 | * | ||
39 | * @var string | ||
40 | */ | ||
41 | private $senderName; | ||
42 | |||
43 | /** | ||
44 | * Support URL to report any bugs. | ||
45 | * | ||
46 | * @var string | ||
47 | */ | ||
48 | private $supportUrl; | ||
49 | |||
50 | /** | ||
51 | * Initialize the auth code mailer with the SwiftMailer object. | ||
52 | * | ||
53 | * @param \Swift_Mailer $mailer | ||
54 | * @param DataCollectorTranslator $translator | ||
55 | * @param string $senderEmail | ||
56 | * @param string $senderName | ||
57 | * @param string $supportUrl | ||
58 | */ | ||
59 | public function __construct(\Swift_Mailer $mailer, DataCollectorTranslator $translator, $senderEmail, $senderName, $supportUrl) | ||
60 | { | ||
61 | $this->mailer = $mailer; | ||
62 | $this->translator = $translator; | ||
63 | $this->senderEmail = $senderEmail; | ||
64 | $this->senderName = $senderName; | ||
65 | $this->supportUrl = $supportUrl; | ||
66 | } | ||
67 | |||
68 | /** | ||
69 | * Send the auth code to the user via email. | ||
70 | * | ||
71 | * @param TwoFactorInterface $user | ||
72 | */ | ||
73 | public function sendAuthCode(TwoFactorInterface $user) | ||
74 | { | ||
75 | $message = new \Swift_Message(); | ||
76 | $message | ||
77 | ->setTo($user->getEmail()) | ||
78 | ->setFrom($this->senderEmail, $this->senderName) | ||
79 | ->setSubject($this->translator->trans('auth_code.mailer.subject', array(), 'wallabag_user')) | ||
80 | ->setBody($this->translator->trans( | ||
81 | 'auth_code.mailer.body', | ||
82 | [ | ||
83 | '%user%' => $user->getName(), | ||
84 | '%code%' => $user->getEmailAuthCode(), | ||
85 | '%support%' => $this->supportUrl, | ||
86 | ], | ||
87 | 'wallabag_user' | ||
88 | )) | ||
89 | ; | ||
90 | |||
91 | $this->mailer->send($message); | ||
92 | } | ||
93 | } | ||
diff --git a/src/Wallabag/UserBundle/Resources/config/services.yml b/src/Wallabag/UserBundle/Resources/config/services.yml index e69de29b..9109b6a3 100644 --- a/src/Wallabag/UserBundle/Resources/config/services.yml +++ b/src/Wallabag/UserBundle/Resources/config/services.yml | |||
@@ -0,0 +1,9 @@ | |||
1 | services: | ||
2 | wallabag_user.auth_code_mailer: | ||
3 | class: Wallabag\UserBundle\Mailer\AuthCodeMailer | ||
4 | arguments: | ||
5 | - "@mailer" | ||
6 | - "@translator" | ||
7 | - "%scheb_two_factor.email.sender_email%" | ||
8 | - "%scheb_two_factor.email.sender_name%" | ||
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 new file mode 100644 index 00000000..f806d1d6 --- /dev/null +++ b/src/Wallabag/UserBundle/Resources/translations/wallabag_user.en.yml | |||
@@ -0,0 +1,10 @@ | |||
1 | # Two factor mail | ||
2 | auth_code.mailer.subject: 'Wallabag authentication Code' | ||
3 | auth_code.mailer.body: | | ||
4 | Hi %user%, | ||
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. | ||
7 | Here is the code: %code% | ||
8 | |||
9 | Please don't hesitate to contact us if you have any problems: %support% | ||
10 | 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 new file mode 100644 index 00000000..386b2d9e --- /dev/null +++ b/src/Wallabag/UserBundle/Resources/translations/wallabag_user.fr.yml | |||
@@ -0,0 +1,10 @@ | |||
1 | # Two factor mail | ||
2 | auth_code.mailer.subject: "Code d'authentification wallabag" | ||
3 | auth_code.mailer.body: | | ||
4 | Bonjour %user%, | ||
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. | ||
7 | Voici le code à renseigner: %code% | ||
8 | |||
9 | Si vous avez un problème de connexion, n'hésitez pas à contacter le support: %support% | ||
10 | L'équipe wallabag | ||
diff --git a/src/Wallabag/UserBundle/Tests/Mailer/AuthCodeMailerTest.php b/src/Wallabag/UserBundle/Tests/Mailer/AuthCodeMailerTest.php new file mode 100644 index 00000000..9122576a --- /dev/null +++ b/src/Wallabag/UserBundle/Tests/Mailer/AuthCodeMailerTest.php | |||
@@ -0,0 +1,78 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\UserBundle\Tests\Mailer; | ||
4 | |||
5 | use Wallabag\UserBundle\Entity\User; | ||
6 | use Wallabag\UserBundle\Mailer\AuthCodeMailer; | ||
7 | use Symfony\Component\Translation\Translator; | ||
8 | use Symfony\Component\Translation\Loader\ArrayLoader; | ||
9 | use Symfony\Component\Translation\DataCollectorTranslator; | ||
10 | |||
11 | /** | ||
12 | * @see https://www.pmg.com/blog/integration-testing-swift-mailer/ | ||
13 | */ | ||
14 | final class CountableMemorySpool extends \Swift_MemorySpool implements \Countable | ||
15 | { | ||
16 | public function count() | ||
17 | { | ||
18 | return count($this->messages); | ||
19 | } | ||
20 | |||
21 | public function getMessages() | ||
22 | { | ||
23 | return $this->messages; | ||
24 | } | ||
25 | } | ||
26 | |||
27 | class AuthCodeMailerTest extends \PHPUnit_Framework_TestCase | ||
28 | { | ||
29 | protected $mailer; | ||
30 | protected $spool; | ||
31 | protected $dataCollector; | ||
32 | |||
33 | protected function setUp() | ||
34 | { | ||
35 | $this->spool = new CountableMemorySpool(); | ||
36 | $transport = new \Swift_Transport_SpoolTransport( | ||
37 | new \Swift_Events_SimpleEventDispatcher(), | ||
38 | $this->spool | ||
39 | ); | ||
40 | $this->mailer = new \Swift_Mailer($transport); | ||
41 | |||
42 | $translator = new Translator('en'); | ||
43 | $translator->addLoader('array', new ArrayLoader()); | ||
44 | $translator->addResource('array', array( | ||
45 | 'auth_code.mailer.subject' => 'auth_code subject', | ||
46 | 'auth_code.mailer.body' => 'Hi %user%, here is the code: %code% and the support: %support%', | ||
47 | ), 'en', 'wallabag_user'); | ||
48 | |||
49 | $this->dataCollector = new DataCollectorTranslator($translator); | ||
50 | } | ||
51 | |||
52 | public function testSendEmail() | ||
53 | { | ||
54 | $user = new User(); | ||
55 | $user->setTwoFactorAuthentication(true); | ||
56 | $user->setEmailAuthCode(666666); | ||
57 | $user->setEmail('test@wallabag.io'); | ||
58 | $user->setName('Bob'); | ||
59 | |||
60 | $authCodeMailer = new AuthCodeMailer( | ||
61 | $this->mailer, | ||
62 | $this->dataCollector, | ||
63 | 'nobody@test.io', | ||
64 | 'wallabag test', | ||
65 | 'http://0.0.0.0' | ||
66 | ); | ||
67 | |||
68 | $authCodeMailer->sendAuthCode($user); | ||
69 | |||
70 | $this->assertCount(1, $this->spool); | ||
71 | |||
72 | $msg = $this->spool->getMessages()[0]; | ||
73 | $this->assertArrayHasKey('test@wallabag.io', $msg->getTo()); | ||
74 | $this->assertEquals(array('nobody@test.io' => 'wallabag test'), $msg->getFrom()); | ||
75 | $this->assertEquals('auth_code subject', $msg->getSubject()); | ||
76 | $this->assertContains('Hi Bob, here is the code: 666666 and the support: http://0.0.0.0', $msg->toString()); | ||
77 | } | ||
78 | } | ||