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

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