<?php
namespace App\EventSubscriber;
use App\Entity\User;
use App\Service\InternalMessengerMercurePublisher;
use App\Service\RightService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Security;
class InternalMessengerMercureCookieSubscriber implements EventSubscriberInterface
{
public function __construct(
private Security $security,
private RightService $rightService,
private InternalMessengerMercurePublisher $mercurePublisher
) {
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::RESPONSE => 'onKernelResponse',
];
}
public function onKernelResponse(ResponseEvent $event): void
{
if (!$event->isMainRequest()) {
return;
}
$request = $event->getRequest();
if (!str_starts_with($request->getPathInfo(), '/admin')) {
return;
}
$user = $this->security->getUser();
if (!$user instanceof User) {
return;
}
if (!$this->security->isGranted('ROLE_SUPER_ADMIN')) {
$rights = $this->rightService->getAllRights($user);
if (!in_array('INTERNAL_MESSENGER', $rights, true)) {
return;
}
}
$this->mercurePublisher->applySubscriberCookie($event->getResponse(), $user);
}
}