src/EventSubscriber/TokenSubscriber.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Application\Application;
  4. use App\Controller\App\TokenAuthenticatedController;
  5. use App\Entity\Company;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use mysql_xdevapi\Exception;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpFoundation\RedirectResponse;
  10. use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
  11. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  12. use Symfony\Component\HttpKernel\KernelEvents;
  13. use Symfony\Component\HttpKernel\Event\ControllerEvent as FilterControllerEvent;
  14. class TokenSubscriber implements EventSubscriberInterface
  15. {
  16.     private $tokens;
  17.     private $em;
  18.     public function __construct(
  19.         EntityManagerInterface $entityManager
  20.     )
  21.     {
  22.         $this->em $entityManager;
  23.     }
  24.     public function onKernelController(FilterControllerEvent $event)
  25.     {
  26.         $controller $event->getController();
  27.         // when a controller class defines multiple action methods, the controller
  28.         // is returned as [$controllerInstance, 'methodName']
  29.         if (is_array($controller)) {
  30.             $controller $controller[0];
  31.         }
  32. /*
  33.         if (!$controller instanceof TokenAuthenticatedController) {
  34.             if(!Application::validLicense($this->em)){
  35.                 $redirectUrl='/access/402';
  36.                 $event->setController(function() use ($redirectUrl) {
  37.                     return new RedirectResponse($redirectUrl);
  38.                 });
  39.             }
  40.         }
  41.         /* */
  42.     }
  43.     public function onKernelResponse(FilterResponseEvent $event)
  44.     {
  45.         // check to see if onKernelController marked this as a token "auth'ed" request
  46.         if (!$token $event->getRequest()->attributes->get('auth_token')) {
  47.             return;
  48.         }
  49.         $response $event->getResponse();
  50.         // create a hash and set it as a response header
  51.         $hash sha1($response->getContent().$token);
  52.         $response->headers->set('X-CONTENT-HASH'$hash);
  53.     }
  54.     public static function getSubscribedEvents()
  55.     {
  56.         return [
  57.             KernelEvents::CONTROLLER => 'onKernelController',
  58.         ];
  59.     }
  60. }