From 23ff8d36199c0cddb5bae4a5010cb71f861eeef8 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Tue, 29 Dec 2015 09:59:46 +0100 Subject: [PATCH] Add custom email for 2FA Related #1490 --- app/config/config.yml | 1 + app/config/parameters.yml.dist | 3 +- app/config/tests/parameters.yml.dist.mysql | 1 + app/config/tests/parameters.yml.dist.pgsql | 1 + app/config/tests/parameters.yml.dist.sqlite | 1 + .../DataFixtures/ORM/LoadConfigData.php | 4 +- .../views/themes/baggy/Config/index.html.twig | 8 ++ .../themes/material/Config/index.html.twig | 6 ++ .../Tests/Controller/ConfigControllerTest.php | 4 +- .../DependencyInjection/Configuration.php | 17 ++++ .../WallabagUserExtension.php | 25 +++++ .../UserBundle/Mailer/AuthCodeMailer.php | 93 +++++++++++++++++++ .../UserBundle/Resources/config/services.yml | 9 ++ .../translations/wallabag_user.en.yml | 10 ++ .../translations/wallabag_user.fr.yml | 10 ++ .../Tests/Mailer/AuthCodeMailerTest.php | 78 ++++++++++++++++ 16 files changed, 266 insertions(+), 5 deletions(-) create mode 100644 src/Wallabag/UserBundle/DependencyInjection/Configuration.php create mode 100644 src/Wallabag/UserBundle/DependencyInjection/WallabagUserExtension.php create mode 100644 src/Wallabag/UserBundle/Mailer/AuthCodeMailer.php create mode 100644 src/Wallabag/UserBundle/Resources/translations/wallabag_user.en.yml create mode 100644 src/Wallabag/UserBundle/Resources/translations/wallabag_user.fr.yml create mode 100644 src/Wallabag/UserBundle/Tests/Mailer/AuthCodeMailerTest.php diff --git a/app/config/config.yml b/app/config/config.yml index f2538c90..8403a458 100644 --- a/app/config/config.yml +++ b/app/config/config.yml @@ -198,6 +198,7 @@ scheb_two_factor: sender_email: %twofactor_sender% digits: 6 template: WallabagUserBundle:Authentication:form.html.twig + mailer: wallabag_user.auth_code_mailer kphoen_rulerz: executors: diff --git a/app/config/parameters.yml.dist b/app/config/parameters.yml.dist index b475d637..149179c2 100644 --- a/app/config/parameters.yml.dist +++ b/app/config/parameters.yml.dist @@ -52,10 +52,11 @@ parameters: export_mobi: true export_pdf: true wallabag_url: http://v2.wallabag.org + wallabag_support_url: 'https://www.wallabag.org/pages/support.html' # default user config items_on_page: 12 theme: material - language: en_US + language: en from_email: no-reply@wallabag.org rss_limit: 50 diff --git a/app/config/tests/parameters.yml.dist.mysql b/app/config/tests/parameters.yml.dist.mysql index 5b29690c..096ad8c7 100644 --- a/app/config/tests/parameters.yml.dist.mysql +++ b/app/config/tests/parameters.yml.dist.mysql @@ -52,6 +52,7 @@ parameters: export_mobi: true export_pdf: true wallabag_url: http://v2.wallabag.org + wallabag_support_url: 'https://www.wallabag.org/pages/support.html' # default user config items_on_page: 12 diff --git a/app/config/tests/parameters.yml.dist.pgsql b/app/config/tests/parameters.yml.dist.pgsql index efdac961..ca3f6ea2 100644 --- a/app/config/tests/parameters.yml.dist.pgsql +++ b/app/config/tests/parameters.yml.dist.pgsql @@ -52,6 +52,7 @@ parameters: export_mobi: true export_pdf: true wallabag_url: http://v2.wallabag.org + wallabag_support_url: 'https://www.wallabag.org/pages/support.html' # default user config items_on_page: 12 diff --git a/app/config/tests/parameters.yml.dist.sqlite b/app/config/tests/parameters.yml.dist.sqlite index 276d1147..92460bcf 100644 --- a/app/config/tests/parameters.yml.dist.sqlite +++ b/app/config/tests/parameters.yml.dist.sqlite @@ -52,6 +52,7 @@ parameters: export_mobi: true export_pdf: true wallabag_url: http://v2.wallabag.org + wallabag_support_url: 'https://www.wallabag.org/pages/support.html' # default user config items_on_page: 12 diff --git a/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadConfigData.php b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadConfigData.php index 84b78a89..3b3c1e97 100644 --- a/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadConfigData.php +++ b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadConfigData.php @@ -25,7 +25,7 @@ class LoadConfigData extends AbstractFixture implements OrderedFixtureInterface $adminConfig->setTheme('material'); $adminConfig->setItemsPerPage(30); - $adminConfig->setLanguage('en_US'); + $adminConfig->setLanguage('en'); $manager->persist($adminConfig); @@ -34,7 +34,7 @@ class LoadConfigData extends AbstractFixture implements OrderedFixtureInterface $bobConfig = new Config($this->getReference('bob-user')); $bobConfig->setTheme('default'); $bobConfig->setItemsPerPage(10); - $bobConfig->setLanguage('fr_FR'); + $bobConfig->setLanguage('fr'); $manager->persist($bobConfig); diff --git a/src/Wallabag/CoreBundle/Resources/views/themes/baggy/Config/index.html.twig b/src/Wallabag/CoreBundle/Resources/views/themes/baggy/Config/index.html.twig index cc797c63..d9850f7a 100644 --- a/src/Wallabag/CoreBundle/Resources/views/themes/baggy/Config/index.html.twig +++ b/src/Wallabag/CoreBundle/Resources/views/themes/baggy/Config/index.html.twig @@ -40,6 +40,10 @@ {{ form_start(form.rss) }} {{ form_errors(form.rss) }} +
+ {% trans %}RSS feeds provided by wallabag allow you to read your saved articles with your favourite RSS reader.{% endtrans %} +
+
@@ -101,6 +105,10 @@
{% if twofactor_auth %} +
+ {% trans %}Enabling two factor authentication means you'll receive an email with a code on every new untrusted connexion{% endtrans %} +
+
{{ form_label(form.user.twoFactorAuthentication) }} diff --git a/src/Wallabag/CoreBundle/Resources/views/themes/material/Config/index.html.twig b/src/Wallabag/CoreBundle/Resources/views/themes/material/Config/index.html.twig index d060311d..8743dc1d 100644 --- a/src/Wallabag/CoreBundle/Resources/views/themes/material/Config/index.html.twig +++ b/src/Wallabag/CoreBundle/Resources/views/themes/material/Config/index.html.twig @@ -131,6 +131,12 @@
{% if twofactor_auth %} +
+
+ {% trans %}Enabling two factor authentication means you'll receive an email with a code on every new untrusted connexion{% endtrans %} +
+
+
{{ form_widget(form.user.twoFactorAuthentication) }} diff --git a/src/Wallabag/CoreBundle/Tests/Controller/ConfigControllerTest.php b/src/Wallabag/CoreBundle/Tests/Controller/ConfigControllerTest.php index 7b32354f..89ca31e2 100644 --- a/src/Wallabag/CoreBundle/Tests/Controller/ConfigControllerTest.php +++ b/src/Wallabag/CoreBundle/Tests/Controller/ConfigControllerTest.php @@ -44,7 +44,7 @@ class ConfigControllerTest extends WallabagCoreTestCase $form = $crawler->filter('button[id=config_save]')->form(); $data = array( - 'config[theme]' => 0, + 'config[theme]' => 'baggy', 'config[items_per_page]' => '30', 'config[language]' => 'en', ); @@ -63,7 +63,7 @@ class ConfigControllerTest extends WallabagCoreTestCase { return array( array(array( - 'config[theme]' => 0, + 'config[theme]' => 'baggy', 'config[items_per_page]' => '', 'config[language]' => 'en', )), 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 @@ +root('wallabag_user'); + + return $treeBuilder; + } +} 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 @@ +processConfiguration($configuration, $configs); + + $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); + $loader->load('services.yml'); + } + + public function getAlias() + { + return 'wallabag_user'; + } +} 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 @@ +mailer = $mailer; + $this->translator = $translator; + $this->senderEmail = $senderEmail; + $this->senderName = $senderName; + $this->supportUrl = $supportUrl; + } + + /** + * Send the auth code to the user via email. + * + * @param TwoFactorInterface $user + */ + public function sendAuthCode(TwoFactorInterface $user) + { + $message = new \Swift_Message(); + $message + ->setTo($user->getEmail()) + ->setFrom($this->senderEmail, $this->senderName) + ->setSubject($this->translator->trans('auth_code.mailer.subject', array(), 'wallabag_user')) + ->setBody($this->translator->trans( + 'auth_code.mailer.body', + [ + '%user%' => $user->getName(), + '%code%' => $user->getEmailAuthCode(), + '%support%' => $this->supportUrl, + ], + 'wallabag_user' + )) + ; + + $this->mailer->send($message); + } +} 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 @@ +services: + wallabag_user.auth_code_mailer: + class: Wallabag\UserBundle\Mailer\AuthCodeMailer + arguments: + - "@mailer" + - "@translator" + - "%scheb_two_factor.email.sender_email%" + - "%scheb_two_factor.email.sender_name%" + - "%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 @@ +# Two factor mail +auth_code.mailer.subject: 'Wallabag authentication Code' +auth_code.mailer.body: | + Hi %user%, + + 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. + Here is the code: %code% + + Please don't hesitate to contact us if you have any problems: %support% + 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 @@ +# Two factor mail +auth_code.mailer.subject: "Code d'authentification wallabag" +auth_code.mailer.body: | + Bonjour %user%, + + 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. + Voici le code à renseigner: %code% + + Si vous avez un problème de connexion, n'hésitez pas à contacter le support: %support% + 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 @@ +messages); + } + + public function getMessages() + { + return $this->messages; + } +} + +class AuthCodeMailerTest extends \PHPUnit_Framework_TestCase +{ + protected $mailer; + protected $spool; + protected $dataCollector; + + protected function setUp() + { + $this->spool = new CountableMemorySpool(); + $transport = new \Swift_Transport_SpoolTransport( + new \Swift_Events_SimpleEventDispatcher(), + $this->spool + ); + $this->mailer = new \Swift_Mailer($transport); + + $translator = new Translator('en'); + $translator->addLoader('array', new ArrayLoader()); + $translator->addResource('array', array( + 'auth_code.mailer.subject' => 'auth_code subject', + 'auth_code.mailer.body' => 'Hi %user%, here is the code: %code% and the support: %support%', + ), 'en', 'wallabag_user'); + + $this->dataCollector = new DataCollectorTranslator($translator); + } + + public function testSendEmail() + { + $user = new User(); + $user->setTwoFactorAuthentication(true); + $user->setEmailAuthCode(666666); + $user->setEmail('test@wallabag.io'); + $user->setName('Bob'); + + $authCodeMailer = new AuthCodeMailer( + $this->mailer, + $this->dataCollector, + 'nobody@test.io', + 'wallabag test', + 'http://0.0.0.0' + ); + + $authCodeMailer->sendAuthCode($user); + + $this->assertCount(1, $this->spool); + + $msg = $this->spool->getMessages()[0]; + $this->assertArrayHasKey('test@wallabag.io', $msg->getTo()); + $this->assertEquals(array('nobody@test.io' => 'wallabag test'), $msg->getFrom()); + $this->assertEquals('auth_code subject', $msg->getSubject()); + $this->assertContains('Hi Bob, here is the code: 666666 and the support: http://0.0.0.0', $msg->toString()); + } +} -- 2.41.0