src/Social/UserBundle/Listener/ActivityListener.php line 107

Open in your IDE?
  1. <?php
  2. namespace Social\UserBundle\Listener;
  3. use Social\UserBundle\Entity\User;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Social\FrontendBundle\Service\Tools;
  6. use Symfony\Component\Routing\RouterInterface;
  7. use Social\UserBundle\Service\UserTrackingHandler;
  8. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  9. use Symfony\Component\DependencyInjection\ContainerInterface;
  10. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  11. use Sentry\Client as SentryClient;
  12. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  13. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  14. /**
  15.  * Class ActivityListener
  16.  *
  17.  * @package Social\UserBundle\Listener
  18.  */
  19. class ActivityListener
  20. {
  21.     /**
  22.      * @var TokenStorageInterface $context
  23.      */
  24.     protected $context;
  25.     /**
  26.      * @var ContainerInterface $service_container
  27.      */
  28.     protected $service_container;
  29.     /**
  30.      * @var EntityManagerInterface $em
  31.      */
  32.     protected $em;
  33.     /**
  34.      * @var RouterInterface $router
  35.      */
  36.     protected $router;
  37.     /**
  38.      * @var Tools $tools
  39.      */
  40.     protected $tools;
  41.     /**
  42.      * @var UserTrackingHandler $userTracking
  43.      */
  44.     protected $userTracking;
  45.     /**
  46.      * @var string|null $backOfficePaymentAllowedHost
  47.      */
  48.     protected $backOfficePaymentAllowedHost;
  49.     /**
  50.      * @var array $frontAllowedHosts
  51.      */
  52.     protected $frontAllowedHosts;
  53.     /**
  54.      * @var SentryClient $sentryClient
  55.      */
  56.     protected $sentryClient;
  57.     /**
  58.      * ActivityListener constructor.
  59.      *
  60.      * @param TokenStorageInterface  $context
  61.      * @param EntityManagerInterface $entityManager
  62.      * @param RouterInterface        $router
  63.      * @param Tools                  $tools
  64.      * @param UserTrackingHandler    $trackingHandler
  65.      * @param SentryClient           $client
  66.      * @param string                 $frontAllowedHosts
  67.      * @param string|null            $backoffice_payment_allowed_host
  68.      */
  69.     public function __construct(
  70.         TokenStorageInterface $context,
  71.         EntityManagerInterface $entityManager,
  72.         RouterInterface $router,
  73.         Tools $tools,
  74.         UserTrackingHandler $trackingHandler,
  75.         SentryClient $client,
  76.         string $frontAllowedHosts,
  77.         string $backoffice_payment_allowed_host null
  78.     ) {
  79.         $this->context      $context;
  80.         $this->em           $entityManager;
  81.         $this->router       $router;
  82.         $this->tools        $tools;
  83.         $this->userTracking $trackingHandler;
  84.         $this->sentryClient $client;
  85.         $this->backOfficePaymentAllowedHost $backoffice_payment_allowed_host;
  86.         $this->frontAllowedHosts            explode(','$frontAllowedHosts);
  87.     }
  88.     /**
  89.      * @param FilterControllerEvent $event
  90.      */
  91.     public function onCoreController(FilterControllerEvent $event)
  92.     {
  93.         try {
  94.             $request $event->getRequest();
  95.             $route   $request->get('_route');
  96.             $host    $request->getHost();
  97.             $session $request->getSession();
  98.             if (in_array($route, [
  99.                 'social_frontend_payment_backoffice_confirmation_payment',
  100.                 'social_frontend_payment_backoffice_confirmation_alert',
  101.             ])) {
  102.                 if ($host != $this->backOfficePaymentAllowedHost) {
  103.                     throw new NotFoundHttpException(sprintf('backend host not allowed "%s"'$host));
  104.                 }
  105.             } else {
  106.                 if (!in_array($host$this->frontAllowedHosts)) {
  107.                     throw new NotFoundHttpException(sprintf('frontend host not allowed "%s"'$host));
  108.                 }
  109.             }
  110.             if (!$this->context->getToken()) {
  111.                 return;
  112.             }
  113.             // @todo - is this used anymore?
  114. //        if(!in_array($this->tools->getUserIp(), $this->service_container->getParameter('backend_allowed_ips')) && strstr($route, 'admin') !== false) {
  115. //            throw new AccessDeniedException('you are not allowed to access the admin: ' . $this->tools->getUserIp());
  116. //        }
  117.             /* @todo - talk with alex and see !!! */
  118. /*            if (isset($_SERVER["HTTP_CF_IPCOUNTRY"])) {
  119.                 $countryCode = $_SERVER["HTTP_CF_IPCOUNTRY"];
  120.                 if (in_array($countryCode,
  121.                         $this->service_container->getParameter('exclude_access_to_countries')) && !in_array(
  122.                         $this->tools->getUserIp(),
  123.                         $this->service_container->getParameter('backend_allowed_ips')
  124.                     ) && $route != 'social_block_user'
  125.                 ) {
  126.                     throw new AccessDeniedException('accessed from a forbidden country: ' . $countryCode . ' with IP : ' . $_SERVER["HTTP_CF_CONNECTING_IP"] . ' / ' . $this->tools->getUserIp());
  127.                 }
  128.             }*/
  129.             $user $this->context->getToken()->getUser();
  130.             if ($user instanceof User) {
  131.                 //here we can update the user as necessary
  132.                 $user->setLastActivity(new \DateTime())
  133.                     ->setRealIsOnline(true)
  134.                      ->setIsOnline(true);
  135.                 $owner $user->getOwner();
  136.                 if ($user->getIsFake() && $owner instanceof User) {
  137.                     $owner->setIsOnline(true)
  138.                           ->setRealIsOnline(false)
  139.                           ->setLastActivity(new \DateTime());
  140.                     $this->em->persist($owner);
  141.                 } else {
  142.                     if ($user->hasRole(User::ROLE_STAFF)) {
  143.                     }
  144.                 }
  145.                 if ($event->isMasterRequest()) {
  146.                     $this->userTracking->checkIfItHasBeenTracked($user$session);
  147.                 }
  148.                 $this->em->persist($user);
  149.                 $this->em->flush();
  150.             }
  151.         } catch (\Exception $exception) {
  152.             $this->sentryClient->captureException($exception);
  153.         }
  154.     }
  155. }