vendor/launchpad/backend/src/Base/EventListener/AppRequestListener.php line 155

Open in your IDE?
  1. <?php
  2. namespace LaunchPad\Bundle\LaunchPadBundle\Base\EventListener;
  3. use Doctrine\Common\Annotations\AnnotationException;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use LaunchPad\Bundle\LaunchPadBundle\Base\Serializer\IdCircularReferenceHandler;
  6. use LaunchPad\Bundle\LaunchPadBundle\Base\Service\Exception\MissingApiParamException;
  7. use LaunchPad\Bundle\LaunchPadBundle\Base\Service\User\UserPermissionService;
  8. use LaunchPad\Bundle\LaunchPadBundle\Base\Service\User\UserActionService;
  9. use LaunchPad\Bundle\LaunchPadBundle\Base\Service\User\UserSessionService;
  10. use LaunchPad\Bundle\LaunchPadBundle\Base\Service\UserDeviceService;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  13. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  14. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  15. use Symfony\Component\Serializer\Encoder\EncoderInterface;
  16. use Symfony\Component\Serializer\Encoder\JsonEncoder;
  17. use LaunchPad\Bundle\LaunchPadBundle\Api\Controller\BaseApiController;
  18. use LaunchPad\Bundle\LaunchPadBundle\Base\Service\BaseService;
  19. use Doctrine\Common\Annotations\AnnotationReader;
  20. use Symfony\Component\HttpFoundation\JsonResponse;
  21. use Symfony\Component\Serializer\Exception\ExceptionInterface;
  22. use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
  23. use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
  24. use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
  25. use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
  26. use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
  27. use Symfony\Component\Serializer\Serializer;
  28. use Symfony\Component\Serializer\SerializerInterface;
  29. class AppRequestListener
  30. {
  31.     /**
  32.      * @var BaseService
  33.      */
  34.     private $apiService;
  35.     /**
  36.      * @var ContainerInterface
  37.      */
  38.     private $container;
  39.     /**
  40.      * @var UserPermissionService
  41.      */
  42.     public $userPermissionService;
  43.     /**
  44.      * @var UserActionService
  45.      */
  46.     private $userActionService;
  47.     /**
  48.      * @var UserSessionService
  49.      */
  50.     private $userSessionService;
  51.     /**
  52.      * @var AuthorizationCheckerInterface
  53.      */
  54.     private $authChecker;
  55.     /**
  56.      * @var SerializerInterface
  57.      */
  58.     private $serializer;
  59.     /**
  60.      * @var UserDeviceService
  61.      */
  62.     private $userDeviceService;
  63.     /**
  64.      * @param ContainerInterface $container
  65.      * @param BaseService $apiService
  66.      * @param UserActionService $userActionService
  67.      * @param UserSessionService $userSessionService
  68.      * @param AuthorizationCheckerInterface $authChecker
  69.      * @param UserPermissionService $userPermissionService
  70.      * @throws AnnotationException
  71.      */
  72.     public function __construct(
  73.         ContainerInterface $container,
  74.         BaseService $apiService,
  75.         UserActionService $userActionService,
  76.         UserSessionService $userSessionService,
  77.         AuthorizationCheckerInterface $authChecker,
  78.         UserPermissionService $userPermissionService
  79.     ) {
  80.         $this->apiService $apiService;
  81.         $this->container $container;
  82.         $this->userPermissionService $userPermissionService;
  83.         $this->userActionService $userActionService;
  84.         $this->userSessionService $userSessionService;
  85.         $this->authChecker $authChecker;
  86.         $this->userDeviceService $this->container->get('lp.user.device');
  87.         $this->serializer = new Serializer([
  88.             new DateTimeNormalizer(),
  89.             new ObjectNormalizer(
  90.                 new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())),
  91.                 null,
  92.                 null,
  93.                 null,
  94.                 null,
  95.                 null,
  96.                 [
  97.                     ObjectNormalizer::CIRCULAR_REFERENCE_HANDLER => new IdCircularReferenceHandler(),
  98.                 ]
  99.             ),
  100.             new GetSetMethodNormalizer(),
  101.         ], [
  102.             new JsonEncoder(),
  103.         ]);
  104.     }
  105.     /**
  106.      * Return JSON output
  107.      *
  108.      * @param $data
  109.      * @param int $code
  110.      * @param array $groups
  111.      * @return JsonResponse
  112.      * @throws ExceptionInterface
  113.      */
  114.     private function jsonOutput($data$code 200$groups = [])
  115.     {
  116.         return new JsonResponse(
  117.             $this->serializer->serialize(
  118.                 $groups
  119.                     $this->serializer->normalize($data'json', ['groups' => $groups])
  120.                     : $data,
  121.                 'json',
  122.                 ['groups' => $groups]
  123.             ),
  124.             $code,
  125.             [],
  126.             true
  127.         );
  128.     }
  129.     /**
  130.      * @param ControllerEvent $event
  131.      * @throws MissingApiParamException
  132.      * @throws \Exception
  133.      */
  134.     public function onKernelController(ControllerEvent $event): void
  135.     {
  136.        // die('test');
  137.         $permissionRoute str_ireplace('/''.'$event->getRequest()->getRequestUri());
  138.         if (strpos($permissionRoute'.') === 0) {
  139.             $permissionRoute substr($permissionRoute1);
  140.         }
  141.         // $this->userPermissionService->checkPermission($permissionRoute);
  142.         $controller $event->getController();
  143.         if (!is_array($controller)) {
  144.             return;
  145.         }
  146.         $controllerName explode('\\'get_class($controller[0]));
  147.         $controllerName lcfirst(substr(end($controllerName), 0, -10));
  148.         // Check controller type
  149.         if (!($controller[0] instanceof BaseApiController)) {
  150.             return;
  151.         }
  152.         // Get controller action
  153.         $controllerAction $controller[1];
  154.         if (substr($controllerAction, -6) === 'Action') {
  155.             $controllerAction substr($controllerAction0, -6);
  156.         }
  157.         // Check mandatory params
  158.         $controller[0]->setData($this->apiService->extractParams($event->getRequest()));
  159.         if($this->authChecker->isGranted('ROLE_USER')){
  160.             $this->userActionService->newUserAction(
  161.                 $this->userSessionService->makeNewSession(),
  162.                 "{$controllerName}.{$controllerAction}",
  163.                 "API call"
  164.             );
  165.         }
  166.         $this->userDeviceService->handleDevice();
  167.     }
  168. }