Symfony2 login event listener and security.interactive_login event never fired using FOSUser

I am trying to add a Symfony2 registration event listener, the goal is to set up a localized database, if the user is registered, for each request and is dropped by default, not one of the users is registered. I am using FOSUserBundle and I am trying to fire the security.interactive_login event. I find a lot of code on the Internet, like this one: http://dev.dbl-a.com/symfony-2-0/how-to-add-a-symfony2-login-event-listener/

I have my own child package FOSUserBundle and this implementation in services.yml:

my_user.security.interactive_login_listener: class: My\UserBundle\EventListener\UserListener arguments: [@security.context, @doctrine] tags: - { name: kernel.event_listener, event: security.interactive_login, method: setLocaleForAuthenticatedUser } my_user.security.kernel_request_listener: class: My\UserBundle\EventListener\UserListener arguments: [@security.context, @doctrine] tags: - { name: kernel.event_listener, event: kernel.request, method: setLocaleForUnauthenticatedUser } 

The problem is that the security.interactive_login event never fires even when logged, even when logged. In contrast, my setLocaleForUnauthenticatedUser always has a trigger. Every sample code I found seems to work fluently, what happened to my application?

+6
source share
2 answers

It turned out also more or less the same: Actually, there are no problems, but after logging in there is some redirection, so the profiler never shows the actual request that the interactive login logger starts.

 use Symfony\Component\Security\Http\Event\InteractiveLoginEvent, Symfony\Component\Security\Http\Event\FilterControllerEvent, Symfony\Component\HttpKernel\Event\GetResponseEvent, Symfony\Component\HttpKernel\HttpKernelInterface; use FOS\UserBundle\Entity\User as BaseUser; class LocaleListener { protected $container; protected $availableLocales; public function __construct(\Symfony\Component\DependencyInjection\Container $container, $availableLocales) { $this->container = $container; $this->availableLocales = $availableLocales; } public function onKernelRequest(GetResponseEvent $event) { $request = $event->getRequest(); $locale = $request->getPreferredLanguage($this->availableLocales); $session = $request->getSession(); $token = $this->container->get('security.context')->getToken(); if( is_object( $token ) ) { $user = $token->getUser(); if ($user instanceof BaseUser) { $locale = $user->getLocale(); } } $session = $this->container->get('session'); $session->set('_locale', $locale); $request->setLocale($locale); } } 

In services.yml:

 user.locales.kernel_request_listener: class: Acme\UserBundle\EventListener\LocaleListener arguments: [ @service_container, [ 'en', 'fr', 'ru' ] ] tags: [{ name: kernel.event_listener, event: kernel.request, method: onKernelRequest, priority: -255 }] 
+1
source

same problem here. But the problem is not that the event does not fire. The problem is that after the redirect, the request locale is lost. Using this link:

Changing the language with symfony 2.1

(And based on this )

I figured out how to solve it. In UserListener:

 class UserListener { private $session; public function setSession($session) { $this->session = $session; } public function setLocaleForUnauthenticatedUser(GetResponseEvent $event) { if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) { return; } $request = $event->getRequest(); if ('undefined' == $request->getLocale()) { if($locale = $request->getSession()->get('_locale')) { $request->setLocale($locale); } else { $request->setLocale($request->getPreferredLanguage()); } } } public function setLocaleForAuthenticatedUser(InteractiveLoginEvent $event) { $user = $event->getAuthenticationToken()->getUser(); if ($locale = $user->getLocale()) { // $event->getRequest()->setLocale($user->getLocale()); $this->session->set('_locale', $locale); } } } 

In services.yml you should declare:

 your_listener_name: class: ...\UserBundle\EventListener\UserListener calls: - [ setSession, ['@session'] ] tags: - { name: kernel.event_listener, event: security.interactive_login, method: setLocaleForAuthenticatedUser } 

Hope this helps.

0
source

All Articles