src/EventSubscriber/InternalMessengerMercureCookieSubscriber.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\User;
  4. use App\Service\InternalMessengerMercurePublisher;
  5. use App\Service\RightService;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. use Symfony\Component\Security\Core\Security;
  10. class InternalMessengerMercureCookieSubscriber implements EventSubscriberInterface
  11. {
  12.     public function __construct(
  13.         private Security $security,
  14.         private RightService $rightService,
  15.         private InternalMessengerMercurePublisher $mercurePublisher
  16.     ) {
  17.     }
  18.     public static function getSubscribedEvents(): array
  19.     {
  20.         return [
  21.             KernelEvents::RESPONSE => 'onKernelResponse',
  22.         ];
  23.     }
  24.     public function onKernelResponse(ResponseEvent $event): void
  25.     {
  26.         if (!$event->isMainRequest()) {
  27.             return;
  28.         }
  29.         $request $event->getRequest();
  30.         if (!str_starts_with($request->getPathInfo(), '/admin')) {
  31.             return;
  32.         }
  33.         $user $this->security->getUser();
  34.         if (!$user instanceof User) {
  35.             return;
  36.         }
  37.         if (!$this->security->isGranted('ROLE_SUPER_ADMIN')) {
  38.             $rights $this->rightService->getAllRights($user);
  39.             if (!in_array('INTERNAL_MESSENGER'$rightstrue)) {
  40.                 return;
  41.             }
  42.         }
  43.         $this->mercurePublisher->applySubscriberCookie($event->getResponse(), $user);
  44.     }
  45. }