src/EventListener/Api/ApiAccessControlListener.php line 41

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\HttpFoundation\Response;
  9. use Symfony\Component\HttpKernel\Event\RequestEvent;
  10. use Symfony\Component\Security\Core\Security;
  11. class ApiAccessControlListener
  12. {
  13.     protected LogWriterService $loggerService;
  14.     protected CustomerService $customerService;
  15.     protected CustomerManager $customerManager;
  16.     protected Security $security;
  17.     protected EnvironmentService $environmentService;
  18.     public function __construct(
  19.         LogWriterService $loggerService,
  20.         CustomerService $customerService,
  21.         CustomerManager $customerManager,
  22.         Security $security,
  23.         EnvironmentService $environmentService
  24.     ) {
  25.         $this->loggerService $loggerService;
  26.         $this->customerService $customerService;
  27.         $this->customerManager $customerManager;
  28.         $this->security $security;
  29.         $this->environmentService $environmentService;
  30.     }
  31.     public function onKernelRequest(RequestEvent $event): void
  32.     {
  33.         if (!$this->environmentService->isApi()) {
  34.             return;
  35.         }
  36.         if (!$this->environmentService->isApiSandbox()) {
  37.             $token $this->security->getToken();
  38.             if ($token === null || $token->getUser() === null) {
  39.                 return;
  40.             }
  41.             $customer $this->customerManager->findOne($token->getUser());
  42.             if ($customer === null) {
  43.                 return;
  44.             }
  45.             if (!$this->customerService->hasValidPack($customer->getDateCsv())) {
  46.                 $response = new Response('User does not have a valid pack: '.$customer->getId(), Response::HTTP_FORBIDDEN);
  47.                 $event->setResponse($response);
  48.             }
  49.         }
  50.     }
  51. }