src/EventSubscriber/PublisherRoleEventSubscriber.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Annotations\PublisherRole;
  4. use Doctrine\Common\Annotations\Reader;
  5. use http\Exception\RuntimeException;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  9. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  10. use Symfony\Component\Security\Core\Security;
  11. class PublisherRoleEventSubscriber implements EventSubscriberInterface
  12. {
  13.     private $annotationReader;
  14.     private $security;
  15.     private $user;
  16.     private $urlGenerator;
  17.     private ControllerEvent $event;
  18.     public function __construct(
  19.         Reader $annotationReader,
  20.         UrlGeneratorInterface $urlGenerator,
  21.         Security $security
  22.     )
  23.     {
  24.         $this->annotationReader $annotationReader;
  25.         $this->security $security;
  26.         $this->user $this->security->getUser();
  27.         $this->urlGenerator $urlGenerator;
  28.     }
  29.     public function onKernelController(ControllerEvent $event)
  30.     {
  31.         if (!$event->isMainRequest()) {
  32.             return;
  33.         }
  34.         $controllers $event->getController();
  35.         if (!is_array($controllers)) {
  36.             return;
  37.         }
  38.         $this->event $event;
  39.         $this->handleAnnotation($controllers);
  40.     }
  41.     private function handleAnnotation(iterable $controllers): void
  42.     {
  43.         list($controller$method) = $controllers;
  44.         try {
  45.             $controller = new \ReflectionClass($controller);
  46.         } catch (\ReflectionException $e) {
  47.             throw new RuntimeException('Failed to read annotation!');
  48.         }
  49.         $this->handleClassAnnotation($controller);
  50.         $this->handleMethodAnnotation($controller$method);
  51.     }
  52.     private function handleClassAnnotation(\ReflectionClass $controller): void
  53.     {
  54.         $annotation $this->annotationReader->getClassAnnotation($controllerPublisherRole::class);
  55.         if ($annotation instanceof PublisherRole) {
  56.             $this->do($annotation->role);
  57.         }
  58.     }
  59.     private function handleMethodAnnotation(\ReflectionClass $controllerstring $method): void
  60.     {
  61.         $method $controller->getMethod($method);
  62.         $annotation $this->annotationReader->getMethodAnnotation($methodPublisherRole::class);
  63.         if ($annotation instanceof PublisherRole) {
  64.             $this->do($annotation->role);
  65.         }
  66.     }
  67.     private function do(string $roles) {
  68.         $authorized false;
  69.         $roles_array array_map('trim'explode(","$roles));
  70.         if (in_array("ROLE_PUBLISHER"$roles_array)) {
  71.             if ($this->user->hasRole("ROLE_PUBLISHER")) {
  72.                 $authorized true;
  73.             }
  74.         }
  75.         if (!$authorized && in_array("ROLE_PROGRAM_ADMIN"$roles_array)) {
  76.             if ($this->user->hasRole("ROLE_PROGRAM_ADMIN")) {
  77.                 $authorized true;
  78.             }
  79.         }
  80.         if (!$authorized && in_array("ROLE_PROGRAM_PUBLISHER"$roles_array)) {
  81.             if ($this->user->hasRole("ROLE_PROGRAM_PUBLISHER")) {
  82.                 $authorized true;
  83.             }
  84.         }
  85.         if (!$authorized) {
  86.             if (
  87.                 $this->user->hasRole("ROLE_PUBLISHER") ||
  88.                 $this->user->hasRole("ROLE_PROGRAM_ADMIN") ||
  89.                 $this->user->hasRole("ROLE_PROGRAM_PUBLISHER")
  90.             ) {
  91.                 $this->event->setController(function() {
  92.                     return new RedirectResponse($this->urlGenerator->generate('dashboard'));
  93.                     //return new RedirectResponse($this->router->generate('login_' . $event->getRequest()->getLocale()));
  94.                 });
  95.             } else {
  96.                 $this->event->setController(function() {
  97.                     return new RedirectResponse($this->urlGenerator->generate('publisher_adhesion_form_page'));
  98.                     //return new RedirectResponse($this->router->generate('login_' . $event->getRequest()->getLocale()));
  99.                 });
  100.             }
  101.         }
  102.     }
  103.     public static function getSubscribedEvents()
  104.     {
  105.         return [
  106.             'kernel.controller' => 'onKernelController',
  107.         ];
  108.     }
  109. }