src/EventListener/AccessListener.php line 70

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\CommonData\Application;
  4. use App\Entity\CommonData\Subscription;
  5. use Doctrine\Common\Annotations\AnnotationReader;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Bundle\FrameworkBundle\Controller\RedirectController;
  8. use Symfony\Component\HttpFoundation\RedirectResponse;
  9. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  10. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  11. use Symfony\Component\HttpKernel\HttpKernel;
  12. use Symfony\Component\Routing\RouterInterface;
  13. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  14. /**
  15.  * Class AccessListener
  16.  *
  17.  * @package App\EventListener
  18.  */
  19. class AccessListener
  20. {
  21.     /**
  22.      * @var EntityManagerInterface
  23.      */
  24.     private $em;
  25.     /**
  26.      * @var TokenStorageInterface
  27.      */
  28.     private $security;
  29.     /**
  30.      * @var SessionInterface
  31.      */
  32.     private $session;
  33.     /**
  34.      * @var RouterInterface
  35.      */
  36.     private $router;
  37.     /**
  38.      * AccessListener constructor.
  39.      *
  40.      * @param EntityManagerInterface $em
  41.      * @param TokenStorageInterface  $security
  42.      * @param SessionInterface       $session
  43.      * @param RouterInterface        $router
  44.      */
  45.     public function __construct(
  46.         EntityManagerInterface $em,
  47.         TokenStorageInterface  $security,
  48.         SessionInterface       $session,
  49.         RouterInterface        $router
  50.     ) {
  51.         $this->em       $em;
  52.         $this->security $security;
  53.         $this->session  $session;
  54.         $this->router   $router;
  55.     }
  56.     /**
  57.      * @param FilterControllerEvent $event
  58.      *
  59.      * @return null
  60.      * @throws \Exception
  61.      */
  62.     public function onKernelController(FilterControllerEvent $event)
  63.     {
  64.         if ($event->getRequestType() !== HttpKernel::MASTER_REQUEST) {
  65.             return null;
  66.         }
  67.         if ($this->security->getToken()) {
  68.             $request $event->getRequest();
  69.             $user    $this
  70.                 ->security
  71.                 ->getToken()
  72.                 ->getUser();
  73.             if (strpos($request->getRequestUri(), '/app/') !== false &&
  74.                 strpos($request->getRequestUri(), '/admin/') === false &&
  75.                 strpos($request->getRequestUri(), '/api/') === false
  76.             ) {
  77.                 list($controller$methodName) = $event->getController();
  78.                 if (!($controller instanceof RedirectController)) {
  79.                     $annotationParams = (new AnnotationReader())
  80.                         ->getClassAnnotations(new \ReflectionClass($controller));
  81.                     $application      $this
  82.                         ->em
  83.                         ->getRepository(Application::class)
  84.                         ->findOneBy([
  85.                             'routePattern' => $annotationParams[0]->getName()
  86.                         ]);
  87.                     if ($application->getType() !== Application::APPLICATION_TYPES['free']) {
  88.                         $subscription $this
  89.                             ->em
  90.                             ->getRepository(Subscription::class)
  91.                             ->findOneBy([
  92.                                 'user'        => $user,
  93.                                 'application' => $application
  94.                             ]);
  95.                         if (!$subscription) {
  96.                             $this
  97.                                 ->session
  98.                                 ->getFlashBag()->add(
  99.                                     'error',
  100.                                     "You don't have subscription for " $application->getName() . "!"
  101.                                 );
  102.                             $redirectUrl $this
  103.                                 ->router
  104.                                 ->generate('mpsh_application_detail', [ 'id' => $application->getId() ]);
  105.                             $event->setController(function () use ($redirectUrl) {
  106.                                 return new RedirectResponse($redirectUrl);
  107.                             });
  108.                         } elseif ($subscription->getStatus() !== Subscription::SUBSCRIPTION_STATUSES['active']) {
  109.                             $this
  110.                                 ->session
  111.                                 ->getFlashBag()->add(
  112.                                     'error',
  113.                                     "Your subscription has expired for " $application->getName() . "!"
  114.                                 );
  115.                             $redirectUrl $this
  116.                                 ->router
  117.                                 ->generate('mpsh_profile_subscription');
  118.                             $event->setController(function () use ($redirectUrl) {
  119.                                 return new RedirectResponse($redirectUrl);
  120.                             });
  121.                         }
  122.                     }
  123.                     // ToDo: this part is wrong, rewrite it fully
  124.                     if (!$user->getEnabled()) {
  125.                         $redirectUrl $this
  126.                             ->router
  127.                             ->generate('app_logout');
  128.                         $event->setController(function () use ($redirectUrl) {
  129.                             return new RedirectResponse($redirectUrl);
  130.                         });
  131.                     }
  132.                 }
  133.             }
  134.         }
  135.     }
  136. }