[CORE][SECURITY][EMAIL] Move email confirmation functionality to it's own static wrapper, in preparation for adding password reset functionality

This commit is contained in:
Hugo Sales 2021-07-29 15:03:52 +00:00
parent 176d604abb
commit bdbd588de9
Signed by untrusted user: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
3 changed files with 38 additions and 37 deletions

View File

@ -13,7 +13,6 @@ use App\Entity\GSActor;
use App\Entity\LocalUser;
use App\Entity\Note;
use App\Security\Authenticator;
use App\Security\EmailVerifier;
use app\Util\Common;
use App\Util\Exception\EmailTakenException;
use App\Util\Exception\NicknameTakenException;
@ -67,7 +66,6 @@ class Security extends Controller
* possibly sending a confirmation email
*/
public function register(Request $request,
EmailVerifier $email_verifier,
GuardAuthenticatorHandler $guard_handler,
Authenticator $authenticator)
{
@ -142,16 +140,8 @@ class Security extends Controller
}
// generate a signed url and email it to the user
if (Common::config('site', 'use_email')) {
$email_verifier->sendEmailConfirmation(
'verify_email',
$user,
(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')
);
if ($_ENV['APP_ENV'] === 'dev' || Common::config('site', 'use_email')) {
Common::sendVerificationEmail();
} else {
$user->setIsEmailVerified(true);
}

View File

@ -69,6 +69,7 @@ use Symfony\Component\Security\Core\Security as SSecurity;
use Symfony\Component\Security\Http\Util\TargetPathTrait;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use SymfonyCasts\Bundle\VerifyEmail\VerifyEmailHelperInterface;
use Twig\Environment;
/**
@ -95,6 +96,7 @@ class GNUsocial implements EventSubscriberInterface
protected ContainerBagInterface $config;
protected Environment $twig;
protected ?Request $request;
protected VerifyEmailHelperInterface $email_verify_helper;
/**
* Symfony dependency injection gives us access to these services
@ -114,24 +116,26 @@ class GNUsocial implements EventSubscriberInterface
SanitizerInterface $san,
ContainerBagInterface $conf,
Environment $twig,
RequestStack $request_stack)
RequestStack $request_stack,
VerifyEmailHelperInterface $email_helper)
{
$this->logger = $logger;
$this->translator = $trans;
$this->entity_manager = $em;
$this->router = $router;
$this->url_generator = $url_gen;
$this->form_factory = $ff;
$this->message_bus = $mb;
$this->event_dispatcher = $ed;
$this->session = $sess;
$this->security = $sec;
$this->module_manager = $mm;
$this->client = $cl;
$this->sanitizer = $san;
$this->config = $conf;
$this->twig = $twig;
$this->request = $request_stack->getCurrentRequest();
$this->logger = $logger;
$this->translator = $trans;
$this->entity_manager = $em;
$this->router = $router;
$this->url_generator = $url_gen;
$this->form_factory = $ff;
$this->message_bus = $mb;
$this->event_dispatcher = $ed;
$this->session = $sess;
$this->security = $sec;
$this->module_manager = $mm;
$this->client = $cl;
$this->sanitizer = $san;
$this->config = $conf;
$this->twig = $twig;
$this->request = $request_stack->getCurrentRequest();
$this->email_verify_helper = $email_helper;
$this->initialize();
}
@ -159,6 +163,7 @@ class GNUsocial implements EventSubscriberInterface
HTTPClient::setClient($this->client);
Formatting::setTwig($this->twig);
Cache::setupCache();
EmailVerifier::setVerifyEmailHelper($this->email_verify_helper);
DB::initTableMap();

View File

@ -6,23 +6,29 @@ use App\Core\DB\DB;
use App\Core\Mailer;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Mime\Address;
use Symfony\Component\Security\Core\User\UserInterface;
use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
use SymfonyCasts\Bundle\VerifyEmail\VerifyEmailHelperInterface;
class EmailVerifier
abstract class EmailVerifier
{
private $verify_email_helper;
public function __construct(VerifyEmailHelperInterface $helper)
private static ?VerifyEmailHelperInterface $verify_email_helper;
public function setVerifyEmailHelper(VerifyEmailHelperInterface $helper)
{
$this->verifyEmailHelper = $helper;
self::$verifyEmailHelper = $helper;
}
public function sendEmailConfirmation(string $verify_email_route_name, UserInterface $user, TemplatedEmail $email): void
public static function sendEmailConfirmation(UserInterface $user): void
{
$signatureComponents = $this->verify_email_helper->generateSignature(
$verify_email_route_name,
$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',
$user->getId(),
$user->getOutgoingEmail()
);