在Symfony的FOSUserbundle中注册后禁用自动登录

dev*_*ev0 6 php symfony fosuserbundle

使用最新的Symfony和FOSUserbundle,成功注册新用户后,用户将自动登录.我想阻止这种情况.我的理由是只有特殊用户才能注册新用户.

我想我必须覆盖bundle的RegisterController中的registerAction,但我不知道如何.

我试过:http://symfony.com/doc/current/bundles/FOSUserBundle/overriding_controllers.html,但似乎已经过时了,没有用这种方法创建用户.

任何提示都表示赞赏.

编辑:

我发现我没有正确创建子包.我还必须创建自己的EventListener.当我覆盖FOSUserEvents::REGISTRATION_SUCCESS事件时,它现在有效.

奇怪的是,当我使用该FOSUserEvents::REGISTRATION_COMPLETED事件时,会调度这两个事件,我的捆绑包和FOSUserbundle,以便用户被重定向到正确的站点,但以新用户身份登录.

编辑2:

所以这是我的倾听者:

public static function getSubscribedEvents()
{
    return array(
        FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess',
        FOSUserEvents::REGISTRATION_COMPLETED => 'onRegistrationCompleted',
    );
}

public function onRegistrationSuccess(FormEvent  $event)
{
    $url = $this->router->generate('admin');

    $event->setResponse(new RedirectResponse($url));
}

public function onRegistrationCompleted(FilterUserResponseEvent  $event)
{

}
Run Code Online (Sandbox Code Playgroud)

我在REGISTRATION_SUCCESS事件中设置重定向并且REGISTRATION_COMPLETED为空.使用调试器,我可以验证我自己的侦听器的事件是否被调用,但是也调用了原始事件.

FZE*_*FZE 1

您可以使用侦听器解决此问题,在 fos 用户捆绑包中,它会在注册后对用户进行身份验证。

文件 :friendsofsymfony/user-bundle/EventListener/AuthenticationListener.php

班级 :FOS\UserBundle\EventListener\AuthenticationListener

如果你检查这个类,你会看到它跟踪REGISTRATION_COMPLETED事件。

它在Authenticatiton Listener触发函数后调度事件logInUser。因此,您必须在侦听器中注销订阅“注册已完成”的用户。

您可以检查https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/controller_events.rst以编写侦听器以注销用户。

注意:在每个注册过程中登录注销用户可能不是一个好方法,但是如果您使用fosuserbundle,最简单的方法和最小的占用空间就是这样,如果已经有一个yml配置不存在,实际上在代码中yml conf 没有方向。所以这种方法是最小的。脚印。

    try {
        $this->loginManager->logInUser($this->firewallName, $event->getUser(), $event->getResponse());

        $eventDispatcher->dispatch(FOSUserEvents::SECURITY_IMPLICIT_LOGIN, new UserEvent($event->getUser(), $event->getRequest()));
    } catch (AccountStatusException $ex) {
        // We simply do not authenticate users which do not pass the user
        // checker (not enabled, expired, etc.).
    }
Run Code Online (Sandbox Code Playgroud)