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

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