2020-07-25 02:06:55 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Security;
|
|
|
|
|
|
|
|
use App\Core\DB\DB;
|
|
|
|
use App\Core\Mailer;
|
|
|
|
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2021-07-29 15:03:52 +00:00
|
|
|
use Symfony\Component\Mime\Address;
|
2020-07-25 02:06:55 +00:00
|
|
|
use Symfony\Component\Security\Core\User\UserInterface;
|
|
|
|
use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
|
|
|
|
use SymfonyCasts\Bundle\VerifyEmail\VerifyEmailHelperInterface;
|
|
|
|
|
2021-07-29 15:03:52 +00:00
|
|
|
abstract class EmailVerifier
|
2020-07-25 02:06:55 +00:00
|
|
|
{
|
2021-07-29 15:03:52 +00:00
|
|
|
private static ?VerifyEmailHelperInterface $verify_email_helper;
|
|
|
|
public function setVerifyEmailHelper(VerifyEmailHelperInterface $helper)
|
2020-07-25 02:06:55 +00:00
|
|
|
{
|
2021-07-29 15:03:52 +00:00
|
|
|
self::$verifyEmailHelper = $helper;
|
2020-07-25 02:06:55 +00:00
|
|
|
}
|
|
|
|
|
2021-07-29 15:03:52 +00:00
|
|
|
public static function sendEmailConfirmation(UserInterface $user): void
|
2020-07-25 02:06:55 +00:00
|
|
|
{
|
2021-07-29 15:03:52 +00:00
|
|
|
$email = (new TemplatedEmail())
|
|
|
|
->from(new Address(Common::config('site', 'email'), Common::config('site', 'nickname')))
|
|
|
|
->to($user->getOutgoingEmail())
|
|
|
|
->subject(_m('Please Confirm your Email'))
|
|
|
|
->htmlTemplate('security/confirmation_email.html.twig');
|
|
|
|
|
|
|
|
$signatureComponents = self::$verify_email_helper->generateSignature(
|
|
|
|
'verify_email',
|
2020-07-25 02:06:55 +00:00
|
|
|
$user->getId(),
|
|
|
|
$user->getOutgoingEmail()
|
|
|
|
);
|
|
|
|
|
|
|
|
$context = $email->getContext();
|
|
|
|
$context['signedUrl'] = $signatureComponents->getSignedUrl();
|
|
|
|
$context['expiresAt'] = $signatureComponents->getExpiresAt();
|
|
|
|
|
|
|
|
$email->context($context);
|
|
|
|
|
|
|
|
Mailer::send($email);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws VerifyEmailExceptionInterface
|
|
|
|
*/
|
|
|
|
public function handleEmailConfirmation(Request $request, UserInterface $user): void
|
|
|
|
{
|
|
|
|
$this->verify_email_helper->validateEmailConfirmation($request->getUri(), $user->getId(), $user->getOutgoingEmail());
|
|
|
|
|
|
|
|
$user->setIsEmailVerified(true);
|
|
|
|
|
|
|
|
DB::persist($user);
|
|
|
|
DB::flush();
|
|
|
|
}
|
|
|
|
}
|