<?php
namespace App\EventSubscriber;
use App\Entity\ClientApi;
use Doctrine\ORM\EntityManagerInterface;
use http\Exception\BadHeaderException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Finder\Exception\AccessDeniedException;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
use Symfony\Component\Security\Core\Security;
class ApiRequestSubcriber implements EventSubscriberInterface
{
private $security;
private $router;
private $entityManager;
public function __construct(Security $security, UrlGeneratorInterface $router, EntityManagerInterface $entityManager)
{
// Avoid calling getUser() in the constructor: auth may not
// be complete yet. Instead, store the entire Security object.
$this->security = $security;
$this->router = $router;
$this->entityManager = $entityManager;
}
public function onKernelRequest(RequestEvent $event)
{
$pathInfo = $event->getRequest()->getPathInfo();
$patharray = explode("/",$pathInfo);
//dd($patharray);
$path2 = array_key_exists(2,$patharray)? $patharray[2]:'';
if ($patharray[1] == "api" && $path2 != "docs" )
{
//return;
//$token = $event->getRequest()->headers->get('X-AUTH-TOKEN');
//$client = $this->entityManager->getRepository(ClientApi::class)->findOneBy(["apiToken"=>$token]);
$user = $this->security->getUser();
if($user !== null) {
if($user->isIsActivated() == false OR $user->isIsDeleted() == true) {
throw new UnauthorizedHttpException("","Votre compte est désactivé , merci de vous rapprocher des admins pour résoudre ce problème");
}
/**if($user->getAgent() !== null && $user->getAgent()->getIsDeleted() == true ) {
throw new UnauthorizedHttpException("","Ce compte est inexistant");
}**/
}
/** if($client == null) {
throw new UnauthorizedHttpException("","Authentification cliente requise");
} else {
$ipAdress = $event->getRequest()->getClientIp();
if ($client->isActivated() == false){
throw new UnauthorizedHttpException("","Client désactivé , merci de vous rapprocher des administrateurs de l'api ");
}
return;
}**/
}
}
public static function getSubscribedEvents()
{
return [
RequestEvent::class => 'onKernelRequest',
];
}
}