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 +++++ 6 files changed, 86 insertions(+), 80 deletions(-) delete mode 100644 src/Wallabag/CoreBundle/EventListener/RegistrationConfirmedListener.php create mode 100644 src/Wallabag/UserBundle/EventListener/CreateConfigListener.php (limited to 'src') 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 } -- cgit v1.2.3