src/EventSubscriber/ApiRequestSubcriber.php line 37

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\ClientApi;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use http\Exception\BadHeaderException;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\Finder\Exception\AccessDeniedException;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\HttpKernel\Event\RequestEvent;
  11. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  12. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  13. use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
  14. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  15. use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
  16. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  17. use Symfony\Component\Security\Core\Exception\BadCredentialsException;
  18. use Symfony\Component\Security\Core\Security;
  19. class ApiRequestSubcriber implements EventSubscriberInterface
  20. {
  21.     private $security;
  22.     private $router;
  23.     private $entityManager;
  24.     public function __construct(Security $securityUrlGeneratorInterface $routerEntityManagerInterface $entityManager)
  25.     {
  26.         // Avoid calling getUser() in the constructor: auth may not
  27.         // be complete yet. Instead, store the entire Security object.
  28.         $this->security $security;
  29.         $this->router $router;
  30.         $this->entityManager $entityManager;
  31.     }
  32.     public function onKernelRequest(RequestEvent $event)
  33.     {
  34.         $pathInfo $event->getRequest()->getPathInfo();
  35.         $patharray explode("/",$pathInfo);
  36.         //dd($patharray);
  37.         $path2 array_key_exists(2,$patharray)? $patharray[2]:'';
  38.         if ($patharray[1] == "api" && $path2 != "docs" )
  39.         {
  40.             //return;
  41.             //$token = $event->getRequest()->headers->get('X-AUTH-TOKEN');
  42.             //$client = $this->entityManager->getRepository(ClientApi::class)->findOneBy(["apiToken"=>$token]);
  43.             $user $this->security->getUser();
  44.             if($user !== null) {
  45.                 if($user->isIsActivated() == false  OR $user->isIsDeleted() == true) {
  46.                     throw new UnauthorizedHttpException("","Votre compte est désactivé , merci de vous rapprocher des admins  pour résoudre ce problème");
  47.                 }
  48.                 /**if($user->getAgent() !== null && $user->getAgent()->getIsDeleted() == true ) {
  49.                     throw new UnauthorizedHttpException("","Ce compte est inexistant");
  50.                 }**/
  51.             }
  52.            /** if($client == null) {
  53.                 throw new UnauthorizedHttpException("","Authentification cliente  requise");
  54.             } else {
  55.                 $ipAdress = $event->getRequest()->getClientIp();
  56.                 if ($client->isActivated() == false){
  57.                     throw new UnauthorizedHttpException("","Client désactivé , merci de vous rapprocher des administrateurs de l'api  ");
  58.                 }
  59.                 return;
  60.             }**/
  61.         }
  62.     }
  63.     public static function getSubscribedEvents()
  64.     {
  65.         return [
  66.             RequestEvent::class => 'onKernelRequest',
  67.         ];
  68.     }
  69. }