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';
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')) {
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!