eltharin/invitations

symfony 的 invitations 套件

安装次数: 30

依赖关系: 0

建议者: 0

安全: 0

星星: 1

关注者: 1

分支: 0

开放问题: 0

类型:symfony-bundle

V1.4.2 2024-05-12 08:38 UTC

This package is auto-updated.

Last update: 2024-09-12 09:24:58 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

安装

  • 使用 composer 安装此套件
composer require eltharin/invitaitons

php bin/console make:migration
php bin/console d:m:m 

to create invitaitons table

什么是 Invitations 套件?

此套件将帮助您处理链式邮件动作。

当您需要发送邮件以允许某个动作时,此套件将为您提供帮助。

如何操作?

例如,我们将设置忘记密码的工作流程

  • 用户访问页面 /forget-password
  • 输入他的邮箱
  • 点击邮件中的链接接收邮件
  • 设置新密码
  • 享受

不需要在用户实体上做任何修改,只需为所有邀请创建一个新表。

创建一个仅要求电子邮件的新密码重置表单类型

class ForgetPasswordType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('email', EmailType::class, [
				'label' => 'Type your email'
            ])
        ;
    }
}

class ResetPasswordType extends AbstractType
{
	public function buildForm(FormBuilderInterface $builder, array $options): void
	{
		$builder
			->add('password', PasswordType::class, [
				'label' => 'Type the new password',
				'help_html' => true,
			])
		;
	}
}

现在我们将创建一个 "邀请" 类,必须继承自 AbstractInvitation,这将自动将其标记为 app.invitation

class ResetPassword extends AbstractInvitation
{
	public function setMailContent(TemplatedEmail $email, InvitationEntityManager $invitationEntityManagerManager): void
	{
		$email->subject('Reset password')
			->htmlTemplate('mail/invitations/resetpassword.html.twig');
	}

	public function resolve(InvitationEntityManager $invitationEntityManagerManager): bool
	{
	    // nothing here all is treated in controller
		return true;
	}

	public function getResolvePath(InvitationEntityManager $invitationEntityManagerManager) : array
	{
		return [
			'path' => 'app_reset_password',
			'args' => ['id' => $invitationEntityManagerManager->getInvitation()->getId(), 'token' => $invitationEntityManagerManager->getInvitation()->getToken()],
		];
	}

	public function resendMailIfAlreadyExists(): bool
	{
		return true;
	}
}

邮件模板将包含链接

Reset your password : 
<a href="{{ include(template_from_string(invitation_url)) }}">Link</a>

现在在安全控制器中创建第一个路由以显示表单并发送邮件

	#[Route(path: '/forget-password', name: 'app_forget_password')]
	public function forgetPassword(
		Request $request,
		UserRepository $userRepository,
		InvitationManager $invitationManager
	): Response
	{
		$form = $this->createForm(ForgetPasswordType::class);
		$form->handleRequest($request);

		if($form->isSubmitted() && $form->isValid())
		{
			$user = $userRepository->findOneByEmail($form->get('email')->getData());

			if($user != null)
			{
				$invitationManager->create(ResetPassword::class, $user, $form->get('email')->getData(),0);
			}

			return $this->render('message.html.twig', [
				'message' => 'If you mail is on our base, we sent you a message for reset your password',
			]);
		}

		return $this->render('security/reset_password/reset_password_request.html.twig', [
			'form' => $form->createView()
		]);
	}

当点击邮件中的链接时调用此路由

	#[Route(path: '/reset-password', name: 'app_reset_password')]
	public function resetPassword(
		Request $request,
		InvitationManager $invitationManager,
		EntityManagerInterface $em,
		UserPasswordHasherInterface $passwordHasher
	): Response
	{
		$invitation = $invitationManager->getInvit($request->query->get('id'), ['type' => [ResetPassword::class], 'token' => $request->query->get('token')]);
		if($invitation == null)
		{
			throw new ItemNotFoundException('Unkwnon Invitation');
		}

		$user = $invitation->getInvitation()->getUser();
		$form = $this->createForm(ResetPasswordType::class, $user, ['action' => $invitation->getResolvePath(true)]);
		$form->handleRequest($request);

		if($form->isSubmitted() && $form->isValid())
		{
			$user->setPassword($passwordHasher->hashPassword($user,$user->getPassword()));
			$em->flush();

			$invitation->resolve(true);

			$this->addFlash('success', 'Your password has been changed.');
			return $this->redirectToRoute('app_login');
		}

		return $this->render('security/reset_password/reset_password_request.html.twig', [
			'form' => $form->createView()
		]);
	}

如您所见,您需要编写代码以实现所需动作的逻辑,但不需要发送邮件、获取令牌、检索哪个用户,所有这些都已为您完成。