vendor/elements/style-lab-bundle/src/EventSubscriber/UserSubscriber.php line 26

Open in your IDE?
  1. <?php
  2. namespace Elements\Bundle\StyleLabBundle\EventSubscriber;
  3. use Elements\Bundle\StyleLabBundle\Controller\AuthController;
  4. use Elements\Bundle\StyleLabBundle\Security\User\User;
  5. use JetBrains\PhpStorm\ArrayShape;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. use Symfony\Component\Security\Core\Security;
  10. class UserSubscriber implements EventSubscriberInterface
  11. {
  12.     private Security $security;
  13.     public function __construct(Security $security)
  14.     {
  15.         $this->security $security;
  16.     } //: __construct
  17.     /**
  18.      * @param ControllerEvent $event
  19.      * @return void
  20.      */
  21.     public function onKernelController(ControllerEvent $event): void
  22.     {
  23.         $controller $event->getController();
  24.         // when a controller class defines multiple action methods, the controller
  25.         // is returned as [$controllerInstance, 'methodName']
  26.         if (is_array($controller)) {
  27.             $controller $controller[0];
  28.         }
  29.         if ($controller instanceof AuthController) {
  30.             $user $this->security->getUser();
  31.             $adminUser false;
  32.             if ($user) {
  33.                 if (($user instanceof User && $user->getUsername() == 'stylelab') || (method_exists($user'getUser') && (strpos($user->getUser()->getEmail(), '@elements.at') !== false))) {
  34.                     $adminUser true;
  35.                 }
  36.             }
  37.             $event->getRequest()->attributes->set('isAdmin'$adminUser);
  38.         }
  39.     } //: onKernelController
  40.     /**
  41.      * @return string[]
  42.      */
  43.     #[ArrayShape( [ KernelEvents::CONTROLLER => "string" ] )]
  44.     public static function getSubscribedEvents(): array
  45.     {
  46.         return [
  47.             KernelEvents::CONTROLLER => 'onKernelController',
  48.         ];
  49.     } //: getSubscribedEvents
  50. }