2013-07-10 40 views
5

Tôi đang cố gắng để thực hiện các sự kiện trên FOSUserBundleSự kiện FOSUserBundle Symfony2 về đăng ký và đăng nhập

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

Và dịch vụ của tôi

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'} 

Nhưng tôi có vấn đề. Khi tôi không đăng nhập gì, số điện thoại getSubscribedEvents() được gọi nhưng không phải onSecurityImplicitLogin(UserEvent $event)

Và vấn đề khác là đăng ký. lỗi sau xảy ra trên 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 

và nếu tôi nhận xét dòng này, tôi đã cùng một lỗi trên onRegistrationCompleted(FilterUserResponseEvent $event)

Sửa

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' } 

Trả lời

7

Hai điểm:

  1. Trong dịch vụ Định nghĩa - { name: 'kernel.event_subscriber'} là đủ, không có mục nhập sự kiện. Đây là sự khác biệt giữa người đăng ký và người nghe. Người nghe chỉ có thể nghe một sự kiện, xác định các thông số eventmethod trong định nghĩa dịch vụ. Một thuê bao có thể nghe nhiều sự kiện, xác định máng phương pháp getSubscribedEvents(), vì vậy không cần thêm thông số nào trong định nghĩa dịch vụ.

  2. Điểm thứ hai, bạn nhập gợi ý vào lớp sự kiện (UserEventFilterUserResponseEvent), nhưng bạn không bao giờ nhập chúng, vì vậy PHP giả định chúng trong cùng một không gian tên (như đã nói trong thông báo lỗi).

    use FOS\UserBundle\Event\UserEvent; 
    use FOS\UserBundle\Event\FilterUserResponseEvent; 
    
+0

Cám ơn câu trả lời của bạn, nhưng tôi thêm - {name: 'kernel.event_listener', sự kiện: 'security.interactive_login'} phục vụ của tôi để phát hiện đăng nhập bình thường – Ajouve

+0

Liệu trộn 'kernel.event_listener' và thẻ 'kernel.event_subscriber' hoạt động? –

+1

Đúng vậy, tôi chỉnh sửa bài đăng của mình bằng dịch vụ mới – Ajouve

Các vấn đề liên quan