src/Listener/KernelListener.php line 36

Open in your IDE?
  1. <?php
  2. /*
  3.  * Eventfix - KernelListener.php
  4.  * --------------------------------------------------------------------------
  5.  * Created by: mhack
  6.  * Created on: 17.1.2024
  7.  * --------------------------------------------------------------------------
  8.  * Copyright (c) 2024 | Michael Hack Software e.K. | www.mh-s.de
  9.  */
  10. namespace App\Listener;
  11. use App\Entity\User;
  12. use App\Service\Settings;
  13. use Symfony\Component\HttpFoundation\RedirectResponse;
  14. use Symfony\Component\HttpKernel\Event\RequestEvent;
  15. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  16. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  17. class KernelListener {
  18.     private Settings $settings;
  19.     private UrlGeneratorInterface $url;
  20.     private TokenStorageInterface $tokenStorage;
  21.     public function __construct(Settings $settingsUrlGeneratorInterface $urlTokenStorageInterface $tokenStorage) {
  22.         $this->settings $settings;
  23.         $this->url          $url;
  24.         $this->tokenStorage $tokenStorage;
  25.     }
  26.     public function onKernelRequest(RequestEvent $request_event) {
  27.         if (!$request_event->isMainRequest()) return;
  28.         // Token - Anmeldung muss vorhanden sein
  29.         if (!$token $this->tokenStorage->getToken()) {
  30.             return;
  31.         }
  32.         // Nur Benutzer behandeln
  33.         $user $token->getUser();
  34.         if (!$user instanceof User) {
  35.             return;
  36.         }
  37.         // Route ermitteln
  38.         $request $request_event->getRequest();
  39.         $route   $request->attributes->get('_route');
  40.         // Status des Benutzers prüfen
  41.         switch ($user->getState()) {
  42.             case User::STATE_ACTIVE:
  43.                 // Gesperrte Benutzer umleiten
  44.                 if ($user->isLocked()) {
  45.                     if ($route != 'security.locked') {
  46.                         $request_event->setResponse(new RedirectResponse($this->url->generate('security.locked')));
  47.                     }
  48.                     return;
  49.                 }
  50.                 // Benutzer muss ein Profil haben
  51.                 $profile $user->getProfile();
  52.                 if (!$profile || !$profile->isActive()) {
  53.                     if (str_starts_with($route'backend.main')) {
  54.                         $request_event->setResponse(new RedirectResponse($this->url->generate('backend.assistant.index')));
  55.                         return;
  56.                     }
  57.                 }
  58.                 // Ein aktives, gebuchtes Paket muss vorhanden sein
  59.                 if (!$user->isAdmin() && !$user->getActivePlan()) {
  60.                     if ($this->settings->getInt('plan.active')) {
  61.                         if (!str_starts_with($route'backend.main.settings')) {
  62.                             $request_event->setResponse(new RedirectResponse($this->url->generate('backend.main.settings', ['module' => 'konto''p1' => 'pakete'])));
  63.                             return;
  64.                         }
  65.                     }
  66.                 }
  67.                 break;
  68.             case User::STATE_DELETED:
  69.                 if (!str_starts_with($route'backend.archive')) {
  70.                     $request_event->setResponse(new RedirectResponse($this->url->generate('backend.archive.index')));
  71.                     return;
  72.                 }
  73.                 break;
  74.         }
  75.     }
  76. }