From ca17abce2d3963e266bee905ab084ddfa7e1ff18 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Fri, 30 Sep 2016 21:01:36 +0200 Subject: Create user config in one place Using a listener, user config is now created when a user: - is created from the command line - register (with or without email confirmation) - is created from the config panel --- src/Wallabag/CoreBundle/Command/InstallCommand.php | 13 ++- .../CoreBundle/Controller/ConfigController.php | 18 ++--- .../RegistrationConfirmedListener.php | 50 ------------ .../CoreBundle/Resources/config/services.yml | 11 --- .../EventListener/CreateConfigListener.php | 62 +++++++++++++++ .../UserBundle/Resources/config/services.yml | 12 +++ .../RegistrationConfirmedListenerTest.php | 91 --------------------- .../EventListener/CreateConfigListenerTest.php | 93 ++++++++++++++++++++++ 8 files changed, 179 insertions(+), 171 deletions(-) delete mode 100644 src/Wallabag/CoreBundle/EventListener/RegistrationConfirmedListener.php create mode 100644 src/Wallabag/UserBundle/EventListener/CreateConfigListener.php delete mode 100644 tests/Wallabag/CoreBundle/EventListener/RegistrationConfirmedListenerTest.php create mode 100644 tests/Wallabag/UserBundle/EventListener/CreateConfigListenerTest.php diff --git a/src/Wallabag/CoreBundle/Command/InstallCommand.php b/src/Wallabag/CoreBundle/Command/InstallCommand.php index 3873d2d3..cc7c2c94 100644 --- a/src/Wallabag/CoreBundle/Command/InstallCommand.php +++ b/src/Wallabag/CoreBundle/Command/InstallCommand.php @@ -2,6 +2,8 @@ namespace Wallabag\CoreBundle\Command; +use FOS\UserBundle\Event\UserEvent; +use FOS\UserBundle\FOSUserEvents; use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Input\ArrayInput; @@ -236,14 +238,9 @@ class InstallCommand extends ContainerAwareCommand $em->persist($user); - $config = new Config($user); - $config->setTheme($this->getContainer()->getParameter('wallabag_core.theme')); - $config->setItemsPerPage($this->getContainer()->getParameter('wallabag_core.items_on_page')); - $config->setRssLimit($this->getContainer()->getParameter('wallabag_core.rss_limit')); - $config->setReadingSpeed($this->getContainer()->getParameter('wallabag_core.reading_speed')); - $config->setLanguage($this->getContainer()->getParameter('wallabag_core.language')); - - $em->persist($config); + // dispatch a created event so the associated config will be created + $event = new UserEvent($user); + $this->getContainer()->get('event_dispatcher')->dispatch(FOSUserEvents::USER_CREATED, $event); $this->defaultOutput->writeln(''); diff --git a/src/Wallabag/CoreBundle/Controller/ConfigController.php b/src/Wallabag/CoreBundle/Controller/ConfigController.php index 4f75511b..75a9af0b 100644 --- a/src/Wallabag/CoreBundle/Controller/ConfigController.php +++ b/src/Wallabag/CoreBundle/Controller/ConfigController.php @@ -2,6 +2,8 @@ namespace Wallabag\CoreBundle\Controller; +use FOS\UserBundle\Event\UserEvent; +use FOS\UserBundle\FOSUserEvents; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\JsonResponse; @@ -133,18 +135,11 @@ class ConfigController extends Controller $newUserForm->handleRequest($request); if ($newUserForm->isValid() && $this->get('security.authorization_checker')->isGranted('ROLE_SUPER_ADMIN')) { - $userManager->updateUser($newUser, true); + $userManager->updateUser($newUser); - $config = new Config($newUser); - $config->setTheme($this->getParameter('wallabag_core.theme')); - $config->setItemsPerPage($this->getParameter('wallabag_core.items_on_page')); - $config->setRssLimit($this->getParameter('wallabag_core.rss_limit')); - $config->setLanguage($this->getParameter('wallabag_core.language')); - $config->setReadingSpeed($this->getParameter('wallabag_core.reading_speed')); - - $em->persist($config); - - $em->flush(); + // dispatch a created event so the associated config will be created + $event = new UserEvent($newUser, $request); + $this->get('event_dispatcher')->dispatch(FOSUserEvents::USER_CREATED, $event); $this->get('session')->getFlashBag()->add( 'notice', @@ -238,6 +233,7 @@ class ConfigController extends Controller ->getRepository('WallabagCoreBundle:Config') ->findOneByUser($this->getUser()); + // should NEVER HAPPEN ... if (!$config) { $config = new Config($this->getUser()); } diff --git a/src/Wallabag/CoreBundle/EventListener/RegistrationConfirmedListener.php b/src/Wallabag/CoreBundle/EventListener/RegistrationConfirmedListener.php deleted file mode 100644 index 10586126..00000000 --- a/src/Wallabag/CoreBundle/EventListener/RegistrationConfirmedListener.php +++ /dev/null @@ -1,50 +0,0 @@ -em = $em; - $this->theme = $theme; - $this->itemsOnPage = $itemsOnPage; - $this->rssLimit = $rssLimit; - $this->language = $language; - } - - public static function getSubscribedEvents() - { - return [ - FOSUserEvents::REGISTRATION_CONFIRMED => 'authenticate', - ]; - } - - public function authenticate(FilterUserResponseEvent $event, $eventName = null, EventDispatcherInterface $eventDispatcher = null) - { - if (!$event->getUser()->isEnabled()) { - return; - } - - $config = new Config($event->getUser()); - $config->setTheme($this->theme); - $config->setItemsPerPage($this->itemsOnPage); - $config->setRssLimit($this->rssLimit); - $config->setLanguage($this->language); - $this->em->persist($config); - $this->em->flush(); - } -} diff --git a/src/Wallabag/CoreBundle/Resources/config/services.yml b/src/Wallabag/CoreBundle/Resources/config/services.yml index 23e6d3ca..5a727ec5 100644 --- a/src/Wallabag/CoreBundle/Resources/config/services.yml +++ b/src/Wallabag/CoreBundle/Resources/config/services.yml @@ -88,17 +88,6 @@ services: arguments: - WallabagCoreBundle:Tag - wallabag_core.registration_confirmed: - class: Wallabag\CoreBundle\EventListener\RegistrationConfirmedListener - arguments: - - "@doctrine.orm.entity_manager" - - "%wallabag_core.theme%" - - "%wallabag_core.items_on_page%" - - "%wallabag_core.rss_limit%" - - "%wallabag_core.language%" - tags: - - { name: kernel.event_subscriber } - wallabag_core.helper.entries_export: class: Wallabag\CoreBundle\Helper\EntriesExport arguments: diff --git a/src/Wallabag/UserBundle/EventListener/CreateConfigListener.php b/src/Wallabag/UserBundle/EventListener/CreateConfigListener.php new file mode 100644 index 00000000..15f4ac3d --- /dev/null +++ b/src/Wallabag/UserBundle/EventListener/CreateConfigListener.php @@ -0,0 +1,62 @@ +em = $em; + $this->theme = $theme; + $this->itemsOnPage = $itemsOnPage; + $this->rssLimit = $rssLimit; + $this->language = $language; + $this->readingSpeed = $readingSpeed; + } + + public static function getSubscribedEvents() + { + return [ + // when a user register using the normal form + FOSUserEvents::REGISTRATION_COMPLETED => 'createConfig', + // when we manually create a user using the command line + // OR when we create it from the config UI + FOSUserEvents::USER_CREATED => 'createConfig', + ]; + } + + public function createConfig(UserEvent $event, $eventName = null, EventDispatcherInterface $eventDispatcher = null) + { + if (!$event->getUser()->isEnabled()) { + return; + } + + $config = new Config($event->getUser()); + $config->setTheme($this->theme); + $config->setItemsPerPage($this->itemsOnPage); + $config->setRssLimit($this->rssLimit); + $config->setLanguage($this->language); + $config->setReadingSpeed($this->readingSpeed); + + $this->em->persist($config); + $this->em->flush(); + } +} diff --git a/src/Wallabag/UserBundle/Resources/config/services.yml b/src/Wallabag/UserBundle/Resources/config/services.yml index 05830555..eb9c8e67 100644 --- a/src/Wallabag/UserBundle/Resources/config/services.yml +++ b/src/Wallabag/UserBundle/Resources/config/services.yml @@ -20,3 +20,15 @@ services: factory: [ "@doctrine.orm.default_entity_manager", getRepository ] arguments: - WallabagUserBundle:User + + wallabag_user.create_config: + class: Wallabag\UserBundle\EventListener\CreateConfigListener + arguments: + - "@doctrine.orm.entity_manager" + - "%wallabag_core.theme%" + - "%wallabag_core.items_on_page%" + - "%wallabag_core.rss_limit%" + - "%wallabag_core.language%" + - "%wallabag_core.reading_speed%" + tags: + - { name: kernel.event_subscriber } diff --git a/tests/Wallabag/CoreBundle/EventListener/RegistrationConfirmedListenerTest.php b/tests/Wallabag/CoreBundle/EventListener/RegistrationConfirmedListenerTest.php deleted file mode 100644 index e45722fa..00000000 --- a/tests/Wallabag/CoreBundle/EventListener/RegistrationConfirmedListenerTest.php +++ /dev/null @@ -1,91 +0,0 @@ -em = $this->getMockBuilder('Doctrine\ORM\EntityManager') - ->disableOriginalConstructor() - ->getMock(); - - $this->listener = new RegistrationConfirmedListener( - $this->em, - 'baggy', - 20, - 50, - 'fr' - ); - - $this->dispatcher = new EventDispatcher(); - $this->dispatcher->addSubscriber($this->listener); - - $this->request = Request::create('/'); - $this->response = Response::create(); - } - - public function testWithInvalidUser() - { - $user = new User(); - $user->setEnabled(false); - - $event = new FilterUserResponseEvent( - $user, - $this->request, - $this->response - ); - - $this->em->expects($this->never())->method('persist'); - $this->em->expects($this->never())->method('flush'); - - $this->dispatcher->dispatch( - FOSUserEvents::REGISTRATION_CONFIRMED, - $event - ); - } - - public function testWithValidUser() - { - $user = new User(); - $user->setEnabled(true); - - $event = new FilterUserResponseEvent( - $user, - $this->request, - $this->response - ); - - $config = new Config($user); - $config->setTheme('baggy'); - $config->setItemsPerPage(20); - $config->setRssLimit(50); - $config->setLanguage('fr'); - - $this->em->expects($this->once()) - ->method('persist') - ->will($this->returnValue($config)); - $this->em->expects($this->once()) - ->method('flush'); - - $this->dispatcher->dispatch( - FOSUserEvents::REGISTRATION_CONFIRMED, - $event - ); - } -} diff --git a/tests/Wallabag/UserBundle/EventListener/CreateConfigListenerTest.php b/tests/Wallabag/UserBundle/EventListener/CreateConfigListenerTest.php new file mode 100644 index 00000000..0cebd3e4 --- /dev/null +++ b/tests/Wallabag/UserBundle/EventListener/CreateConfigListenerTest.php @@ -0,0 +1,93 @@ +em = $this->getMockBuilder('Doctrine\ORM\EntityManager') + ->disableOriginalConstructor() + ->getMock(); + + $this->listener = new CreateConfigListener( + $this->em, + 'baggy', + 20, + 50, + 'fr', + 1 + ); + + $this->dispatcher = new EventDispatcher(); + $this->dispatcher->addSubscriber($this->listener); + + $this->request = Request::create('/'); + $this->response = Response::create(); + } + + public function testWithInvalidUser() + { + $user = new User(); + $user->setEnabled(false); + + $event = new FilterUserResponseEvent( + $user, + $this->request, + $this->response + ); + + $this->em->expects($this->never())->method('persist'); + $this->em->expects($this->never())->method('flush'); + + $this->dispatcher->dispatch( + FOSUserEvents::REGISTRATION_COMPLETED, + $event + ); + } + + public function testWithValidUser() + { + $user = new User(); + $user->setEnabled(true); + + $event = new FilterUserResponseEvent( + $user, + $this->request, + $this->response + ); + + $config = new Config($user); + $config->setTheme('baggy'); + $config->setItemsPerPage(20); + $config->setRssLimit(50); + $config->setLanguage('fr'); + $config->setReadingSpeed(1); + + $this->em->expects($this->once()) + ->method('persist') + ->will($this->returnValue($config)); + $this->em->expects($this->once()) + ->method('flush'); + + $this->dispatcher->dispatch( + FOSUserEvents::REGISTRATION_COMPLETED, + $event + ); + } +} -- cgit v1.2.3 From fb7354c4a430f4f4916a1247aa1b1bf564ead0da Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Fri, 30 Sep 2016 21:02:44 +0200 Subject: Fix bad template name When a user register, the template displayed saying it should now check its email was misspelled. Resulting in displaying the default one (with margin issue). --- .../Resources/views/Registration/checkEmail.html.twig | 11 ----------- .../Resources/views/Registration/check_email.html.twig | 11 +++++++++++ 2 files changed, 11 insertions(+), 11 deletions(-) delete mode 100644 src/Wallabag/UserBundle/Resources/views/Registration/checkEmail.html.twig create mode 100644 src/Wallabag/UserBundle/Resources/views/Registration/check_email.html.twig diff --git a/src/Wallabag/UserBundle/Resources/views/Registration/checkEmail.html.twig b/src/Wallabag/UserBundle/Resources/views/Registration/checkEmail.html.twig deleted file mode 100644 index 50937276..00000000 --- a/src/Wallabag/UserBundle/Resources/views/Registration/checkEmail.html.twig +++ /dev/null @@ -1,11 +0,0 @@ -{% extends "FOSUserBundle::layout.html.twig" %} - -{% trans_default_domain 'FOSUserBundle' %} - -{% block fos_user_content %} -
-
-

{{ 'registration.check_email'|trans({'%email%': user.email}) }}

-
-
-{% endblock fos_user_content %} diff --git a/src/Wallabag/UserBundle/Resources/views/Registration/check_email.html.twig b/src/Wallabag/UserBundle/Resources/views/Registration/check_email.html.twig new file mode 100644 index 00000000..50937276 --- /dev/null +++ b/src/Wallabag/UserBundle/Resources/views/Registration/check_email.html.twig @@ -0,0 +1,11 @@ +{% extends "FOSUserBundle::layout.html.twig" %} + +{% trans_default_domain 'FOSUserBundle' %} + +{% block fos_user_content %} +
+
+

{{ 'registration.check_email'|trans({'%email%': user.email}) }}

+
+
+{% endblock fos_user_content %} -- cgit v1.2.3