<?php
namespace App\EventSubscriber;
use App\Security\AccessDeniedHandler;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class CheckUserListener implements EventSubscriberInterface
{
protected $authChecker;
protected $entityManager;
protected $tokenStorage;
public function __construct(AuthorizationCheckerInterface $authChecker, EntityManagerInterface $entityManager, TokenStorageInterface $tokenStorage)
{
$this->authChecker = $authChecker;
$this->entityManager = $entityManager;
$this->tokenStorage = $tokenStorage;
}
public function onKernelRequest()
{
if ($this->tokenStorage->getToken() != '' && $this->tokenStorage->getToken()->getUser()->getIsActive() == false) {
throw new AccessDeniedHttpException("Account is blocked");
}
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::CONTROLLER => 'onKernelRequest',
];
}
}