mirror of
https://github.com/wallabag/wallabag.git
synced 2025-08-31 18:31:02 +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
|
@ -0,0 +1,62 @@
|
|||
<?php
|
||||
|
||||
namespace Wallabag\UserBundle\EventListener;
|
||||
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use FOS\UserBundle\Event\UserEvent;
|
||||
use FOS\UserBundle\FOSUserEvents;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
use Wallabag\CoreBundle\Entity\Config;
|
||||
|
||||
/**
|
||||
* 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, $readingSpeed)
|
||||
{
|
||||
$this->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();
|
||||
}
|
||||
}
|
|
@ -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 }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue