src/Core/Domain/Infrastructure/Annotation/AccessEventSubscriber.php line 38

  1. <?php
  2.     
  3.     namespace App\Core\Domain\Infrastructure\Annotation;
  4.     
  5.     use App\Core\Domain\Infrastructure\Repository\Insales\Interface\ApplicationRepositoryInterface;
  6.     use App\Core\Ports\Http\Error\SessionEndAction;
  7.     use App\Shared\Domain\Exception\AccessForbiddenException;
  8.     use App\Shared\Domain\Exception\InvalidInputDataException;
  9.     use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
  10.     use Symfony\Component\HttpFoundation\RedirectResponse;
  11.     use Symfony\Component\HttpKernel\Event\ControllerEvent;
  12.     use Symfony\Component\HttpKernel\KernelEvents;
  13.     use Symfony\Component\Routing\RouterInterface;
  14.     /**
  15.      * Class AccessEventSubscriber
  16.      * @package App\Core\Domain\Infrastructure\Annotation
  17.      */
  18.     final class AccessEventSubscriber
  19.     {
  20.     
  21.         /**
  22.          * @param ApplicationRepositoryInterface $applicationRepository
  23.          * @param RouterInterface $router
  24.          */
  25.         public function __construct(
  26.             private readonly ApplicationRepositoryInterface $applicationRepository,
  27.             private readonly RouterInterface                $router
  28.         )
  29.         {
  30.         }
  31.     
  32.         /**
  33.          * @throws AccessForbiddenException
  34.          * @throws InvalidInputDataException
  35.          */
  36.         #[AsEventListener(KernelEvents::CONTROLLER)]
  37.         public function onController(ControllerEvent $event)
  38.         {
  39.             if(
  40.                 !empty($event->getAttributes()[Access::class][0])
  41.                 && $event->getAttributes()[Access::class][0] instanceof Access
  42.             )
  43.             {
  44.     
  45.                 /** @var Access $access */
  46.                 $access $event->getAttributes()[Access::class][0];
  47.     
  48.                 try
  49.                 {
  50.                     if(!$event->getRequest()->getSession()->has('insalesId'))
  51.                     {
  52.                         throw new AccessForbiddenException(
  53.                             AccessForbiddenException::SESSION_CHECK_FAIL
  54.                         );
  55.                     }
  56.                     if(!$event->getRequest()->attributes->has($access->getName()))
  57.                     {
  58.                         throw new AccessForbiddenException(
  59.                             AccessForbiddenException::SESSION_CHECK_FAIL
  60.                         );
  61.                     }
  62.     
  63.                     $application $this->applicationRepository->findOneByUuid(
  64.                         $event->getRequest()->attributes->get(
  65.                             $access->getName()
  66.                         )
  67.                     );
  68.     
  69.                     if(
  70.                         $application === null
  71.                         || $application->getInsalesId() !== $event->getRequest()->getSession()->get('insalesId')
  72.                     )
  73.                     {
  74.                         throw new AccessForbiddenException(
  75.                             AccessForbiddenException::SESSION_CHECK_FAIL
  76.                         );
  77.                     }
  78.     
  79.                     if(empty($access->getName()))
  80.                     {
  81.                         throw new AccessForbiddenException(
  82.                             InvalidInputDataException::INVALID_ACCESS_CONFIGURATION
  83.                         );
  84.                     }
  85.                 }
  86.                 catch (AccessForbiddenException $exception)
  87.                 {
  88.                     if($access->isAutoRedirect())
  89.                     {
  90.                         $event->setController(
  91.                             function()
  92.                             {
  93.                                 return new RedirectResponse(
  94.                                     $this->router->generate(
  95.                                         SessionEndAction::ROUTE_NAME
  96.                                     )
  97.                                 );
  98.                             }
  99.                         );
  100.                     }
  101.                     else
  102.                     {
  103.                         throw $exception;
  104.                     }
  105.                 }
  106.             }
  107.         }
  108.     }