通过邮箱symfony3验证用户帐户

时间:2017-05-22 08:24:53

标签: php symfony

我想在会员点击他的邮箱中的链接时使用symfony3进行注册电子邮件验证...而不使用FOSuserBundle

我在我的用户实体中添加了2个字段,一个布尔$ validMail属性和一个字符串code_validation,以便在每次注册时生成一个随机数。

默认情况下,我将$ validMail初始化为false,我在每个注册的实体用户中生成一个随机代码,我想检测用户验证电子邮件的时间。

这是我的服务:

namespace AppBundle\Services;

use AppBundle\Entity\User;
use AppBundle\Form\InscriptionType;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Form\FormFactory;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoder;
use Symfony\Component\Security\Core\Tests\Encoder\PasswordEncoder;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;


class Securite implements  ContainerAwareInterface
{

    private $doctrine;
    private $form;
    private $session;
    private $authenticationUtils;


    /**
     * @var ContainerInterface
     */
    private $container;

    /**
     * @var PasswordEncoder
     */
    private $passwordEncoder;


    /**
     * @var \Swift_Mailer
     */
    private $mailer;
    private $twig;

    public function setContainer(ContainerInterface $container = null)
    {
        $this->container = $container;
    }

    public function __construct(EntityManager $doctrine, Session $session, FormFactory $form, AuthenticationUtils $authenticationUtils, UserPasswordEncoder $passwordEncoder, \Swift_Mailer $mailer, \Twig_Environment $twig)
    {
        $this->doctrine = $doctrine;
        $this->session = $session;
        $this->form = $form;
        $this->authenticationUtils = $authenticationUtils;
        $this->passwordEncoder = $passwordEncoder;
        $this->mailer = $mailer;
        $this->twig = $twig;
    }


    public function register(Request $request)
    {
        $inscription = new User();

        $form = $this->form->create(InscriptionType::class, $inscription);

        if ($request->isMethod('POST') && $form->handleRequest($request)->isValid())
        {

            $data = $form->getData();
            $this->session->set('inscription', $data);

            $inscription->setValidMail(false);

            $password = $inscription->getPassword();

            $this->cryptPassword($inscription, $password);

            $this->doctrine->persist($inscription);
            $this->doctrine->flush();
            $this->session->getFlashBag()->add("success", "inscription confirmée, veuillez confirmer votre email");

            $this->isValidMail();

        }
        return $form;
    }


    public function cryptPassword(User $user, $plainPassword)
    {
        $encoder = $this->passwordEncoder;

        $encoded = $encoder->encodePassword($user, $plainPassword);

        $user->setPassword($encoded);
    }


    public function isValidMail()
    {
        $inscription = $this->session->get('inscription');

        if ($inscription) {
            $message = \Swift_Message::newInstance()
                ->setCharset('UTF-8')
                ->setSubject('Veuillez confirmer votre inscription')
                ->setBody($this->twig->render('back/email_inscription.html.twig', array(
                    'username'        => $inscription->getUsername(),
                    'password'         => $inscription->getPassword(),
                    'code_validation' => $inscription->getCodeValidation(),
                )))
                ->setContentType('text/html')
                ->setFrom('test@gmail.com')
                ->setTo($inscription->getUsername());

            $this->mailer->send($message);
        }
    }
}

我不知道如何检测用户何时点击其邮箱中的链接,刷新页面...我尝试按照教程但看起来太复杂了。什么是最简单的方法?

2 个答案:

答案 0 :(得分:0)

为什么不在请求中传递令牌($ _GET参数)。例如url:

www.blah.com/confirm_register?token=asdawddazvaefas

然后拿走那个标记:

$token = $request->attributes->get('token');

然后检查数据库中是否存在该令牌(如果存在)将用户字段$ validEmail更新为true。

答案 1 :(得分:0)

验证链接必须包含code_validation作为URL参数。然后,您为电子邮件验证创建新路线。

当用户点击邮箱中的链接时,他会使用参数访问电子邮件验证路径。然后验证GITS参数,如Elimsas所说。