2017-08-02 170 views
1

有關堆棧的問題很多,但說實話 - 沒有人爲我工作(我正在使用Symfony 3.3和FOSUserBundle 2.x)。註冊後禁用用戶,並允許管理員確認

我需要什麼才達到:

  • 用戶註冊成功,在我的網頁
  • 他的賬戶被自動設置爲默認值(不發送電子郵件)
  • 管理員可以激活/啓用該帳戶從他管理面板

我已經創建了一個事件監聽,但它不會工作..我甚至無法處理用戶對象改變enabled狀態...

#config.yml 
fos_user: 
    db_driver: orm # other valid values are 'mongodb' and 'couchdb' 
    firewall_name: main 
    user_class: AppBundle\Entity\User 
    from_email: 
     address: "%mailer_user%" 
     sender_name: "%mailer_user%" 
    registration: 
     form: 
      type: AppBundle\Form\RegistrationType 

和事件監聽器:

class RegistrationListener implements EventSubscriberInterface 
{ 
    /** 
    * @var RouterInterface 
    */ 
    private $router; 

    private $em; 

    public function __construct(RouterInterface $route, UserManagerInterface $em) 
    { 
     $this->router = $route; 
     $this->em = $em; 
    } 

    /** 
    * {@inheritDoc} 
    */ 
    public static function getSubscribedEvents() 
    { 
     return array(
      FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess', 
      FOSUserEvents::REGISTRATION_CONFIRMED=> ['onRegistrationConfirmed', 999], 
      FOSUserEvents::REGISTRATION_FAILURE => 'onRegistrationFailure' 
     ); 
    } 

    public function onRegistrationSuccess(FormEvent $event) 
    { 
     $url = $this->router->generate('app.registration.succes'); 
     $response = new JsonResponse(['redirectUrl' => $url]); 
     $event->setResponse($response); 
    } 

    public function onRegistrationFailure(FormEvent $event) 
    { 
     $array = array('success' => false, 'message' => (string)$event->getForm()->getErrors(true, true)); 
     $response = new JsonResponse($array, Response::HTTP_BAD_REQUEST); 
     $event->setResponse($response); 
    } 

    public function onRegistrationConfirmed(FilterUserResponseEvent $event) 
    { 
     /** 
     * this one is no working.. after registration account is enabled anyway... 
     */ 
     $user = $event->getUser(); 
     $user->setEnabled(false); 
     $user->setSalt('test'); 
     $this->em->updateUser($user); 
    } 
} 

回答

2

明白了!

只需撥打FOSUserEvents::REGISTRATION_SUCCESS即可,因爲FOSUserEvents::REGISTRATION_CONFIRMED只有在用戶點擊他的電子郵件上的激活鏈接時纔會觸發,而我並未發送給他。

class RegistrationListener implements EventSubscriberInterface 
{ 
    /** 
    * @var RouterInterface 
    */ 
    private $router; 

    private $em; 

    public function __construct(RouterInterface $route, UserManagerInterface $em) 
    { 
     $this->router = $route; 
     $this->em = $em; 
    } 

    /** 
    * {@inheritDoc} 
    */ 
    public static function getSubscribedEvents() 
    { 
     return array(
      FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess', 
      FOSUserEvents::REGISTRATION_FAILURE => 'onRegistrationFailure' 
     ); 
    } 

    public function onRegistrationSuccess(FormEvent $event) 
    { 
     $user = $event->getForm()->getData(); 
     $user->setEnabled(false); 
     $this->em->updateUser($user); 

     $url = $this->router->generate('app.registration.succes'); 
     $response = new JsonResponse(['redirectUrl' => $url]); 
     $event->setResponse($response); 
    } 

    public function onRegistrationFailure(FormEvent $event) 
    { 
     $array = array('success' => false, 'message' => (string)$event->getForm()->getErrors(true, true)); 
     $response = new JsonResponse($array, Response::HTTP_BAD_REQUEST); 
     $event->setResponse($response); 
    } 
} 
相關問題