vendor/eckinox/security-bundle/src/EventSubscriber/AppUserSubscriber.php line 36

Open in your IDE?
  1. <?php
  2. namespace Eckinox\SecurityBundle\EventSubscriber;
  3. use Eckinox\SecurityBundle\Event\AppUserEvent;
  4. use Eckinox\SecurityBundle\Notifier\CreatePasswordLoginLinkNotification;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpFoundation\RequestStack;
  7. use Symfony\Component\Notifier\NotifierInterface;
  8. use Symfony\Component\Notifier\Recipient\Recipient;
  9. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  10. use Symfony\Component\Security\Http\LoginLink\LoginLinkDetails;
  11. use Symfony\Component\Security\Http\LoginLink\LoginLinkHandlerInterface;
  12. use Symfony\Contracts\Translation\TranslatorInterface;
  13. use Twig\Environment;
  14. class AppUserSubscriber implements EventSubscriberInterface
  15. {
  16.     public function __construct(
  17.         private RequestStack $requestStack,
  18.         private LoginLinkHandlerInterface $loginLinkHandler,
  19.         private Environment $twig,
  20.         private UrlGeneratorInterface $router,
  21.         private NotifierInterface $notifier,
  22.         private TranslatorInterface $translator
  23.     ) {
  24.     }
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             AppUserEvent::CREATED_SUCCESS => [['onUserCreate'0]],
  29.         ];
  30.     }
  31.     public function onUserCreate(AppUserEvent $event): void
  32.     {
  33.         $user $event->getUser();
  34.         $recipient = new Recipient($user->getEmail());
  35.         $loginLinkDetails $this->loginLinkHandler->createLoginLink($user);
  36.         // Remake the login URL for the frontend firewall
  37.         $url str_replace($this->router->generate('eckinox_security_login_check'), $this->router->generate('eckinox_security_app_login_check'), $loginLinkDetails->getUrl());
  38.         $loginLinkDetails = new LoginLinkDetails($url$loginLinkDetails->getExpiresAt());
  39.         $template $this->twig->getLoader()->exists('@EckinoxSecurity/email/app_create_password_login_email.html.twig') ? '@EckinoxSecurity/email/app_create_password_login_email.html.twig' null;
  40.         $subject $this->translator->trans('eckinox_security.messages.app_welcome') == 'eckinox_security.messages.app_welcome' $this->translator->trans('eckinox_security.messages.welcome') : $this->translator->trans('eckinox_security.messages.app_welcome');
  41.         $notification = new CreatePasswordLoginLinkNotification(
  42.             $this->translator,
  43.             $loginLinkDetails,
  44.             $subject,
  45.             $template
  46.         );
  47.         
  48.         $this->notifier->send($notification$recipient);
  49.     }
  50. }