5

我正在尝试在 FOSUserBundle 上实现事件

<?php
namespace EasyApp\UserBundle\Service;

use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Doctrine\Bundle\DoctrineBundle\Registry as Doctrine; 
use EasyApp\UserBundle\Entity\UserLogin;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\FormEvent;
use FOS\UserBundle\FOSUserBundle;


class LoginManager implements EventSubscriberInterface
{
    /** @var \Symfony\Component\Security\Core\SecurityContext */
    private $securityContext;

    /** @var \Doctrine\ORM\EntityManager */
    private $em;

    /**
     * Constructor
     *
     * @param SecurityContext $securityContext
     * @param Doctrine        $doctrine
     */
    public function __construct(SecurityContext $securityContext, Doctrine $doctrine)
    {
        $this->securityContext = $securityContext;
        $this->em = $doctrine->getEntityManager();
    }

    /**
     * {@inheritDoc}
     */
    public static function getSubscribedEvents()
    {
        return array(
                FOSUserEvents::SECURITY_IMPLICIT_LOGIN => 'onSecurityImplicitLogin',
                FOSUserEvents::REGISTRATION_COMPLETED=> 'onRegistrationCompleted'
        );
    }

    public function onSecurityImplicitLogin(UserEvent $event)
    {
        die;
        $user = $event->getAuthenticationToken()->getUser();
        $request = $event->getRequest();

        if ($this->securityContext->isGranted('IS_AUTHENTICATED_FULLY')) {
            // user has just logged in
            $this->saveLogin($request, $user);
        }
    }

    public function onRegistrationCompleted(FilterUserResponseEvent $event){
        $user = $event->getAuthenticationToken()->getUser();
        $request = $event->getRequest();
        saveLogin($request, $user);
    }

    public function saveLogin($request, $user){
        $login = new UserLogin();
        $login->setIp($request->getClientIp());
        $login->setUser($user);

        $this->em->persist($login);
        $this->em->flush();
    }
}

还有我的服务

services:
    user_login_manager:
        class: 'EasyApp\UserBundle\Service\LoginManager'
        arguments: ['@security.context', '@doctrine']
        tags:
            - { name: 'kernel.event_subscriber', event: 'fos_user.security.interactive_login'}
            - { name: 'kernel.event_subscriber', event: 'fos_user.registration.completed'}

但我有问题。当我登录时没有任何反应,getSubscribedEvents()被调用但不是onSecurityImplicitLogin(UserEvent $event)

另一个问题是注册。以下错误发生在onSecurityImplicitLogin(UserEvent $event)

Catchable Fatal Error: Argument 1 passed to EasyApp\UserBundle\Service\LoginManager::onSecurityImplicitLogin() must be an instance of EasyApp\UserBundle\Service\UserEvent, instance of FOS\UserBundle\Event\UserEvent given in /Users/antoine/Documents/projects/easyApp/application/src/EasyApp/UserBundle/Service/LoginManager.php line 46

如果我评论这一行,我会遇到同样的错误 onRegistrationCompleted(FilterUserResponseEvent $event)

编辑

services:
    user_login_manager:
        class: 'EasyApp\UserBundle\Service\LoginManager'
        arguments: ['@security.context', '@doctrine']
        tags:
            - { name: 'kernel.event_subscriber'}
            - { name: 'kernel.event_listener', event: 'security.interactive_login' }
4

1 回答 1

7

两点:

  1. 在服务定义- { name: 'kernel.event_subscriber'}中就足够了,没有事件入口。这就是订阅者和听众之间的区别。一个监听器只能监听一个事件,在服务定义中定义了 trougheventmethodparams。订阅者可以侦听通过方法定义的多个事件,getSubscribedEvents()因此不需要服务定义中的其他参数。

  2. 第二点,您正在对事件类(UserEventFilterUserResponseEvent)进行类型提示,但您从未导入它们,因此 PHP 假定它们位于相同的命名空间中(如错误消息中所述)。

    use FOS\UserBundle\Event\UserEvent;
    use FOS\UserBundle\Event\FilterUserResponseEvent;
    
于 2013-07-10T09:11:16.723 回答