src/EventListener/BlockedOrganizationListener.php line 42

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\EventListener;
  4. use App\Entity\User;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  7. use Symfony\Component\HttpKernel\Event\RequestEvent;
  8. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  9. use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
  10. use Symfony\Component\Security\Core\Event\AuthenticationSuccessEvent;
  11. use Symfony\Component\Security\Core\Security;
  12. use Symfony\Component\Security\Csrf\TokenStorage\TokenStorageInterface;
  13. class BlockedOrganizationListener
  14. {
  15.     /**
  16.      * @var Security
  17.      */
  18.     private $security;
  19.     /**
  20.      * @var UrlGeneratorInterface
  21.      */
  22.     private $router;
  23.     /**
  24.      * @param Security              $security
  25.      * @param UrlGeneratorInterface $router
  26.      */
  27.     public function __construct(Security $securityUrlGeneratorInterface $router)
  28.     {
  29.         $this->router $router;
  30.         $this->security $security;
  31.     }
  32.     /**
  33.      * @param RequestEvent $event
  34.      */
  35.     public function onKernelRequest(RequestEvent $event): void
  36.     {
  37.         $request $event->getRequest();
  38.         if (\in_array($request->get('_route'), ['profile_organization_blocked''user_logout'])) {
  39.             return;
  40.         }
  41.         if (!$event->isMasterRequest()
  42.             || (null === $this->security->getToken())
  43.             || ($this->security->getToken() instanceof AnonymousToken)
  44.             || !($this->security->getUser() instanceof User)) {
  45.             // don't do anything if it's not the master request
  46.             return;
  47.         }
  48.         /** @var User $user */
  49.         $user $this->security->getUser();
  50.         if (($user->getOrganization() && $user->getOrganization()->isActive())
  51.             || ($user->isSystemUser())) {
  52.             // don't do anything if users organization is active
  53.             return;
  54.         }
  55.         $response = new RedirectResponse($this->router->generate('profile_organization_blocked'));
  56.         $event->setResponse($response);
  57.     }
  58. }