[英]How to use SwitchUserListener in Symfony2?
I'm trying to do a redirect when a user impersonates another user. 我正在尝试在一个用户模拟另一个用户时进行重定向。
For this I registered a service: 为此,我注册了一项服务:
ACME_listener.security_switch_user:
class: ACME\CustomerLoginBundle\Listener\SecuritySwitchUserListener
arguments: [@service_container, @router, @security.context]
tags:
- { name: kernel.event_listener, event: security.switch_user, method: onSecuritySwitchUser }
My listener class looks like this: 我的侦听器类如下所示:
namespace ACME\CustomerLoginBundle\Listener;
use Symfony\Component\Security\Http\Event\SwitchUserEvent;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\Security\Http\Firewall\ListenerInterface;
class SecuritySwitchUserListener implements ListenerInterface {
public function __construct($appContainer, $router) {
$this->router = $router;
$this->appContainer = $appContainer;
}
public function onSecuritySwitchUser(SwitchUserEvent $event) {
echo "im in here!";
// this does get called
}
public function handle(GetResponseEvent $event) {
echo "but not here :(";
// this does not get called!
}
}
Now the problem is that I can not redirect the user from within the onSecuritySwitchUser
method. 现在的问题是,我无法从onSecuritySwitchUser
方法内重定向用户。 Returning a RedirectResponse
does NOT work and the SwitchUserEvent
does NOT have a setResponse()
method. 返回RedirectResponse
不起作用,并且SwitchUserEvent
没有setResponse()
方法。
What do I have to do so that the handle()
method does get called? 我该怎么做才能使handle()
方法被调用?
I think that handle()
is called from onSecuritySwitchUser()
. 我认为handle()
是从onSecuritySwitchUser()
调用的。 But I can be wrong. 但是我可能是错的。
UPDATE 更新
You can overwrite the event with your own request :) 您可以使用自己的请求覆盖事件:)
Look at: 看着:
Symfony\Component\Security\Http\Firewall\SwitchUserListener
And then Dispach new SwitchUserEvent with overwritten request 然后使用覆盖的请求分发新的SwitchUserEvent
if (null !== $this->dispatcher) {
$switchEvent = new SwitchUserEvent($request, $token->getUser());
$this->dispatcher->dispatch(SecurityEvents::SWITCH_USER, $switchEvent);
}
Maybe that will help you. 也许会对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.