src/EventSubscriber/CustomUserSubscriber.php line 26

Open in your IDE?
  1. <?php
  2. // src/EventSubscriber/RequestSubscriber.php
  3. namespace App\EventSubscriber;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  6. use Symfony\Component\Security\Core\Security;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Component\HttpFoundation\RedirectResponse;
  9. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  10. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  11. class CustomUserSubscriber implements EventSubscriberInterface
  12. {
  13.     protected $security;
  14.     public function __construct(Security $securityEntityManagerInterface $entityManagerUrlGeneratorInterface $urlGenerator)
  15.     {
  16.         $this->security $security;
  17.         $this->entityManager $entityManager;
  18.         $this->urlGenerator $urlGenerator;
  19.     }
  20.     public function onKernelController(ControllerEvent $event)
  21.     {               
  22.         $user $this->security->getUser();
  23.         
  24.         $route $event->getRequest()->attributes->get('_route');
  25.         
  26.         if($event->getRequest()->attributes->get('route')!=null){
  27.             $route $event->getRequest()->attributes->get('route'); 
  28.         }
  29.         // Check if policies have been accepted
  30.         // TO DO - check if can move this to RequestSubscriber
  31.         if ($user && $route!='user_policies' && $route!='partner_questionnaire' && $route!='notifications_shownotification' && $route!='app_login' && $route!=null
  32.                 && (!$user->getacceptedPolicies() || !$user->getAcceptedCookies() || !$user->getAcceptedTerms())) {
  33.             
  34.            
  35.             throw new AccessDeniedHttpException();
  36.         }           
  37.     }
  38.     public static function getSubscribedEvents()
  39.     {
  40.         return array(
  41.             'kernel.controller' => 'onKernelController',
  42.         );
  43.     }
  44. }