2020-07-25 02:06:55 +00:00
|
|
|
<?php
|
|
|
|
|
2021-10-10 09:26:18 +01:00
|
|
|
declare(strict_types = 1);
|
|
|
|
|
2020-07-25 02:06:55 +00:00
|
|
|
namespace App\Security;
|
|
|
|
|
2021-09-06 23:47:28 +01:00
|
|
|
use App\Controller\ResetPassword;
|
2020-07-25 02:06:55 +00:00
|
|
|
use App\Core\DB\DB;
|
2021-09-06 19:49:03 +01:00
|
|
|
use function App\Core\I18n\_m;
|
2021-09-06 23:47:28 +01:00
|
|
|
use App\Entity\LocalUser;
|
2021-09-06 19:49:03 +01:00
|
|
|
use App\Util\Common;
|
2021-07-29 17:26:14 +00:00
|
|
|
use App\Util\Exception\NotFoundException;
|
|
|
|
use App\Util\Exception\RedirectException;
|
2020-07-25 02:06:55 +00:00
|
|
|
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2021-07-29 17:26:14 +00:00
|
|
|
use Symfony\Component\Mailer\MailerInterface;
|
2021-07-29 15:03:52 +00:00
|
|
|
use Symfony\Component\Mime\Address;
|
2021-09-06 19:49:03 +01:00
|
|
|
use SymfonyCasts\Bundle\ResetPassword\Exception\ResetPasswordExceptionInterface;
|
2021-07-29 17:26:14 +00:00
|
|
|
use SymfonyCasts\Bundle\ResetPassword\ResetPasswordHelperInterface;
|
2020-07-25 02:06:55 +00:00
|
|
|
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 17:26:14 +00:00
|
|
|
private static ?MailerInterface $mailer_helper;
|
2021-07-29 15:03:52 +00:00
|
|
|
private static ?VerifyEmailHelperInterface $verify_email_helper;
|
2021-07-29 17:26:14 +00:00
|
|
|
private static ?ResetPasswordHelperInterface $reset_password_helper;
|
|
|
|
|
|
|
|
public static function setEmailHelpers(MailerInterface $mailer, VerifyEmailHelperInterface $email_helper, ResetPasswordHelperInterface $reset_helper)
|
|
|
|
{
|
|
|
|
self::$mailer_helper = $mailer;
|
|
|
|
self::$verify_email_helper = $email_helper;
|
|
|
|
self::$reset_password_helper = $reset_helper;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function validateTokenAndFetchUser(string $token)
|
2020-07-25 02:06:55 +00:00
|
|
|
{
|
2021-07-29 17:26:14 +00:00
|
|
|
return self::$reset_password_helper->validateTokenAndFetchUser($token);
|
|
|
|
}
|
|
|
|
|
2021-09-06 23:47:28 +01:00
|
|
|
public static function removeResetRequest(string $token): void
|
2021-07-29 17:26:14 +00:00
|
|
|
{
|
2021-09-06 23:47:28 +01:00
|
|
|
self::$reset_password_helper->removeResetRequest($token);
|
2020-07-25 02:06:55 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 23:47:28 +01:00
|
|
|
public static function sendEmailConfirmation(LocalUser $user): void
|
2020-07-25 02:06:55 +00:00
|
|
|
{
|
2021-07-29 15:03:52 +00:00
|
|
|
$email = (new TemplatedEmail())
|
2021-10-10 09:26:18 +01:00
|
|
|
->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');
|
2021-07-29 15:03:52 +00:00
|
|
|
|
|
|
|
$signatureComponents = self::$verify_email_helper->generateSignature(
|
|
|
|
'verify_email',
|
2020-07-25 02:06:55 +00:00
|
|
|
$user->getId(),
|
2021-10-10 09:26:18 +01:00
|
|
|
$user->getOutgoingEmail(),
|
2020-07-25 02:06:55 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$context = $email->getContext();
|
|
|
|
$context['signedUrl'] = $signatureComponents->getSignedUrl();
|
|
|
|
$context['expiresAt'] = $signatureComponents->getExpiresAt();
|
|
|
|
|
|
|
|
$email->context($context);
|
|
|
|
|
2021-07-29 17:26:14 +00:00
|
|
|
self::send($email);
|
|
|
|
}
|
|
|
|
|
2021-09-06 23:47:28 +01:00
|
|
|
public static function send($email): void
|
2021-07-29 17:26:14 +00:00
|
|
|
{
|
2021-09-06 23:47:28 +01:00
|
|
|
self::$mailer_helper->send($email);
|
2020-07-25 02:06:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws VerifyEmailExceptionInterface
|
|
|
|
*/
|
2021-09-06 23:47:28 +01:00
|
|
|
public function handleEmailConfirmation(Request $request, LocalUser $user): void
|
2020-07-25 02:06:55 +00:00
|
|
|
{
|
2021-07-29 17:26:14 +00:00
|
|
|
self::$verify_email_helper->validateEmailConfirmation($request->getUri(), $user->getId(), $user->getOutgoingEmail());
|
2020-07-25 02:06:55 +00:00
|
|
|
$user->setIsEmailVerified(true);
|
|
|
|
DB::persist($user);
|
|
|
|
DB::flush();
|
|
|
|
}
|
2021-07-29 17:26:14 +00:00
|
|
|
|
2021-09-06 23:47:28 +01:00
|
|
|
public static function processSendingPasswordResetEmail(string $emailFormData, ResetPassword $controller)
|
2021-07-29 17:26:14 +00:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
$user = DB::findOneBy('local_user', ['outgoing_email' => $emailFormData]);
|
|
|
|
$reset_token = self::$reset_password_helper->generateResetToken($user);
|
|
|
|
// Found a user
|
2021-10-10 09:26:18 +01:00
|
|
|
} catch (NotFoundException|ResetPasswordExceptionInterface) {
|
2021-07-29 17:26:14 +00:00
|
|
|
// Not found, do not reveal whether a user account was found or not.
|
|
|
|
throw new RedirectException('check_email');
|
|
|
|
}
|
|
|
|
|
|
|
|
$email = (new TemplatedEmail())
|
2021-10-10 09:26:18 +01:00
|
|
|
->from(new Address('foo@email.com', 'FOO NAME'))
|
|
|
|
->to($user->getOutgoingEmail())
|
|
|
|
->subject('Your password reset request')
|
|
|
|
->htmlTemplate('reset_password/email.html.twig')
|
|
|
|
->context([
|
|
|
|
'resetToken' => $reset_token,
|
|
|
|
]);
|
2021-07-29 17:26:14 +00:00
|
|
|
|
|
|
|
self::send($email);
|
|
|
|
|
|
|
|
// Store the token object in session for retrieval in check-email route.
|
2021-09-06 23:47:28 +01:00
|
|
|
$controller->setInSession($reset_token);
|
2021-07-29 17:26:14 +00:00
|
|
|
|
|
|
|
throw new RedirectException('check_email');
|
|
|
|
}
|
2020-07-25 02:06:55 +00:00
|
|
|
}
|