How to redirect the onSecurityInteractiveLogin method to LoginListener - Symfony2

We want to redirect to this or that path after entering the system. We also need to convey the language along the way.

We have a Service Listener as a service to verify this. Our task is to redirect. Code:

<service id="acme.login_listener" class="acme\DemoBundle\Listener\LoginListener"> <tag name="kernel.event_listener" event="security.interactive_login" method="onSecurityInteractiveLogin" priority="-1" /> <tag name="kernel.event_listener" event="filter.response" method="onKernelResponse" /> <argument type="service" id="service_container" /> <argument type="service" id="router" /> </service> 

Listener Code:

 namespace Acme\DemoBundle\Listener; use Symfony\Component\Security\Http\Event\InteractiveLoginEvent; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\Routing\Router; class LoginListener { protected $container; protected $router; public function __construct(ContainerInterface $container, Router $router) { $this->container = $container; $this->router = $router; } public function onSecurityInteractiveLogin(InteractiveLoginEvent $event) { $token = $event->getAuthenticationToken(); $request = $event->getRequest(); $username = $token->getUsername(); $em = $this->container->get('doctrine')->getEntityManager(); $user = $em->getRepository('acmeDemoBundle:User')->findOneByUsername($username); $locale = strtolower($user->getUsuari()->getIdioma()->getCodi6391()); $locale = 'ca'; //force locale test $session = $request->getSession(); $session->setLocale($locale); $session->set('idioma', $locale); $session->set('_locale', $locale); $session->set('locale', $locale); $url = $this->router->generate('onePath', array('_locale' => $locale)); //$event->setResponse(new RedirectResponse($url)); //$event->stopPropagation(); //echo $url;exit; $response = new RedirectResponse($url); return $response; ... 

Our task is to redirect to the desired path in the onSecurityInteractiveLogin function.

We tried to execute $ event-> setResponse, but it does not work. This $ event var does not have this method.

Any idea? Thank you for your help.


Edited: Option Two (January 27, 2012)

Next: http://www.reecefowell.com/2011/10/26/redirecting-on-loginlogout-in-symfony2-using-loginhandlers/

I tried to use handlers. I configured services.xml:

 <service id="company.login_success_handler" class="project\demoBundle\Handler\LoginSucessHandler"> <tag name="monolog.logger" channel="project" /> <argument type="service" id="logger" /> <argument type="service" id="router" /> </service> 

ON security.yml:

 firewalls: main: pattern: /.* form_login: check_path: /login_check login_path: /login success_handler: company.login_success_handler use_forward: false 

And in symfony / src / project / demoBundle / Handler / loginSuccessHandler.php

 <?php namespace project\demoBundle\Handler; use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Core\SecurityContext; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\Routing\Router; class LoginSuccessHandler implements AuthenticationSuccessHandlerInterface { protected $router; protected $security; public function __construct(Router $router, SecurityContext $security) { $this->router = $router; $this->security = $security; echo "hello world";exit; } public function onAuthenticationSuccess(Request $request, TokenInterface $token) { echo "hello world 2";exit; if ($this->security->isGranted('ROLE_SUPER_ADMIN')) { $response = new RedirectResponse($this->router->generate('category_index')); } elseif ($this->security->isGranted('ROLE_ADMIN')) { $response = new RedirectResponse($this->router->generate('category_index')); } elseif ($this->security->isGranted('ROLE_USER')) { // redirect the user to where they were before the login process begun. $referer_url = $request->headers->get('referer'); $response = new RedirectResponse($referer_url); } return $response; } } 

But if I am running a web application, it is not included in this function and does not show "Hello world".

You know what I'm doing badly? Thanks!

+4
source share
3 answers

I found that UrlGeneratorInterface is used in FOSUserBundle

FosUserBundle Connecting to controllers

 use Symfony\Component\Routing\Generator\UrlGeneratorInterface; ... public function __construct(UrlGeneratorInterface $router) { $this->router = $router; } ... ... $url = $this->router->generate('homepage'); 
0
source

Use a handler instead of an event listener if you need to change the response. Further information here: http://www.reecefowell.com/2011/10/26/redirecting-on-loginlogout-in-symfony2-using-loginhandlers/

+3
source

This may be another solution:

https://gist.github.com/snc/1312769

+3
source

Source: https://habr.com/ru/post/1392469/


All Articles