mirror of
https://github.com/wallabag/wallabag.git
synced 2024-11-26 19:11:07 +00:00
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:
parent
114c55c0a6
commit
ca17abce2d
6 changed files with 51 additions and 43 deletions
|
@ -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('');
|
||||
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -1,39 +1,49 @@
|
|||
<?php
|
||||
|
||||
namespace Wallabag\CoreBundle\EventListener;
|
||||
namespace Wallabag\UserBundle\EventListener;
|
||||
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use FOS\UserBundle\Event\FilterUserResponseEvent;
|
||||
use FOS\UserBundle\Event\UserEvent;
|
||||
use FOS\UserBundle\FOSUserEvents;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
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 $theme;
|
||||
private $itemsOnPage;
|
||||
private $rssLimit;
|
||||
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->theme = $theme;
|
||||
$this->itemsOnPage = $itemsOnPage;
|
||||
$this->rssLimit = $rssLimit;
|
||||
$this->language = $language;
|
||||
$this->readingSpeed = $readingSpeed;
|
||||
}
|
||||
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
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()) {
|
||||
return;
|
||||
|
@ -44,6 +54,8 @@ class RegistrationConfirmedListener implements EventSubscriberInterface
|
|||
$config->setItemsPerPage($this->itemsOnPage);
|
||||
$config->setRssLimit($this->rssLimit);
|
||||
$config->setLanguage($this->language);
|
||||
$config->setReadingSpeed($this->readingSpeed);
|
||||
|
||||
$this->em->persist($config);
|
||||
$this->em->flush();
|
||||
}
|
|
@ -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 }
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Wallabag\CoreBundle\EventListener;
|
||||
namespace Tests\Wallabag\UserBundle\EventListener;
|
||||
|
||||
use FOS\UserBundle\Event\FilterUserResponseEvent;
|
||||
use FOS\UserBundle\FOSUserEvents;
|
||||
|
@ -8,10 +8,10 @@ use Symfony\Component\EventDispatcher\EventDispatcher;
|
|||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Wallabag\CoreBundle\Entity\Config;
|
||||
use Wallabag\CoreBundle\EventListener\RegistrationConfirmedListener;
|
||||
use Wallabag\UserBundle\EventListener\CreateConfigListener;
|
||||
use Wallabag\UserBundle\Entity\User;
|
||||
|
||||
class RegistrationConfirmedListenerTest extends \PHPUnit_Framework_TestCase
|
||||
class CreateConfigListenerTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
private $em;
|
||||
private $listener;
|
||||
|
@ -25,12 +25,13 @@ class RegistrationConfirmedListenerTest extends \PHPUnit_Framework_TestCase
|
|||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$this->listener = new RegistrationConfirmedListener(
|
||||
$this->listener = new CreateConfigListener(
|
||||
$this->em,
|
||||
'baggy',
|
||||
20,
|
||||
50,
|
||||
'fr'
|
||||
'fr',
|
||||
1
|
||||
);
|
||||
|
||||
$this->dispatcher = new EventDispatcher();
|
||||
|
@ -55,7 +56,7 @@ class RegistrationConfirmedListenerTest extends \PHPUnit_Framework_TestCase
|
|||
$this->em->expects($this->never())->method('flush');
|
||||
|
||||
$this->dispatcher->dispatch(
|
||||
FOSUserEvents::REGISTRATION_CONFIRMED,
|
||||
FOSUserEvents::REGISTRATION_COMPLETED,
|
||||
$event
|
||||
);
|
||||
}
|
||||
|
@ -76,6 +77,7 @@ class RegistrationConfirmedListenerTest extends \PHPUnit_Framework_TestCase
|
|||
$config->setItemsPerPage(20);
|
||||
$config->setRssLimit(50);
|
||||
$config->setLanguage('fr');
|
||||
$config->setReadingSpeed(1);
|
||||
|
||||
$this->em->expects($this->once())
|
||||
->method('persist')
|
||||
|
@ -84,7 +86,7 @@ class RegistrationConfirmedListenerTest extends \PHPUnit_Framework_TestCase
|
|||
->method('flush');
|
||||
|
||||
$this->dispatcher->dispatch(
|
||||
FOSUserEvents::REGISTRATION_CONFIRMED,
|
||||
FOSUserEvents::REGISTRATION_COMPLETED,
|
||||
$event
|
||||
);
|
||||
}
|
Loading…
Reference in a new issue