当我使用fosuserbundle在我自己的控制器上创建用户时,我的用户没有确认令牌(在数据库中)。我怎么能创造这个?
$userManager = $this->get('fos_user.user_manager');
$userCreate = $userManager->createUser();
$userCreate->setUsername($user->getUsername());
$userCreate->setEmail($user->getEmail());
$userCreate->setEmailCanonical($user->getEmail());
$userCreate->setPlainPassword($user->getPassword());
$userCreate->setEnabled(true);
$userManager->updateUser($userCreate, true);
这是正常的吗?
答案 0 :(得分:1)
确认令牌是使用事件REGISTRATION_SUCCESS创建的,当此事件发送时,事件监听器应提供令牌并发送电子邮件以允许创建的用户确认其地址,
在您的情况下,应配置fosUser的电子邮件配置。结束然后确认令牌和电子邮件将正常运行。
/**
* @param FormEvent $event
*/
public function onRegistrationSuccess(FormEvent $event)
{
/** @var $user \FOS\UserBundle\Model\UserInterface */
$user = $event->getForm()->getData();
$user->setEnabled(false);
if (null === $user->getConfirmationToken()) {
$user->setConfirmationToken($this->tokenGenerator->generateToken());
}
$this->mailer->sendConfirmationEmailMessage($user);
$this->session->set('fos_user_send_confirmation_email/email', $user->getEmail());
$url = $this->router->generate('fos_user_registration_check_email');
$event->setResponse(new RedirectResponse($url));
}