<?php
namespace Boldr\Cms\MaintenanceUsersBundle;
use Boldr\Cms\MaintenanceBundle\Event\MaintenancePageEvent;
use Boldr\Cms\MaintenanceUsersBundle\Entity\UserOptions;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class EventSubscriber implements EventSubscriberInterface
{
private Security $security;
private EntityManagerInterface $em;
public function __construct(Security $security, EntityManagerInterface $em)
{
$this->security = $security;
$this->em = $em;
}
public static function getSubscribedEvents(): array
{
return [
MaintenancePageEvent::class => [
['onMaintenancePage', 10]
]
];
}
public function onMaintenancePage(MaintenancePageEvent $event)
{
$user = $this->security->getUser();
$userOptions = $user === null ? null : $this->em->getRepository(UserOptions::class)->find($user);
if ($userOptions && $userOptions->getBypassAllowed())
{
$event->setBypassAllowed(true);
}
}
}