diff --git a/src/Controller/Security.php b/src/Controller/Security.php index 1119977372..0d95337e76 100644 --- a/src/Controller/Security.php +++ b/src/Controller/Security.php @@ -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); } diff --git a/src/Core/GNUsocial.php b/src/Core/GNUsocial.php index 261d1c6ae7..71d1802530 100644 --- a/src/Core/GNUsocial.php +++ b/src/Core/GNUsocial.php @@ -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(); diff --git a/src/Security/EmailVerifier.php b/src/Security/EmailVerifier.php index fb77bc3a3a..c1245f4c59 100644 --- a/src/Security/EmailVerifier.php +++ b/src/Security/EmailVerifier.php @@ -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() );