我将 FOSUserBundle 与 NnmMultiUserBundle 一起使用。当用户注册时,使用我的 form_handler,用户实体被创建,并且应该自动登录这个新用户。
我无法使自动登录工作。
我的控制器:
public function registerAction()
{
$discriminator = $this->container->get('nmn_user_discriminator');
$discriminator->setClass('Travelyo\UserBundle\Entity\UserFrontend');
$form = $discriminator->getRegistrationForm();
//$form = $this->container->get('fos_user.registration.form');
$formHandler = $this->container->get('fos_user.registration.form.handler');
$confirmationEnabled = $this->container->getParameter('fos_user.registration.confirmation.enabled');
$process = $formHandler->process($confirmationEnabled, array(User::ROLE_CUSTOMER));
if ($process) {
$user = $form->getData();
if ($confirmationEnabled) {
$this->container->get('session')->set('fos_user_send_confirmation_email/email', $user->getEmail());
$route = 'fos_frontend_registration_check_email';
} else {
$this->authenticateUser($user);
$route = 'fos_frontend_registration_confirmed';
}
$this->setFlash('fos_user_success', 'registration.flash.user_created');
$url = $this->container->get('router')->generate($route);
return new RedirectResponse($url);
}
return $this->container->get('templating')->renderResponse('TravelyoUserBundle:Registration:user_frontend.form.html.' . $this->getEngine(), array('form' => $form->createView(),));
}
鉴定方法:
protected function authenticateUser(UserInterface $user)
{
try {
$this->container->get('fos_user.user_checker')->checkPostAuth($user);
} catch (AccountStatusException $e) {
// Don't authenticate locked, disabled or expired users
return;
}
$providerKey = $this->container->getParameter('fos_user.firewall_name');
$token = new UsernamePasswordToken($user, null, $providerKey, $user->getRoles());
$this->container->get('security.context')->setToken($token);
}
调用 athenticate 后,控制器重定向到 ConfirmedAction,它会尝试获取“登录”用户:
public function confirmedAction()
{
$user = $this->container->get('security.context')->getToken()->getUser();
if (!is_object($user) || !$user instanceof UserInterface) {
throw new AccessDeniedException('This user does not have access to this section.');
}
return $this->container->get('templating')->renderResponse('FOSUserBundle:Registration:confirmed.html.'.$this->getEngine(), array(
'user' => $user,
));
}
错误信息:
致命错误:在第 115 行的 /Data/Web/Local/travelyo/vendor/bundles/FOS/UserBundle/Controller/RegistrationController.php 中的非对象上调用成员函数 getUser()
如果我然后尝试使用登录表单登录它可以正常工作(这意味着用户的创建正在工作)。但我不明白为什么不能从 security.context 中检索到这个令牌。