src/Flexy/FrontBundle/EventSubscriber/ApiPlatformKernelListener.php line 29

  1. <?php
  2. namespace App\Flexy\FrontBundle\EventSubscriber;
  3. use App\Entity\Settings;
  4. use Doctrine\Persistence\ManagerRegistry;
  5. use Exception;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  8. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  9. use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpKernel\Event\RequestEvent;
  13. use Symfony\Component\Security\Core\Security;
  14. #[AsEventListener(eventRequestEvent::class, method'onKernelRequest')]
  15. class ApiPlatformKernelListener
  16. {
  17.     public function __construct(
  18.         private readonly ManagerRegistry $doctrine,private readonly Security $security
  19.         ){
  20.     }
  21.     public function onKernelRequest(RequestEvent $event)
  22.     {
  23.         
  24.         $request $event->getRequest();
  25.         // Check if the request is targeting the API platform
  26.         if (strpos($request->getPathInfo(), '/'.$request->getLocale().'/api') !== 0) {
  27.             
  28.             return;
  29.         }
  30.         
  31.         
  32.          $settings $this->doctrine->getRepository(Settings::class)->findOneBy(["code"=>"main"]);
  33.         
  34.          if($settings->getAssetFolderName() == "Taxiciel"){
  35.             
  36.              $allowedURIs = [
  37.                 '/'.$request->getLocale().'/api/booking/ajax-preview-mission',
  38.                  '/'.$request->getLocale().'/api/missions-by-invoice',
  39.                  '/'.$request->getLocale().'/api_login_check',
  40.                  '/'.$request->getLocale().'/api/customers',
  41.                  '/'.$request->getLocale().'/api/city_regions',
  42.                  '/'.$request->getLocale().'/api/shipping_vehicle_types',
  43.              ];
  44.             
  45.             
  46.              foreach($allowedURIs as $singleURI){
  47.                  if (strpos($request->getPathInfo(), $singleURI) === 0) {
  48.                       return;
  49.                  }
  50.              }
  51.               //User is not authenticated or doesn't have admin role
  52.              $response = new JsonResponse(["message"=>'No Api Available'], Response::HTTP_FORBIDDEN);
  53.              $event->setResponse($response);
  54.             
  55.             
  56.         }
  57.         
  58.       
  59.     }
  60. }