<?php
namespace App\Uplifted\BaseBundle\EventListener;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Symfony\Component\Security\Core\Security;
class UserEnabledListener
{
private $security;
public function __construct(Security $security)
{
$this->security = $security;
}
public function onKernelRequest(RequestEvent $event)
{
// If it's not the master request, ignore
if (!$event->isMasterRequest()) {
return;
}
// Get the current user
$user = $this->security->getUser();
// If user is logged in and not enabled, throw an AccessDeniedHttpException
if (
$user &&
!$user->isEnabled()
) {
throw new UnauthorizedHttpException('User account is disabled.');
}
}
}