kikwik/double-opt-in-bundle

Doctrine 2 实体的双确认管理

安装: 426

依赖: 0

建议者: 0

安全: 0

星星: 0

关注者: 2

分支: 0

开放问题: 0

类型:symfony-bundle

v1.1.0 2023-04-30 14:44 UTC

This package is auto-updated.

Last update: 2024-09-30 01:54:21 UTC


README

在 symfony 4.4 和 5.x 中为 Doctrine 2 实体提供双确认管理

安装

打开命令控制台,进入您的项目目录并执行以下命令以下载此扩展包的最新稳定版本

$ composer require kikwik/double-opt-in-bundle

配置

在您的 .env 文件中配置 symfony/mailer 组件

MAILER_DSN=sendmail+smtp://

config/routes.yaml 中导入双确认检查路由

kikwik_double_opt_in_bundle:
  resource: '@KikwikDoubleOptInBundle/Resources/config/routes.xml'
  prefix: '/double-opt-in'

创建 config/packages/kikwik_double_opt_in.yaml 配置文件,设置 sender_email 参数

kikwik_double_opt_in:
    sender_email: '%env(SENDER_EMAIL)%'
    sender_name: '%env(SENDER_NAME)%'
    remove_secret_code_after_verification: true

并在您的 .env 文件中定义它

SENDER_EMAIL=no-reply@example.com
SENDER_NAME="My Company Name"

在您的类中实现 DoubleOptInInterface 或使用 DoubleOptInTrait

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Kikwik\DoubleOptInBundle\Model\DoubleOptInInterface;
use Kikwik\DoubleOptInBundle\Model\DoubleOptInTrait;
use Symfony\Component\Security\Core\User\UserInterface;

/**
 * @ORM\Entity(repositoryClass=UserRepository::class)
 * @ORM\Table(name="`user`")
 */
class User implements UserInterface, DoubleOptInInterface
{
    use DoubleOptInTrait;

    //...
}

创建迁移并更新您的数据库

$ php bin/console make:migration
$ php bin/console doctrine:migrations:migrate

使用方法

每次持久化一个实现了 DoubleOptInInterface 的新实体时,都会发送一封确认电子邮件。如果您想禁用确认电子邮件(在导入数据时很有用),请在持久化实体之前调用 disableDoubleOptIn

$user = new User();
$user->setEmail('test@example.com');
$user->setPassword('secret');

$user->disableDoubleOptIn();

$em->persist($user);
$em->flush();

要重新发送确认电子邮件,自动装配 DoubleOptInMailManager 服务并调用 sendEmail(请确保 $doubleOptInSecretCode 字段不为空)

/**
 * @Route("/resend-email/{id}", name="app_resend_email")
 */
public function resendConfirmationEmail(User $user, DoubleOptInMailManager $doubleOptInMailManager)
{
    if($user->getDoubleOptInSecretCode())
    {
        $doubleOptInMailManager->sendEmail($user);
    }
    return $this->redirectToRoute('app_home');
}

定制

vendor/kikwik/double-opt-in-bundle/src/Resources/translations/KikwikDoubleOptInBundle.xx.yaml 复制翻译文件到 translations/KikwikDoubleOptInBundle.xx.yaml 并在此处进行更改。

double_opt_in:
    title: Verifica email
    success: La tua email è stata verificata con successo
    danger: Il codice di verifica non è stato trovato, forse è già stato usato?
    email:
        subject: 'Conferma la tua email'
        content: |
            <p>
                <a href="{{ confirm_url }}">Clicca qui per confermare la tua email</a><br/>
                oppure incolla in seguente link nella barra degli indirizzi del browser: <br/>{{ confirm_url }}
            </p>

事件监听器

在双确认确认成功后,该扩展包将触发 DoubleOptInVerifiedEvent 自定义事件,因此您可以监听它并进行操作。

// src/EventSubscriber/AfterDoubleOptInEventSubscriber.php
namespace App\EventSubscriber;

use Kikwik\DoubleOptInBundle\Event\DoubleOptInVerifiedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class AfterDoubleOptInEventSubscriber implements EventSubscriberInterface
{
    public function onDoubleOptInVerified(DoubleOptInVerifiedEvent $event)
    {
        $object = $event->getObject();

        // Do something with the $object, which is verified
    }

    public static function getSubscribedEvents()
    {
        return [
            'kikwik.double_opt_in.verified' => 'onDoubleOptInVerified',
        ];
    }
}