簡體   English   中英

如何在Symfony2中使用SwitchUserListener?

[英]How to use SwitchUserListener in Symfony2?

我正在嘗試在一個用戶模擬另一個用戶時進行重定向。

為此,我注冊了一項服務:

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 }

我的偵聽器類如下所示:

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!
    }

}

現在的問題是,我無法從onSecuritySwitchUser方法內重定向用戶。 返回RedirectResponse不起作用,並且SwitchUserEvent沒有setResponse()方法。

我該怎么做才能使handle()方法被調用?

我認為handle()是從onSecuritySwitchUser()調用的。 但是我可能是錯的。

更新

您可以使用自己的請求覆蓋事件:)

看着:

Symfony\Component\Security\Http\Firewall\SwitchUserListener

然后使用覆蓋的請求分發新的SwitchUserEvent

if (null !== $this->dispatcher) {
        $switchEvent = new SwitchUserEvent($request, $token->getUser());
        $this->dispatcher->dispatch(SecurityEvents::SWITCH_USER, $switchEvent);
}

也許會對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM