src/Subscriber/LoginSubscriber.php line 55

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Subscriber;
  4. use App\Controller\AuthController;
  5. use App\Controller\UserController;
  6. use App\Logger\Log;
  7. use App\Service\FileService;
  8. use App\Service\IAppService;
  9. use App\Service\IAuthService;
  10. use App\Service\IOptionService;
  11. use App\Service\OptionService;
  12. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpFoundation\JsonResponse;
  15. use Symfony\Component\HttpFoundation\RedirectResponse;
  16. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  17. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  18. use Symfony\Component\HttpKernel\KernelEvents;
  19. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  20. use Symfony\Component\Security\Core\Security;
  21. use Symfony\Contracts\Translation\TranslatorInterface;
  22. class LoginSubscriber implements EventSubscriberInterface
  23. {
  24.     private ParameterBagInterface $parameterBag;
  25.     private TranslatorInterface $translator;
  26.     private SessionInterface $session;
  27.     private Security $security;
  28.     protected TokenStorageInterface $securityToken;
  29.     protected Log $log;
  30.     protected FileService $fileService;
  31.     private OptionService $optionService;
  32.     public function __construct(
  33.         ParameterBagInterface $parameterBag,
  34.         SessionInterface $session,
  35.         Security $security,
  36.         TokenStorageInterface $securityToken,
  37.         Log $log,
  38.         FileService $fileService,
  39.         TranslatorInterface $translator,
  40.         OptionService $optionService
  41.     ) {
  42.         $this->session $session;
  43.         $this->translator $translator;
  44.         $this->security $security;
  45.         $this->securityToken $securityToken;
  46.         $this->fileService $fileService;
  47.         $this->log $log;
  48.         $this->parameterBag $parameterBag;
  49.         $this->optionService $optionService;
  50.     }
  51.     public function onKernelController(ControllerEvent $event)
  52.     {
  53.         $controller $event->getController();
  54.         if (!is_array($controller)) {
  55.             return;
  56.         }
  57.         if ($this->fileService->checkFile($this->parameterBag->get('app.connection_dir').DIRECTORY_SEPARATOR.IOptionService::MAINTENANCE_FILE) && IAuthService::STAFF != $this->parameterBag->get('app.user')) {
  58.             $this->log->writeLog(
  59.                 [
  60.                     IOptionService::KEY_CLASS => get_class($this),
  61.                     IOptionService::KEY_FUNCTION => __FUNCTION__,
  62.                     IOptionService::KEY_MESSAGE => 'site is under maintenance',
  63.                 ],
  64.                 Log::MESSAGE_INFO
  65.             );
  66.             if (null !== $this->security->getUser()) {
  67.                 // clean session i think is not working
  68.                 $session $event->getRequest()->getSession();
  69.                 $session->invalidate();
  70.                 $session->clear();
  71.                 // clean user activity
  72.                 $this->securityToken->setToken(null);
  73.                 // redirect to maintenance page
  74.                 $this->redirectionMaintenance($event);
  75.             } elseif ('maintenance' != $event->getRequest()->attributes->get('_route')) {
  76.                 $this->redirectionMaintenance($event);
  77.             }
  78.         } elseif (
  79.             $controller[0] instanceof AuthController
  80.             && null !== $this->security->getUser()
  81.             && (
  82.                 'auth_signin' == $event->getRequest()->attributes->get('_route')
  83.                 || 'auth_forgot_password' == $event->getRequest()->attributes->get('_route')
  84.                 || 'auth_reset_password' == $event->getRequest()->attributes->get('_route')
  85.             )
  86.         ) {
  87.             $this->redirectionHome($event);
  88.         } elseif (
  89.             $controller[0] instanceof AuthController
  90.             && 'new_account' != $event->getRequest()->attributes->get('_route')
  91.             && 'auth_activation' != $event->getRequest()->attributes->get('_route')
  92.             && 'auth_validate_account' != $event->getRequest()->attributes->get('_route')
  93.             && 'auth_signout' != $event->getRequest()->attributes->get('_route')
  94.             && $event->getRequest()->attributes->get('_route') != "auth_resent_activation_code"
  95.         ) {
  96.             // dd($this->security->getUser());
  97.             if (null !== $this->security->getUser()) {
  98.                 $event->setController(function () {
  99.                     return new RedirectResponse(IAppService::INDEX_URL);
  100.                 });
  101.             }
  102.         }
  103.     }
  104.     public static function getSubscribedEvents()
  105.     {
  106.         return [
  107.             KernelEvents::CONTROLLER => 'onKernelController',
  108.         ];
  109.     }
  110.     public function redirectionMaintenance(ControllerEvent $event)
  111.     {
  112.         if ($event->getRequest()->isMethod('POST') && $event->getRequest()->isXmlHttpRequest()) {
  113.             if (!$event->getController() instanceof AuthController) {
  114.                 $event->setController(function () {
  115.                     return new JsonResponse([
  116.                         'redirect' => $this->parameterBag->get('app.url').'maintenance',
  117.                     ], 200);
  118.                 });
  119.             } else {
  120.                 $event->setController(function () {
  121.                     return new JsonResponse('maintenance'200);
  122.                 });
  123.             }
  124.         } else {
  125.             $event->setController(function () {
  126.                 return new RedirectResponse($this->parameterBag->get('app.url').'maintenance');
  127.             });
  128.         }
  129.     }
  130.     public function redirectionHome(ControllerEvent $event)
  131.     {
  132.         if ($event->getRequest()->isMethod('POST') && $event->getRequest()->isXmlHttpRequest()) {
  133.             if (!$event->getController() instanceof AuthController) {
  134.                 $event->setController(function () {
  135.                     return new JsonResponse([
  136.                         'redirect' => $this->getLastUrl(),
  137.                     ], 200);
  138.                 });
  139.             } else {
  140.                 // $event->setController(function () {
  141.                 //     return new JsonResponse('HOME', 200);
  142.                 // });
  143.             }
  144.         } else {
  145.             $event->setController(function () {
  146.                 return new RedirectResponse($this->getLastUrl());
  147.             });
  148.         }
  149.     }
  150.     private function getLastUrl()
  151.     {
  152.         $lastUrl $this->parameterBag->get('app.url') . IAppService::INDEX_URL;
  153.         if (null !== $this->session->get(IAppService::SESSION_URL)) {
  154.             $lastUrl $this->session->get(IAppService::SESSION_URL)['page'];
  155.         }
  156.         return $lastUrl;
  157.     }
  158. }