<?php
namespace Social\UserBundle\Listener;
use Sentry\ClientInterface;
use Social\UserBundle\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Social\FrontendBundle\Service\Tools;
use Symfony\Component\Routing\RouterInterface;
use Social\UserBundle\Service\UserTrackingHandler;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
/**
* Class ActivityListener
*
* @package Social\UserBundle\Listener
*/
class ActivityListener
{
/**
* @var TokenStorageInterface $context
*/
protected $context;
/**
* @var ContainerInterface $service_container
*/
protected $service_container;
/**
* @var EntityManagerInterface $em
*/
protected $em;
/**
* @var RouterInterface $router
*/
protected $router;
/**
* @var Tools $tools
*/
protected $tools;
/**
* @var UserTrackingHandler $userTracking
*/
protected $userTracking;
/**
* @var string|null $backOfficePaymentAllowedHost
*/
protected $backOfficePaymentAllowedHost;
/**
* @var array $frontAllowedHosts
*/
protected $frontAllowedHosts;
/**
* @var ClientInterface $sentryClient
*/
protected $sentryClient;
/**
* ActivityListener constructor.
*
* @param TokenStorageInterface $context
* @param EntityManagerInterface $entityManager
* @param RouterInterface $router
* @param Tools $tools
* @param UserTrackingHandler $trackingHandler
* @param ClientInterface $client
* @param string $frontAllowedHosts
* @param string|null $backoffice_payment_allowed_host
*/
public function __construct(
TokenStorageInterface $context,
EntityManagerInterface $entityManager,
RouterInterface $router,
Tools $tools,
UserTrackingHandler $trackingHandler,
ClientInterface $client,
string $frontAllowedHosts,
string $backoffice_payment_allowed_host = null
) {
$this->context = $context;
$this->em = $entityManager;
$this->router = $router;
$this->tools = $tools;
$this->userTracking = $trackingHandler;
$this->sentryClient = $client;
$this->backOfficePaymentAllowedHost = $backoffice_payment_allowed_host;
$this->frontAllowedHosts = explode(',', $frontAllowedHosts);
}
/**
* @param FilterControllerEvent $event
*/
public function onCoreController(FilterControllerEvent $event)
{
try {
$request = $event->getRequest();
$route = $request->get('_route');
$host = $request->getHost();
$session = $request->getSession();
if (in_array($route, [
'social_frontend_payment_backoffice_confirmation_payment',
'social_frontend_payment_backoffice_confirmation_alert',
])) {
if ($host != $this->backOfficePaymentAllowedHost) {
throw new NotFoundHttpException(sprintf('backend host not allowed "%s"', $host));
}
} else {
if (!in_array($host, $this->frontAllowedHosts)) {
throw new NotFoundHttpException(sprintf('frontend host not allowed "%s"', $host));
}
}
if (!$this->context->getToken()) {
return;
}
// @todo - is this used anymore?
// if(!in_array($this->tools->getUserIp(), $this->service_container->getParameter('backend_allowed_ips')) && strstr($route, 'admin') !== false) {
// throw new AccessDeniedException('you are not allowed to access the admin: ' . $this->tools->getUserIp());
// }
/* @todo - talk with alex and see !!! */
/* if (isset($_SERVER["HTTP_CF_IPCOUNTRY"])) {
$countryCode = $_SERVER["HTTP_CF_IPCOUNTRY"];
if (in_array($countryCode,
$this->service_container->getParameter('exclude_access_to_countries')) && !in_array(
$this->tools->getUserIp(),
$this->service_container->getParameter('backend_allowed_ips')
) && $route != 'social_block_user'
) {
throw new AccessDeniedException('accessed from a forbidden country: ' . $countryCode . ' with IP : ' . $_SERVER["HTTP_CF_CONNECTING_IP"] . ' / ' . $this->tools->getUserIp());
}
}*/
$user = $this->context->getToken()->getUser();
if ($user instanceof User) {
//here we can update the user as necessary
$user->setLastActivity(new \DateTime())
->setRealIsOnline(true)
->setIsOnline(true);
$owner = $user->getOwner();
if ($user->getIsFake() && $owner instanceof User) {
$owner->setIsOnline(true)
->setRealIsOnline(false)
->setLastActivity(new \DateTime());
$this->em->persist($owner);
} else {
if ($user->hasRole(User::ROLE_STAFF)) {
}
}
if ($event->isMasterRequest()) {
$this->userTracking->checkIfItHasBeenTracked($user, $session);
}
$this->em->persist($user);
$this->em->flush();
}
} catch (\Exception $exception) {
$this->sentryClient->captureException($exception);
}
}
}