2

ユーザーが別のユーザーになりすますときにリダイレクトを実行しようとしています。

このために、サービスを登録しました:

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()メソッドが呼び出されるようにするにはどうすればよいですか?

4

1 に答える 1

0

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);
}

多分それはあなたを助けるでしょう。

于 2012-09-17T07:52:02.440 に答える