src/EventListener/Api/ApiAccessControlListener.php line 46

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\EventListener\Api;
  4. use App\Application\Service\Customer\CustomerService;
  5. use App\Application\Service\Helper\LogWriterService;
  6. use App\Manager\System\CustomerManager;
  7. use App\Service\EnvironmentService;
  8. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\HttpKernel\Event\RequestEvent;
  11. use Symfony\Component\Security\Core\Security;
  12. class ApiAccessControlListener
  13. {
  14.     protected LogWriterService $loggerService;
  15.     protected CustomerService $customerService;
  16.     protected CustomerManager $customerManager;
  17.     protected Security $security;
  18.     protected EnvironmentService $environmentService;
  19.     private ParameterBagInterface $parameterBag;
  20.     public function __construct(
  21.         LogWriterService $loggerService,
  22.         CustomerService $customerService,
  23.         CustomerManager $customerManager,
  24.         Security $security,
  25.         EnvironmentService $environmentService,
  26.         ParameterBagInterface $parameterBag
  27.     ) {
  28.         $this->loggerService $loggerService;
  29.         $this->customerService $customerService;
  30.         $this->customerManager $customerManager;
  31.         $this->security $security;
  32.         $this->environmentService $environmentService;
  33.         $this->parameterBag $parameterBag;
  34.     }
  35.     public function onKernelRequest(RequestEvent $event): void
  36.     {
  37.         if (!$this->environmentService->isApi() || $this->parameterBag->get('api_sandbox')) {
  38.             return;
  39.         }
  40.         $token $this->security->getToken();
  41.         if ($token === null || $token->getUser() === null) {
  42.             return;
  43.         }
  44.         $customer $this->customerManager->findOne($token->getUser());
  45.         if ($customer === null) {
  46.             return;
  47.         }
  48.         if (!$this->customerService->hasValidPack($customer->getDateCsv())) {
  49.             $response = new Response('User does not have a valid pack: '.$customer->getId(), Response::HTTP_FORBIDDEN);
  50.             $event->setResponse($response);
  51.         }
  52.     }
  53. }