diff options
author | Jeremy Benoist <jeremy.benoist@gmail.com> | 2016-09-30 21:01:36 +0200 |
---|---|---|
committer | Jeremy Benoist <jeremy.benoist@gmail.com> | 2016-09-30 21:01:36 +0200 |
commit | ca17abce2d3963e266bee905ab084ddfa7e1ff18 (patch) | |
tree | ce33b43a21acc99300d7046387f99d437f713785 | |
parent | 114c55c0a6eade2ba6c53fe25f61cc58cca91620 (diff) | |
download | wallabag-ca17abce2d3963e266bee905ab084ddfa7e1ff18.tar.gz wallabag-ca17abce2d3963e266bee905ab084ddfa7e1ff18.tar.zst wallabag-ca17abce2d3963e266bee905ab084ddfa7e1ff18.zip |
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
-rw-r--r-- | src/Wallabag/CoreBundle/Command/InstallCommand.php | 13 | ||||
-rw-r--r-- | src/Wallabag/CoreBundle/Controller/ConfigController.php | 18 | ||||
-rw-r--r-- | src/Wallabag/CoreBundle/Resources/config/services.yml | 11 | ||||
-rw-r--r-- | src/Wallabag/UserBundle/EventListener/CreateConfigListener.php (renamed from src/Wallabag/CoreBundle/EventListener/RegistrationConfirmedListener.php) | 24 | ||||
-rw-r--r-- | src/Wallabag/UserBundle/Resources/config/services.yml | 12 | ||||
-rw-r--r-- | tests/Wallabag/UserBundle/EventListener/CreateConfigListenerTest.php (renamed from tests/Wallabag/CoreBundle/EventListener/RegistrationConfirmedListenerTest.php) | 16 |
6 files changed, 51 insertions, 43 deletions
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 @@ | |||
2 | 2 | ||
3 | namespace Wallabag\CoreBundle\Command; | 3 | namespace Wallabag\CoreBundle\Command; |
4 | 4 | ||
5 | use FOS\UserBundle\Event\UserEvent; | ||
6 | use FOS\UserBundle\FOSUserEvents; | ||
5 | use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | 7 | use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
6 | use Symfony\Component\Console\Helper\Table; | 8 | use Symfony\Component\Console\Helper\Table; |
7 | use Symfony\Component\Console\Input\ArrayInput; | 9 | use Symfony\Component\Console\Input\ArrayInput; |
@@ -236,14 +238,9 @@ class InstallCommand extends ContainerAwareCommand | |||
236 | 238 | ||
237 | $em->persist($user); | 239 | $em->persist($user); |
238 | 240 | ||
239 | $config = new Config($user); | 241 | // dispatch a created event so the associated config will be created |
240 | $config->setTheme($this->getContainer()->getParameter('wallabag_core.theme')); | 242 | $event = new UserEvent($user); |
241 | $config->setItemsPerPage($this->getContainer()->getParameter('wallabag_core.items_on_page')); | 243 | $this->getContainer()->get('event_dispatcher')->dispatch(FOSUserEvents::USER_CREATED, $event); |
242 | $config->setRssLimit($this->getContainer()->getParameter('wallabag_core.rss_limit')); | ||
243 | $config->setReadingSpeed($this->getContainer()->getParameter('wallabag_core.reading_speed')); | ||
244 | $config->setLanguage($this->getContainer()->getParameter('wallabag_core.language')); | ||
245 | |||
246 | $em->persist($config); | ||
247 | 244 | ||
248 | $this->defaultOutput->writeln(''); | 245 | $this->defaultOutput->writeln(''); |
249 | 246 | ||
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 @@ | |||
2 | 2 | ||
3 | namespace Wallabag\CoreBundle\Controller; | 3 | namespace Wallabag\CoreBundle\Controller; |
4 | 4 | ||
5 | use FOS\UserBundle\Event\UserEvent; | ||
6 | use FOS\UserBundle\FOSUserEvents; | ||
5 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | 7 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
6 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | 8 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
7 | use Symfony\Component\HttpFoundation\JsonResponse; | 9 | use Symfony\Component\HttpFoundation\JsonResponse; |
@@ -133,18 +135,11 @@ class ConfigController extends Controller | |||
133 | $newUserForm->handleRequest($request); | 135 | $newUserForm->handleRequest($request); |
134 | 136 | ||
135 | if ($newUserForm->isValid() && $this->get('security.authorization_checker')->isGranted('ROLE_SUPER_ADMIN')) { | 137 | if ($newUserForm->isValid() && $this->get('security.authorization_checker')->isGranted('ROLE_SUPER_ADMIN')) { |
136 | $userManager->updateUser($newUser, true); | 138 | $userManager->updateUser($newUser); |
137 | 139 | ||
138 | $config = new Config($newUser); | 140 | // dispatch a created event so the associated config will be created |
139 | $config->setTheme($this->getParameter('wallabag_core.theme')); | 141 | $event = new UserEvent($newUser, $request); |
140 | $config->setItemsPerPage($this->getParameter('wallabag_core.items_on_page')); | 142 | $this->get('event_dispatcher')->dispatch(FOSUserEvents::USER_CREATED, $event); |
141 | $config->setRssLimit($this->getParameter('wallabag_core.rss_limit')); | ||
142 | $config->setLanguage($this->getParameter('wallabag_core.language')); | ||
143 | $config->setReadingSpeed($this->getParameter('wallabag_core.reading_speed')); | ||
144 | |||
145 | $em->persist($config); | ||
146 | |||
147 | $em->flush(); | ||
148 | 143 | ||
149 | $this->get('session')->getFlashBag()->add( | 144 | $this->get('session')->getFlashBag()->add( |
150 | 'notice', | 145 | 'notice', |
@@ -238,6 +233,7 @@ class ConfigController extends Controller | |||
238 | ->getRepository('WallabagCoreBundle:Config') | 233 | ->getRepository('WallabagCoreBundle:Config') |
239 | ->findOneByUser($this->getUser()); | 234 | ->findOneByUser($this->getUser()); |
240 | 235 | ||
236 | // should NEVER HAPPEN ... | ||
241 | if (!$config) { | 237 | if (!$config) { |
242 | $config = new Config($this->getUser()); | 238 | $config = new Config($this->getUser()); |
243 | } | 239 | } |
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: | |||
88 | arguments: | 88 | arguments: |
89 | - WallabagCoreBundle:Tag | 89 | - WallabagCoreBundle:Tag |
90 | 90 | ||
91 | wallabag_core.registration_confirmed: | ||
92 | class: Wallabag\CoreBundle\EventListener\RegistrationConfirmedListener | ||
93 | arguments: | ||
94 | - "@doctrine.orm.entity_manager" | ||
95 | - "%wallabag_core.theme%" | ||
96 | - "%wallabag_core.items_on_page%" | ||
97 | - "%wallabag_core.rss_limit%" | ||
98 | - "%wallabag_core.language%" | ||
99 | tags: | ||
100 | - { name: kernel.event_subscriber } | ||
101 | |||
102 | wallabag_core.helper.entries_export: | 91 | wallabag_core.helper.entries_export: |
103 | class: Wallabag\CoreBundle\Helper\EntriesExport | 92 | class: Wallabag\CoreBundle\Helper\EntriesExport |
104 | arguments: | 93 | arguments: |
diff --git a/src/Wallabag/CoreBundle/EventListener/RegistrationConfirmedListener.php b/src/Wallabag/UserBundle/EventListener/CreateConfigListener.php index 10586126..15f4ac3d 100644 --- a/src/Wallabag/CoreBundle/EventListener/RegistrationConfirmedListener.php +++ b/src/Wallabag/UserBundle/EventListener/CreateConfigListener.php | |||
@@ -1,39 +1,49 @@ | |||
1 | <?php | 1 | <?php |
2 | 2 | ||
3 | namespace Wallabag\CoreBundle\EventListener; | 3 | namespace Wallabag\UserBundle\EventListener; |
4 | 4 | ||
5 | use Doctrine\ORM\EntityManager; | 5 | use Doctrine\ORM\EntityManager; |
6 | use FOS\UserBundle\Event\FilterUserResponseEvent; | 6 | use FOS\UserBundle\Event\UserEvent; |
7 | use FOS\UserBundle\FOSUserEvents; | 7 | use FOS\UserBundle\FOSUserEvents; |
8 | use Symfony\Component\EventDispatcher\EventDispatcherInterface; | 8 | use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
9 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; | 9 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
10 | use Wallabag\CoreBundle\Entity\Config; | 10 | use Wallabag\CoreBundle\Entity\Config; |
11 | 11 | ||
12 | class RegistrationConfirmedListener implements EventSubscriberInterface | 12 | /** |
13 | * This listener will create the associated configuration when a user register. | ||
14 | * This configuration will be created right after the registration (no matter if it needs an email validation). | ||
15 | */ | ||
16 | class CreateConfigListener implements EventSubscriberInterface | ||
13 | { | 17 | { |
14 | private $em; | 18 | private $em; |
15 | private $theme; | 19 | private $theme; |
16 | private $itemsOnPage; | 20 | private $itemsOnPage; |
17 | private $rssLimit; | 21 | private $rssLimit; |
18 | private $language; | 22 | private $language; |
23 | private $readingSpeed; | ||
19 | 24 | ||
20 | public function __construct(EntityManager $em, $theme, $itemsOnPage, $rssLimit, $language) | 25 | public function __construct(EntityManager $em, $theme, $itemsOnPage, $rssLimit, $language, $readingSpeed) |
21 | { | 26 | { |
22 | $this->em = $em; | 27 | $this->em = $em; |
23 | $this->theme = $theme; | 28 | $this->theme = $theme; |
24 | $this->itemsOnPage = $itemsOnPage; | 29 | $this->itemsOnPage = $itemsOnPage; |
25 | $this->rssLimit = $rssLimit; | 30 | $this->rssLimit = $rssLimit; |
26 | $this->language = $language; | 31 | $this->language = $language; |
32 | $this->readingSpeed = $readingSpeed; | ||
27 | } | 33 | } |
28 | 34 | ||
29 | public static function getSubscribedEvents() | 35 | public static function getSubscribedEvents() |
30 | { | 36 | { |
31 | return [ | 37 | return [ |
32 | FOSUserEvents::REGISTRATION_CONFIRMED => 'authenticate', | 38 | // when a user register using the normal form |
39 | FOSUserEvents::REGISTRATION_COMPLETED => 'createConfig', | ||
40 | // when we manually create a user using the command line | ||
41 | // OR when we create it from the config UI | ||
42 | FOSUserEvents::USER_CREATED => 'createConfig', | ||
33 | ]; | 43 | ]; |
34 | } | 44 | } |
35 | 45 | ||
36 | public function authenticate(FilterUserResponseEvent $event, $eventName = null, EventDispatcherInterface $eventDispatcher = null) | 46 | public function createConfig(UserEvent $event, $eventName = null, EventDispatcherInterface $eventDispatcher = null) |
37 | { | 47 | { |
38 | if (!$event->getUser()->isEnabled()) { | 48 | if (!$event->getUser()->isEnabled()) { |
39 | return; | 49 | return; |
@@ -44,6 +54,8 @@ class RegistrationConfirmedListener implements EventSubscriberInterface | |||
44 | $config->setItemsPerPage($this->itemsOnPage); | 54 | $config->setItemsPerPage($this->itemsOnPage); |
45 | $config->setRssLimit($this->rssLimit); | 55 | $config->setRssLimit($this->rssLimit); |
46 | $config->setLanguage($this->language); | 56 | $config->setLanguage($this->language); |
57 | $config->setReadingSpeed($this->readingSpeed); | ||
58 | |||
47 | $this->em->persist($config); | 59 | $this->em->persist($config); |
48 | $this->em->flush(); | 60 | $this->em->flush(); |
49 | } | 61 | } |
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: | |||
20 | factory: [ "@doctrine.orm.default_entity_manager", getRepository ] | 20 | factory: [ "@doctrine.orm.default_entity_manager", getRepository ] |
21 | arguments: | 21 | arguments: |
22 | - WallabagUserBundle:User | 22 | - WallabagUserBundle:User |
23 | |||
24 | wallabag_user.create_config: | ||
25 | class: Wallabag\UserBundle\EventListener\CreateConfigListener | ||
26 | arguments: | ||
27 | - "@doctrine.orm.entity_manager" | ||
28 | - "%wallabag_core.theme%" | ||
29 | - "%wallabag_core.items_on_page%" | ||
30 | - "%wallabag_core.rss_limit%" | ||
31 | - "%wallabag_core.language%" | ||
32 | - "%wallabag_core.reading_speed%" | ||
33 | tags: | ||
34 | - { name: kernel.event_subscriber } | ||
diff --git a/tests/Wallabag/CoreBundle/EventListener/RegistrationConfirmedListenerTest.php b/tests/Wallabag/UserBundle/EventListener/CreateConfigListenerTest.php index e45722fa..0cebd3e4 100644 --- a/tests/Wallabag/CoreBundle/EventListener/RegistrationConfirmedListenerTest.php +++ b/tests/Wallabag/UserBundle/EventListener/CreateConfigListenerTest.php | |||
@@ -1,6 +1,6 @@ | |||
1 | <?php | 1 | <?php |
2 | 2 | ||
3 | namespace Tests\Wallabag\CoreBundle\EventListener; | 3 | namespace Tests\Wallabag\UserBundle\EventListener; |
4 | 4 | ||
5 | use FOS\UserBundle\Event\FilterUserResponseEvent; | 5 | use FOS\UserBundle\Event\FilterUserResponseEvent; |
6 | use FOS\UserBundle\FOSUserEvents; | 6 | use FOS\UserBundle\FOSUserEvents; |
@@ -8,10 +8,10 @@ use Symfony\Component\EventDispatcher\EventDispatcher; | |||
8 | use Symfony\Component\HttpFoundation\Request; | 8 | use Symfony\Component\HttpFoundation\Request; |
9 | use Symfony\Component\HttpFoundation\Response; | 9 | use Symfony\Component\HttpFoundation\Response; |
10 | use Wallabag\CoreBundle\Entity\Config; | 10 | use Wallabag\CoreBundle\Entity\Config; |
11 | use Wallabag\CoreBundle\EventListener\RegistrationConfirmedListener; | 11 | use Wallabag\UserBundle\EventListener\CreateConfigListener; |
12 | use Wallabag\UserBundle\Entity\User; | 12 | use Wallabag\UserBundle\Entity\User; |
13 | 13 | ||
14 | class RegistrationConfirmedListenerTest extends \PHPUnit_Framework_TestCase | 14 | class CreateConfigListenerTest extends \PHPUnit_Framework_TestCase |
15 | { | 15 | { |
16 | private $em; | 16 | private $em; |
17 | private $listener; | 17 | private $listener; |
@@ -25,12 +25,13 @@ class RegistrationConfirmedListenerTest extends \PHPUnit_Framework_TestCase | |||
25 | ->disableOriginalConstructor() | 25 | ->disableOriginalConstructor() |
26 | ->getMock(); | 26 | ->getMock(); |
27 | 27 | ||
28 | $this->listener = new RegistrationConfirmedListener( | 28 | $this->listener = new CreateConfigListener( |
29 | $this->em, | 29 | $this->em, |
30 | 'baggy', | 30 | 'baggy', |
31 | 20, | 31 | 20, |
32 | 50, | 32 | 50, |
33 | 'fr' | 33 | 'fr', |
34 | 1 | ||
34 | ); | 35 | ); |
35 | 36 | ||
36 | $this->dispatcher = new EventDispatcher(); | 37 | $this->dispatcher = new EventDispatcher(); |
@@ -55,7 +56,7 @@ class RegistrationConfirmedListenerTest extends \PHPUnit_Framework_TestCase | |||
55 | $this->em->expects($this->never())->method('flush'); | 56 | $this->em->expects($this->never())->method('flush'); |
56 | 57 | ||
57 | $this->dispatcher->dispatch( | 58 | $this->dispatcher->dispatch( |
58 | FOSUserEvents::REGISTRATION_CONFIRMED, | 59 | FOSUserEvents::REGISTRATION_COMPLETED, |
59 | $event | 60 | $event |
60 | ); | 61 | ); |
61 | } | 62 | } |
@@ -76,6 +77,7 @@ class RegistrationConfirmedListenerTest extends \PHPUnit_Framework_TestCase | |||
76 | $config->setItemsPerPage(20); | 77 | $config->setItemsPerPage(20); |
77 | $config->setRssLimit(50); | 78 | $config->setRssLimit(50); |
78 | $config->setLanguage('fr'); | 79 | $config->setLanguage('fr'); |
80 | $config->setReadingSpeed(1); | ||
79 | 81 | ||
80 | $this->em->expects($this->once()) | 82 | $this->em->expects($this->once()) |
81 | ->method('persist') | 83 | ->method('persist') |
@@ -84,7 +86,7 @@ class RegistrationConfirmedListenerTest extends \PHPUnit_Framework_TestCase | |||
84 | ->method('flush'); | 86 | ->method('flush'); |
85 | 87 | ||
86 | $this->dispatcher->dispatch( | 88 | $this->dispatcher->dispatch( |
87 | FOSUserEvents::REGISTRATION_CONFIRMED, | 89 | FOSUserEvents::REGISTRATION_COMPLETED, |
88 | $event | 90 | $event |
89 | ); | 91 | ); |
90 | } | 92 | } |