src/Uplifted/BaseBundle/EventListener/UserEnabledListener.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Uplifted\BaseBundle\EventListener;
  3. use Symfony\Component\HttpKernel\Event\RequestEvent;
  4. use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
  5. use Symfony\Component\Security\Core\Security;
  6. class UserEnabledListener
  7. {
  8.     private $security;
  9.     public function __construct(Security $security)
  10.     {
  11.         $this->security $security;
  12.     }
  13.     public function onKernelRequest(RequestEvent $event)
  14.     {
  15.         // If it's not the master request, ignore
  16.         if (!$event->isMasterRequest()) {
  17.             return;
  18.         }
  19.         // Get the current user
  20.         $user $this->security->getUser();
  21.         // If user is logged in and not enabled, throw an AccessDeniedHttpException
  22.         if (
  23.             $user &&
  24.             !$user->isEnabled()
  25.         ) {
  26.             throw new UnauthorizedHttpException('User account is disabled.');
  27.         }
  28.     }
  29. }