<?php
/*
* Eventfix - KernelListener.php
* --------------------------------------------------------------------------
* Created by: mhack
* Created on: 17.1.2024
* --------------------------------------------------------------------------
* Copyright (c) 2024 | Michael Hack Software e.K. | www.mh-s.de
*/
namespace App\Listener;
use App\Entity\User;
use App\Service\Settings;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class KernelListener {
private Settings $settings;
private UrlGeneratorInterface $url;
private TokenStorageInterface $tokenStorage;
public function __construct(Settings $settings, UrlGeneratorInterface $url, TokenStorageInterface $tokenStorage) {
$this->settings = $settings;
$this->url = $url;
$this->tokenStorage = $tokenStorage;
}
public function onKernelRequest(RequestEvent $request_event) {
if (!$request_event->isMainRequest()) return;
// Token - Anmeldung muss vorhanden sein
if (!$token = $this->tokenStorage->getToken()) {
return;
}
// Nur Benutzer behandeln
$user = $token->getUser();
if (!$user instanceof User) {
return;
}
// Route ermitteln
$request = $request_event->getRequest();
$route = $request->attributes->get('_route');
// Status des Benutzers prüfen
switch ($user->getState()) {
case User::STATE_ACTIVE:
// Gesperrte Benutzer umleiten
if ($user->isLocked()) {
if ($route != 'security.locked') {
$request_event->setResponse(new RedirectResponse($this->url->generate('security.locked')));
}
return;
}
// Benutzer muss ein Profil haben
$profile = $user->getProfile();
if (!$profile || !$profile->isActive()) {
if (str_starts_with($route, 'backend.main')) {
$request_event->setResponse(new RedirectResponse($this->url->generate('backend.assistant.index')));
return;
}
}
// Ein aktives, gebuchtes Paket muss vorhanden sein
if (!$user->isAdmin() && !$user->getActivePlan()) {
if ($this->settings->getInt('plan.active')) {
if (!str_starts_with($route, 'backend.main.settings')) {
$request_event->setResponse(new RedirectResponse($this->url->generate('backend.main.settings', ['module' => 'konto', 'p1' => 'pakete'])));
return;
}
}
}
break;
case User::STATE_DELETED:
if (!str_starts_with($route, 'backend.archive')) {
$request_event->setResponse(new RedirectResponse($this->url->generate('backend.archive.index')));
return;
}
break;
}
}
}