src/EventSubscriber/StudioSubscriber.php line 91

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace App\EventSubscriber;
  11. use App\Admin\Entity\StdEmailsLog;
  12. use App\Cookie\Entity\StdCookies;
  13. use App\Event\StdDeleteUserDataEvent;
  14. use App\Service\StudioService;
  15. use App\Utils\Studio;
  16. use Doctrine\ORM\EntityManagerInterface;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Symfony\Component\HttpKernel\Event\RequestEvent;
  19. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  20. use Symfony\Component\HttpKernel\KernelEvents;
  21. use Symfony\Component\HttpFoundation\Cookie;
  22. use Symfony\Component\HttpFoundation\RequestStack;
  23. use Symfony\Component\Security\Core\Security;
  24. use Symfony\Component\HttpFoundation\Response;
  25. use App\Cookie\Helpers\StdCookieHelper;
  26. use App\Admin\Entity\StdWebUsers;
  27. use App\Event\StdWebUsersLoggedInEvent;
  28. class StudioSubscriber implements EventSubscriberInterface
  29. {
  30.     private $entityManager;
  31.     private $security;
  32.     private $session;
  33.     private $request;
  34.     private $studioService;
  35.     public function __construct(EntityManagerInterface $entityManagerSecurity $securityRequestStack $requestStackStudioService $studioService)
  36.     {
  37.         $this->entityManager $entityManager;
  38.         $this->security $security;
  39.         $this->request $requestStack->getCurrentRequest();
  40.         $this->studioService $studioService;
  41.     }
  42.     
  43.     public static function getSubscribedEvents(): array
  44.     {
  45.         return [
  46.             KernelEvents::REQUEST => ['onKernelRequest',10],
  47.             KernelEvents::RESPONSE => ['onKernelResponse'],
  48.             StdWebUsersLoggedInEvent::NAME => 'onUserLogin',
  49.             StdDeleteUserDataEvent::NAME => 'onDeleteUserData',
  50.         ];
  51.     }
  52.     
  53.     public function onKernelRequest(RequestEvent $event): void
  54.     {
  55.         global $studio;
  56.         
  57.         $this->session $event->getRequest()->getSession();
  58.         /*is_null($studio) && */$studio = new Studio($this->entityManager$this->security$this->session);
  59.         
  60.         //Validate Studio Features
  61.         $this->studioService->validateFeatures();
  62.         // Handle session stdCookie
  63.         if (getenv('COOKIE_DATABASE_ENABLED'))
  64.         {
  65.             if (!$this->session->has('stdCookie') && $event->getRequest()->cookies->get(getenv('COOKIE_STD_NAME'),'') != '')
  66.             {
  67.                 if ($cookie $this->entityManager->getRepository(StdCookies::class)->findOneBy(['uniqueKey'=>$event->getRequest()->cookies->get(getenv('COOKIE_STD_NAME'),'')]))
  68.                 {
  69.                     $expires = (new \DateTime())->add(new \DateInterval(getenv('COOKIE_STD_EXPIRE')));
  70.                     $cookie->setValidUntil($expires);
  71.                     $this->entityManager->persist($cookie);
  72.                     $this->entityManager->flush();
  73.                     $studio->setCookie($cookie);
  74.                     $this->session->set('setStdCookie'true);
  75.                 }
  76.             }elseif ($this->session->has('stdCookie')) {
  77.                 $studio->setCookie($this->session->get('stdCookie'));
  78.             }
  79.         }
  80.     }
  81.     public function onKernelResponse(ResponseEvent $event)
  82.     {        
  83.         if ($event->isMainRequest()
  84.             && ($stdCookie $event->getRequest()->getSession()->get('stdCookie')) 
  85.             && $event->getRequest()->getSession()->get('setStdCookie'false))
  86.         {
  87.             $event->getResponse()->headers->setCookie(\Symfony\Component\HttpFoundation\Cookie::create(getenv('COOKIE_STD_NAME'), $stdCookie->getUniqueKey(), $stdCookie->getValidUntil(), '/'nullfalsefalsefalsenull));
  88.             $event->getRequest()->getSession()->remove('setStdCookie');
  89.         }
  90.     }
  91.     
  92.     private function handleWebuserCookies(StdWebUsers $webUser, ?Response $response)
  93.     {
  94.         if (getenv('COOKIE_DATABASE_ENABLED')
  95.             && ($stdCookie StdCookieHelper::userLogin($webUser))
  96.             && $response)
  97.         {
  98.             $response->headers->setCookie(\Symfony\Component\HttpFoundation\Cookie::create(getenv('COOKIE_STD_NAME'), $stdCookie->getUniqueKey(), $stdCookie->getValidUntil(), '/'nullfalsefalsefalsenull));
  99.         }
  100.     }
  101.     public function onUserLogin(StdWebUsersLoggedInEvent $event)
  102.     {
  103.         $webUser $event->getWebUser();
  104.         $response $event->getResponse();
  105.         $this->handleWebuserCookies($webUser$response);
  106.     }
  107.     public function onDeleteUserData(StdDeleteUserDataEvent $event)
  108.     {
  109.         $address=$event->getAddress();
  110.         // find std_emails_log to delete
  111.         $logsToRemove=$this->entityManager->getRepository(StdEmailsLog::class)->findByToaddress($address);
  112.         foreach($logsToRemove as $log){
  113.             $this->entityManager->remove($log);
  114.         }
  115.         $this->entityManager->flush();
  116.     }
  117. }