2017-07-03 70 views
1

我希望用戶註冊,確認它是電子郵件,但是由管理員手動激活FOSUserBundle:如何允許註冊,確認但不激活?

感謝this頁面我找到了FOSUserEvents::REGISTRATION_CONFIRMED這是在點擊電子郵件中的確認鏈接後調用的。

現在我想禁用該帳戶(見下文)。

class RegistrationListener implements EventSubscriberInterface 
{ 
    public static function getSubscribedEvents() 
    { 
     return array(   
      FOSUserEvents::REGISTRATION_CONFIRMED => 'onRegistrationCompleted' 
     ); 
    } 

    public function onRegistrationCompleted(UserEvent $event) { 
     // registration completed 
     // TODO: disable the user. How? 
    } 
} 

或者有沒有我錯過的configuration

任何想法?

在此先感謝!

回答

2

正如我所看到的,裏面FOS\UserBundle\Controller\RegistrationController:: confirmAction()用戶啓用:

/** 
* Receive the confirmation token from user email provider, login the user. 
* 
* @param Request $request 
* @param string $token 
* 
* @return Response 
*/ 
public function confirmAction(Request $request, $token) 
{ 
    /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */ 
    $userManager = $this->get('fos_user.user_manager'); 

    ... 

    $user->setConfirmationToken(null); 
    $user->setEnabled(true); 

    $event = new GetResponseUserEvent($user, $request); 
    $dispatcher->dispatch(FOSUserEvents::REGISTRATION_CONFIRM, $event); 

    $userManager->updateUser($user); 
    ... 

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

    return $response; 
} 

我能想到的兩件事情可以做,以禁用它。

1)編寫事件偵聽器,這將在FOSUserEvents::REGISTRATION_CONFIRMED反應並禁用用戶=>http://symfony.com/doc/master/bundles/FOSUserBundle/controller_events.html

2)覆蓋RegistrationController =>https://symfony.com/doc/current/bundles/FOSUserBundle/overriding_controllers.html

我更喜歡第一個選項。

class RegistrationListener implements EventSubscriberInterface 
{ 
    /** @var EntityManager */ 
    private $em; 


    /** 
    * @param EntityManager $em 
    */ 
    public function __construct(EntityManager $em) 
    { 
     $this->em = $em; 
    } 

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

    public function onRegistrationCompleted(UserEvent $event) { 
     // registration completed 
     // TODO: disable the user. How? 
     $user = $event->getUser(); 
     $user->setEnabled(false); 

     $this->em->persist($user); 
     $this->em->flush(); 
    } 
} 
+0

你已經擁有'$ user',你不需要使用'persist'。 「沖洗」就足夠了。 –

+0

這就是我正在尋找的。謝謝! –