fosuserbundle在create user上设置确认令牌

时间:2017-05-18 07:29:45

标签: symfony fosuserbundle

当我使用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);

这是正常的吗?

1 个答案:

答案 0 :(得分:1)

确认令牌是使用事件REGISTRATION_SUCCESS创建的,当此事件发送时,事件监听器应提供令牌并发送电子邮件以允许创建的用户确认其地址,

在您的情况下,应配置fosUser的电子邮件配置。结束然后确认令牌和电子邮件将正常运行。

看那里:https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/EventListener/EmailConfirmationListener.php

/**
 * @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));
}