src/App/Security/Firewall/PortalFirewall.php line 50

Open in your IDE?
  1. <?php
  2. namespace App\Security\Firewall;
  3. use Products\NotificationsBundle\Controller\AbstractPortalController;
  4. use Products\NotificationsBundle\Controller\Portal\LoginController;
  5. use Products\NotificationsBundle\Controller\Portal\MessagesController;
  6. use Products\NotificationsBundle\Controller\Portal\OptInController;
  7. use Products\NotificationsBundle\Entity\Profile;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpFoundation\RedirectResponse;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\RequestMatcherInterface;
  12. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  13. use Symfony\Component\Routing\RouterInterface;
  14. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  15. use Symfony\Component\Security\Core\Security;
  16. use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
  17. /**
  18.  * Class PortalFirewall
  19.  * @package App\Security\Firewall
  20.  */
  21. final readonly class PortalFirewall implements
  22.     RequestMatcherInterface,
  23.     AuthenticationEntryPointInterface,
  24.     EventSubscriberInterface
  25. {
  26.     public function __construct(
  27.         private RouterInterface $router,
  28.         private Security $security,
  29.     )
  30.     {
  31.     }
  32.     /**
  33.      * {@inheritDoc}
  34.      */
  35.     public static function getSubscribedEvents(): array
  36.     {
  37.         return [
  38.             ControllerEvent::class => 'onController',
  39.         ];
  40.     }
  41.     /**
  42.      * @param ControllerEvent $event
  43.      * @return void
  44.      */
  45.     public function onController(
  46.         ControllerEvent $event,
  47.     ): void
  48.     {
  49.         $controller $event->getController();
  50.         if (is_array($controller)) {
  51.             $controller $controller[0];
  52.         }
  53.         if ($controller instanceof AbstractPortalController && $this->matches($event->getRequest()) && ! $this->security->getUser() instanceof Profile) {
  54.             throw new AuthenticationException();
  55.         }
  56.     }
  57.     /**
  58.      * {@inheritDoc}
  59.      */
  60.     public function matches(
  61.         Request $request,
  62.     ): bool
  63.     {
  64.         $route $request->attributes->get('_route');
  65.         if ( ! empty($route) && str_starts_with($route'app.notifications.portal.')) {
  66.             switch (true) {
  67.                 case $route === LoginController::ROUTES__LOGIN:
  68.                 case $route === LoginController::ROUTES__UNMATCHED:
  69.                 case $route === LoginController::ROUTES__CODE:
  70.                 case $route === LoginController::ROUTES__SELECT:
  71.                 case $route === MessagesController::ROUTES__MAIN:
  72.                 case $route === MessagesController::ROUTES__DETAILS:
  73.                 case $route === MessagesController::ROUTES__CONTACT:
  74.                 case $route === MessagesController::ROUTES__MESSAGE_HTML:
  75.                 case $route === OptInController::ROUTES__MAIN:
  76.                 case $route === OptInController::ROUTES__NOTICE:
  77.                 case $route === OptInController::ROUTES__PRIVACY_POLICY:
  78.                     return false;
  79.                 default:
  80.                     return true;
  81.             }
  82.         }
  83.         return false;
  84.     }
  85.     /**
  86.      * {@inheritDoc}
  87.      */
  88.     public function start(
  89.         Request $request,
  90.         ?AuthenticationException $authException null,
  91.     ): RedirectResponse
  92.     {
  93.         return new RedirectResponse(
  94.             $this->router->generate(
  95.                 LoginController::ROUTES__LOGIN,
  96.             ),
  97.         );
  98.     }
  99. }