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
This commit is contained in:
Jeremy Benoist 2016-09-30 21:01:36 +02:00
parent 114c55c0a6
commit ca17abce2d
No known key found for this signature in database
GPG Key ID: BCA73962457ACC3C
6 changed files with 51 additions and 43 deletions

View File

@ -2,6 +2,8 @@
namespace Wallabag\CoreBundle\Command; namespace Wallabag\CoreBundle\Command;
use FOS\UserBundle\Event\UserEvent;
use FOS\UserBundle\FOSUserEvents;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Input\ArrayInput;
@ -236,14 +238,9 @@ class InstallCommand extends ContainerAwareCommand
$em->persist($user); $em->persist($user);
$config = new Config($user); // dispatch a created event so the associated config will be created
$config->setTheme($this->getContainer()->getParameter('wallabag_core.theme')); $event = new UserEvent($user);
$config->setItemsPerPage($this->getContainer()->getParameter('wallabag_core.items_on_page')); $this->getContainer()->get('event_dispatcher')->dispatch(FOSUserEvents::USER_CREATED, $event);
$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);
$this->defaultOutput->writeln(''); $this->defaultOutput->writeln('');

View File

@ -2,6 +2,8 @@
namespace Wallabag\CoreBundle\Controller; namespace Wallabag\CoreBundle\Controller;
use FOS\UserBundle\Event\UserEvent;
use FOS\UserBundle\FOSUserEvents;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
@ -133,18 +135,11 @@ class ConfigController extends Controller
$newUserForm->handleRequest($request); $newUserForm->handleRequest($request);
if ($newUserForm->isValid() && $this->get('security.authorization_checker')->isGranted('ROLE_SUPER_ADMIN')) { if ($newUserForm->isValid() && $this->get('security.authorization_checker')->isGranted('ROLE_SUPER_ADMIN')) {
$userManager->updateUser($newUser, true); $userManager->updateUser($newUser);
$config = new Config($newUser); // dispatch a created event so the associated config will be created
$config->setTheme($this->getParameter('wallabag_core.theme')); $event = new UserEvent($newUser, $request);
$config->setItemsPerPage($this->getParameter('wallabag_core.items_on_page')); $this->get('event_dispatcher')->dispatch(FOSUserEvents::USER_CREATED, $event);
$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();
$this->get('session')->getFlashBag()->add( $this->get('session')->getFlashBag()->add(
'notice', 'notice',
@ -238,6 +233,7 @@ class ConfigController extends Controller
->getRepository('WallabagCoreBundle:Config') ->getRepository('WallabagCoreBundle:Config')
->findOneByUser($this->getUser()); ->findOneByUser($this->getUser());
// should NEVER HAPPEN ...
if (!$config) { if (!$config) {
$config = new Config($this->getUser()); $config = new Config($this->getUser());
} }

View File

@ -88,17 +88,6 @@ services:
arguments: arguments:
- WallabagCoreBundle:Tag - 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: wallabag_core.helper.entries_export:
class: Wallabag\CoreBundle\Helper\EntriesExport class: Wallabag\CoreBundle\Helper\EntriesExport
arguments: arguments:

View File

@ -1,39 +1,49 @@
<?php <?php
namespace Wallabag\CoreBundle\EventListener; namespace Wallabag\UserBundle\EventListener;
use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManager;
use FOS\UserBundle\Event\FilterUserResponseEvent; use FOS\UserBundle\Event\UserEvent;
use FOS\UserBundle\FOSUserEvents; use FOS\UserBundle\FOSUserEvents;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Wallabag\CoreBundle\Entity\Config; use Wallabag\CoreBundle\Entity\Config;
class RegistrationConfirmedListener implements EventSubscriberInterface /**
* This listener will create the associated configuration when a user register.
* This configuration will be created right after the registration (no matter if it needs an email validation).
*/
class CreateConfigListener implements EventSubscriberInterface
{ {
private $em; private $em;
private $theme; private $theme;
private $itemsOnPage; private $itemsOnPage;
private $rssLimit; private $rssLimit;
private $language; private $language;
private $readingSpeed;
public function __construct(EntityManager $em, $theme, $itemsOnPage, $rssLimit, $language) public function __construct(EntityManager $em, $theme, $itemsOnPage, $rssLimit, $language, $readingSpeed)
{ {
$this->em = $em; $this->em = $em;
$this->theme = $theme; $this->theme = $theme;
$this->itemsOnPage = $itemsOnPage; $this->itemsOnPage = $itemsOnPage;
$this->rssLimit = $rssLimit; $this->rssLimit = $rssLimit;
$this->language = $language; $this->language = $language;
$this->readingSpeed = $readingSpeed;
} }
public static function getSubscribedEvents() public static function getSubscribedEvents()
{ {
return [ return [
FOSUserEvents::REGISTRATION_CONFIRMED => 'authenticate', // 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 authenticate(FilterUserResponseEvent $event, $eventName = null, EventDispatcherInterface $eventDispatcher = null) public function createConfig(UserEvent $event, $eventName = null, EventDispatcherInterface $eventDispatcher = null)
{ {
if (!$event->getUser()->isEnabled()) { if (!$event->getUser()->isEnabled()) {
return; return;
@ -44,6 +54,8 @@ class RegistrationConfirmedListener implements EventSubscriberInterface
$config->setItemsPerPage($this->itemsOnPage); $config->setItemsPerPage($this->itemsOnPage);
$config->setRssLimit($this->rssLimit); $config->setRssLimit($this->rssLimit);
$config->setLanguage($this->language); $config->setLanguage($this->language);
$config->setReadingSpeed($this->readingSpeed);
$this->em->persist($config); $this->em->persist($config);
$this->em->flush(); $this->em->flush();
} }

View File

@ -20,3 +20,15 @@ services:
factory: [ "@doctrine.orm.default_entity_manager", getRepository ] factory: [ "@doctrine.orm.default_entity_manager", getRepository ]
arguments: arguments:
- WallabagUserBundle:User - 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 }

View File

@ -1,6 +1,6 @@
<?php <?php
namespace Tests\Wallabag\CoreBundle\EventListener; namespace Tests\Wallabag\UserBundle\EventListener;
use FOS\UserBundle\Event\FilterUserResponseEvent; use FOS\UserBundle\Event\FilterUserResponseEvent;
use FOS\UserBundle\FOSUserEvents; use FOS\UserBundle\FOSUserEvents;
@ -8,10 +8,10 @@ use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Wallabag\CoreBundle\Entity\Config; use Wallabag\CoreBundle\Entity\Config;
use Wallabag\CoreBundle\EventListener\RegistrationConfirmedListener; use Wallabag\UserBundle\EventListener\CreateConfigListener;
use Wallabag\UserBundle\Entity\User; use Wallabag\UserBundle\Entity\User;
class RegistrationConfirmedListenerTest extends \PHPUnit_Framework_TestCase class CreateConfigListenerTest extends \PHPUnit_Framework_TestCase
{ {
private $em; private $em;
private $listener; private $listener;
@ -25,12 +25,13 @@ class RegistrationConfirmedListenerTest extends \PHPUnit_Framework_TestCase
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$this->listener = new RegistrationConfirmedListener( $this->listener = new CreateConfigListener(
$this->em, $this->em,
'baggy', 'baggy',
20, 20,
50, 50,
'fr' 'fr',
1
); );
$this->dispatcher = new EventDispatcher(); $this->dispatcher = new EventDispatcher();
@ -55,7 +56,7 @@ class RegistrationConfirmedListenerTest extends \PHPUnit_Framework_TestCase
$this->em->expects($this->never())->method('flush'); $this->em->expects($this->never())->method('flush');
$this->dispatcher->dispatch( $this->dispatcher->dispatch(
FOSUserEvents::REGISTRATION_CONFIRMED, FOSUserEvents::REGISTRATION_COMPLETED,
$event $event
); );
} }
@ -76,6 +77,7 @@ class RegistrationConfirmedListenerTest extends \PHPUnit_Framework_TestCase
$config->setItemsPerPage(20); $config->setItemsPerPage(20);
$config->setRssLimit(50); $config->setRssLimit(50);
$config->setLanguage('fr'); $config->setLanguage('fr');
$config->setReadingSpeed(1);
$this->em->expects($this->once()) $this->em->expects($this->once())
->method('persist') ->method('persist')
@ -84,7 +86,7 @@ class RegistrationConfirmedListenerTest extends \PHPUnit_Framework_TestCase
->method('flush'); ->method('flush');
$this->dispatcher->dispatch( $this->dispatcher->dispatch(
FOSUserEvents::REGISTRATION_CONFIRMED, FOSUserEvents::REGISTRATION_COMPLETED,
$event $event
); );
} }