正如我所看到的,裏面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();
}
}
你已經擁有'$ user',你不需要使用'persist'。 「沖洗」就足夠了。 –
這就是我正在尋找的。謝謝! –