src/Listeners/ControllerListener.php line 51

Open in your IDE?
  1. <?php
  2. namespace App\Listeners;
  3. use App\Annotation\Authorization;
  4. use App\Security\CallerAuthorizationService;
  5. use Doctrine\Common\Annotations\Reader;
  6. use Doctrine\Common\Util\ClassUtils;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController as Controller;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
  10. class ControllerListener
  11. {
  12.     /**
  13.      * @var Reader
  14.      */
  15.     protected $reader;
  16.     /**
  17.      * @var RequestStack
  18.      */
  19.     protected $requestStack;
  20.     /**
  21.      * @var CallerAuthorizationService
  22.      */
  23.     protected $callerAuthorizationService;
  24.     /**
  25.      * @param Reader                     $reader
  26.      * @param RequestStack               $requestStack
  27.      * @param CallerAuthorizationService $callerAuthorizationService
  28.      */
  29.     public function __construct(
  30.         Reader $reader,
  31.         RequestStack $requestStack,
  32.         CallerAuthorizationService $callerAuthorizationService
  33.     ) {
  34.         $this->reader $reader;
  35.         $this->requestStack $requestStack;
  36.         $this->callerAuthorizationService $callerAuthorizationService;
  37.     }
  38.     /**
  39.      * @param ControllerArgumentsEvent $event
  40.      *
  41.      * @throws \App\Exception\AccessDeniedException
  42.      * @throws \ReflectionException
  43.      */
  44.     public function onKernelControllerArguments(ControllerArgumentsEvent $event)
  45.     {
  46.         $controller $event->getController();
  47.         if (!is_array($controller)) {
  48.             return;
  49.         }
  50.         /** @var Controller $controllerObject */
  51.         list($controllerObject$methodName) = $controller;
  52.         $request $this->requestStack->getCurrentRequest();
  53.         if (null == $request) {
  54.             return;
  55.         }
  56.         $callerId $request->headers->get('X-CALLER-ID');
  57.         $callers $controllerObject;
  58.         $annotation Authorization::class;
  59.         $classAnnotation $this->reader->getClassAnnotation(
  60.             new \ReflectionClass(ClassUtils::getClass($controllerObject)),
  61.             $annotation
  62.         );
  63.         $controllerReflectionMethod = (new \ReflectionObject($controllerObject))->getMethod($methodName);
  64.         $methodAnnotation $this->reader->getMethodAnnotation($controllerReflectionMethod$annotation);
  65.         if ($classAnnotation || $methodAnnotation) {
  66.             $annotation $classAnnotation ?? $methodAnnotation;
  67.             if (null == $annotation) {
  68.                 return;
  69.             }
  70.             $callers $annotation->apps;
  71.             $this->callerAuthorizationService->checkCallerAuthorization(
  72.                 $callerId,
  73.                 $callers,
  74.                 $request->attributes->get('_route')
  75.             );
  76.         }
  77.     }
  78. }