src/EventSubscriber/ParticipationSubscriber.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Document\Participation;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpFoundation\ParameterBag;
  7. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  8. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. /**
  11.  * Class ParticipationSubscriber.
  12.  */
  13. class ParticipationSubscriber implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @return array
  17.      */
  18.     public static function getSubscribedEvents()
  19.     {
  20.         return [
  21.             KernelEvents::CONTROLLER => [
  22.                 ['hasFilter'EventPriorities::PRE_READ],
  23.             ]
  24.         ];
  25.     }
  26.     public function hasFilter(ControllerEvent $event)
  27.     {
  28.         $query $event->getRequest()->query;
  29.         $resource $event->getRequest()->attributes->get('_api_resource_class');
  30.         if (Participation::class !== $resource) {
  31.             return;
  32.         }
  33.         if (!$query instanceof ParameterBag) {
  34.             return;
  35.         }
  36.         if(is_null($query->get('operation_company_companyID'))){
  37.             throw new AccessDeniedHttpException('Can\'t request this route without company filter');
  38.         }
  39.     }
  40. }