Execute function after User Login Symfony 2.3

青春壹個敷衍的年華 提交于 2019-12-11 02:42:01

问题


I am trying to execute a function after user login in FOSUserBundle, in my config.yml I set the service:

services:
    authentication.success.listener:
        class: MyCompany\MyBundle\EventListener\AuthenticationEventListener
        arguments: [@router]
        tags:
            - { name: kernel.event_subscriber }

Then, I create the Listener class with the methods:

<?php

namespace MyCompany\MyBundle\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\UserEvent;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Cookie as Cookie;

class AuthenticationEventListener implements EventSubscriberInterface
{    
    private $router;

    public function __construct(UrlGeneratorInterface $router)
    {
         $this->router = $router;
    }

    public static function getSubscribedEvents()
    {
        return array(
                FOSUserEvents::SECURITY_IMPLICIT_LOGIN => 'onAuthenticationSuccess',
        );
    }

    public function onAuthenticationSuccess(UserEvent $event)
    {
        //my actions goes here...
    }

}

?>

When I try to login, nothing happens after, I write some wrong code for generate an exception but everything goes well... apparently the function is not being excetuted.

Any help please?


回答1:


Well, finally after few hours searching and enjoying the Juventus victory over Real Madrid (2-1) I found a solution. My solution consist in modify the 'success_handler' in security.yml and create the event, this is the code:

security:
    ..
    firewalls:
        main: 
        ..
        success_handler: authentication.success.listener

Then in services.yml I declare the service:

services:
    authentication.success.listener:
        class: MyCompany\MyBundle\EventListener\AuthenticationEventListener
        arguments: ['@router', '@security.context', '@service_container']

Then I declare the class/function to listen:

// MyCompany\MyBundle\EventListener\AuthenticationEventListener.php

<?php

namespace MyCompany\MyBundle\EventListener;

use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Router;    

class AuthenticationEventListener implements AuthenticationSuccessHandlerInterface
{    
    protected $router;
    protected $security;
    protected $container;
    protected $em;

    public function __construct(Router $router, SecurityContext $security, $container)
    {
        $this->router = $router;
        $this->security = $security;
        $this->container = $container;
        $this->em = $this->container->get('doctrine')->getEntityManager();
    }

    public function onAuthenticationSuccess(Request $request, TokenInterface $token)
    {
        $response = new Response();
        $response = new RedirectResponse('dashboard');
        return $response;
    }

}

?>

I hope it could be useful for you guys...

Thanks anyways!




回答2:


Try this code:

public static function getSubscribedEvents()
{
    return array(
        FOSUserEvents::SECURITY_IMPLICIT_LOGIN => array('onAuthenticationnSuccess',0)
    );
}

This should be the syntax liked documented here: http://symfony.com/doc/current/components/event_dispatcher/introduction.html



来源:https://stackoverflow.com/questions/30054216/execute-function-after-user-login-symfony-2-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!