Symfony2, FOSUser, 重置密码后不能登录用户[英] Symfony2, FOSUser, don't login user after reset password

本文是小编为大家收集整理的关于Symfony2, FOSUser, 重置密码后不能登录用户的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在使用fosuser重置密码系统,我不知道用户更改密码后如何删除自动蛋白?

感谢您的帮助,我不想覆盖,我直接更改FOS文件.

推荐答案

正确的方式:)

编译器通过Symfony允许您操纵其他服务.在这种情况下,您要覆盖fos_user.listener.authentication使用自定义订阅者,而不是提供fosuserbundle FOS\UserBundle\EventListener\AuthenticationListener的订户.您可以将提供的一个扩展到仅订阅注册事件,而不是重置密码事件:

<?php
// src/YourCompany/YourBundle/EventListener/AuthenticationListener.php

namespace YourCompany\YourBundle\EventListener;

use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\EventListener\AuthenticationListener as FOSAuthenticationListener;

class AuthenticationListener extends FOSAuthenticationListener
{

    public static function getSubscribedEvents()
    {
        return array(
            FOSUserEvents::REGISTRATION_COMPLETED => 'authenticate',
            FOSUserEvents::REGISTRATION_CONFIRMED => 'authenticate'
        );
    }

}

这样做只定义这样的编译器通行证:

<?php
// src/YourCompany/YourBundle/DependencyInjection/Compiler/FOSUserOverridePass.php

namespace YourCompany\YourBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class FOSUserOverridePass implements CompilerPassInterface
{

    public function process(ContainerBuilder $container)
    {
        $container->getDefinition('fos_user.listener.authentication')->setClass('YourCompany\YourBundle\EventListener\AuthenticationListener');
    }

}

,然后在捆绑包中注册您的编译器通过:

<?php
// src/YourCompany/YourBundle/YourCompanyYourBundle.php

namespace YourCompany\YourBundle;

use YourCompany\YourBundle\Compiler\FOSUserOverridePass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

class YourCompanyYourBundle extends Bundle
{

    public function build(ContainerBuilder $container)
    {
        parent::build($container);
        $container->addCompilerPass(new FOSUserOverridePass());
    }

}

就是这样!


以下是要阅读的东西:/cookbook/service_container/compiler_passes.html

您要覆盖的服务:

和原始类:/fosuserbundle/blob/master/eventlistener/authenticationlistener.php

其他推荐答案

如果要覆盖捆绑包,则必须在\ vendor \ friendsofsymfony \ user-bundle \ fos \ fos \ userbundle \ userbundle \ resettingconterloller.php:

中评论第132行.
$dispatcher->dispatch(FOSUserEvents::RESETTING_RESET_COMPLETED, new FilterUserResponseEvent($user, $request, $response));

禁用自动login.但是正如Bartek所说,这不是做到这一点的好方法. 虽然只有一行... 我希望它能帮助您,如我所关注,我通过在线上的过载文件在重新计算中的评论相同:

$dispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user, $request, $response));

注册后禁用自动蛋白.

其他推荐答案

最后,我想我找到了一种轻巧但干净的方法:可以停止事件! 检查 http://symfony.com/symfony.com/doc/doc/current/current/current/current/current/components/component/componet/event/event/event_dispatcher/introdcluction. html 在停止事件流/传播. 注册后的事件进行登录似乎是注册_Complet的,因此请对此事件进行订阅,并具有很高的优先级

...
public static function getSubscribedEvents() {
    return array(
        FOSUserEvents::REGISTRATION_COMPLETED => ['onRegistrationCompleted',9999],
    );
}
public function onRegistrationCompleted(FilterUserResponseEvent $event) {
    // this trick prevent login now
    $event->stopPropagation();
}

那么,如果您担心一些重要的听众/订阅者会放松事件,请调整听力优先级...

本文地址:https://www.itbaoku.cn/post/2090778.html

问题描述

I'm working on resetting password system with FosUser, and I don't know how can I remove the auto-login after user changes his password ?

Thanks for help, I don't want to override, I change directly FOS files.

推荐答案

The proper way :)

Compiler Passes in Symfony allow you to manipulate other services. In this case you want to override fos_user.listener.authentication to use your custom subscriber instead of the one provided with FOSUserBundle FOS\UserBundle\EventListener\AuthenticationListener. You can just extends the provided one to subscribe only to the registration events and NOT to the resetting password event:

<?php
// src/YourCompany/YourBundle/EventListener/AuthenticationListener.php

namespace YourCompany\YourBundle\EventListener;

use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\EventListener\AuthenticationListener as FOSAuthenticationListener;

class AuthenticationListener extends FOSAuthenticationListener
{

    public static function getSubscribedEvents()
    {
        return array(
            FOSUserEvents::REGISTRATION_COMPLETED => 'authenticate',
            FOSUserEvents::REGISTRATION_CONFIRMED => 'authenticate'
        );
    }

}

To do so just define a Compiler Pass like this:

<?php
// src/YourCompany/YourBundle/DependencyInjection/Compiler/FOSUserOverridePass.php

namespace YourCompany\YourBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class FOSUserOverridePass implements CompilerPassInterface
{

    public function process(ContainerBuilder $container)
    {
        $container->getDefinition('fos_user.listener.authentication')->setClass('YourCompany\YourBundle\EventListener\AuthenticationListener');
    }

}

And then register your compiler pass in your bundle definition:

<?php
// src/YourCompany/YourBundle/YourCompanyYourBundle.php

namespace YourCompany\YourBundle;

use YourCompany\YourBundle\Compiler\FOSUserOverridePass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

class YourCompanyYourBundle extends Bundle
{

    public function build(ContainerBuilder $container)
    {
        parent::build($container);
        $container->addCompilerPass(new FOSUserOverridePass());
    }

}

That's it!


Here's something to read: http://symfony.com/doc/current/cookbook/service_container/compiler_passes.html

The service you are going to override: https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/config/listeners.xml

And the original class: https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/EventListener/AuthenticationListener.php

其他推荐答案

If you want to override the bundle, you will have to comment the line 132 in \vendor\friendsofsymfony\user-bundle\FOS\UserBundle\Controller\ResettingController.php :

$dispatcher->dispatch(FOSUserEvents::RESETTING_RESET_COMPLETED, new FilterUserResponseEvent($user, $request, $response));

which disable the auto-login. But as bartek says, it is not a good way to do that. Although it is only for one line... I hope it will help you, as I am concerned, I have comment a same line in the regitration by an overloading file on line :

$dispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user, $request, $response));

To disable the auto-login after the registration.

其他推荐答案

Finally I think I found a light but clean way of doing this : events can be stopped ! check http://symfony.com/doc/current/components/event_dispatcher/introduction.html at Stopping Event Flow/Propagation. The event making login after registration seems to be REGISTRATION_COMPLETED, so make a subscriber to this event with a high priority

...
public static function getSubscribedEvents() {
    return array(
        FOSUserEvents::REGISTRATION_COMPLETED => ['onRegistrationCompleted',9999],
    );
}
public function onRegistrationCompleted(FilterUserResponseEvent $event) {
    // this trick prevent login now
    $event->stopPropagation();
}

Then if you fear that some important listener/subscribers loose the event, adjust the listen priority...