vendor/sentry/sentry-symfony/src/EventListener/RequestListener.php line 97

Open in your IDE?
  1. <?php
  2. namespace Sentry\SentryBundle\EventListener;
  3. use Sentry\SentrySdk;
  4. use Sentry\State\HubInterface;
  5. use Sentry\State\Scope;
  6. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  7. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  8. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  9. use Symfony\Component\HttpKernel\Event\RequestEvent;
  10. use Symfony\Component\HttpKernel\Kernel;
  11. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  12. use Symfony\Component\Security\Core\User\UserInterface;
  13. if (Kernel::MAJOR_VERSION >= 5) {
  14.     if (! class_exists(RequestListenerRequestEvent::class, false)) {
  15.         class_alias(RequestEvent::class, RequestListenerRequestEvent::class);
  16.     }
  17.     if (! class_exists(RequestListenerControllerEvent::class, false)) {
  18.         class_alias(ControllerEvent::class, RequestListenerControllerEvent::class);
  19.     }
  20. } else {
  21.     if (! class_exists(RequestListenerRequestEvent::class, false)) {
  22.         class_alias(GetResponseEvent::class, RequestListenerRequestEvent::class);
  23.     }
  24.     if (! class_exists(RequestListenerControllerEvent::class, false)) {
  25.         class_alias(FilterControllerEvent::class, RequestListenerControllerEvent::class);
  26.     }
  27. }
  28. /**
  29.  * Class RequestListener
  30.  * @package Sentry\SentryBundle\EventListener
  31.  */
  32. final class RequestListener
  33. {
  34.     /** @var HubInterface */
  35.     private $hub;
  36.     /** @var TokenStorageInterface|null */
  37.     private $tokenStorage;
  38.     /**
  39.      * RequestListener constructor.
  40.      * @param HubInterface $hub
  41.      * @param TokenStorageInterface|null $tokenStorage
  42.      */
  43.     public function __construct(
  44.         HubInterface $hub,
  45.         ?TokenStorageInterface $tokenStorage
  46.     ) {
  47.         $this->hub $hub// not used, needed to trigger instantiation
  48.         $this->tokenStorage $tokenStorage;
  49.     }
  50.     /**
  51.      * Set the username from the security context by listening on core.request
  52.      *
  53.      * @param RequestListenerRequestEvent $event
  54.      */
  55.     public function onKernelRequest(RequestListenerRequestEvent $event): void
  56.     {
  57.         if (! $event->isMasterRequest()) {
  58.             return;
  59.         }
  60.         $currentClient SentrySdk::getCurrentHub()->getClient();
  61.         if (null === $currentClient || ! $currentClient->getOptions()->shouldSendDefaultPii()) {
  62.             return;
  63.         }
  64.         $token null;
  65.         if ($this->tokenStorage instanceof TokenStorageInterface) {
  66.             $token $this->tokenStorage->getToken();
  67.         }
  68.         $userData = [];
  69.         if (
  70.             null !== $token
  71.             && $token->isAuthenticated()
  72.             && $token->getUser()
  73.         ) {
  74.             $userData $this->getUserData($token->getUser());
  75.         }
  76.         $userData['ip_address'] = $event->getRequest()->getClientIp();
  77.         SentrySdk::getCurrentHub()
  78.             ->configureScope(function (Scope $scope) use ($userData): void {
  79.                 $scope->setUser($userDatatrue);
  80.             });
  81.     }
  82.     public function onKernelController(RequestListenerControllerEvent $event): void
  83.     {
  84.         if (! $event->isMasterRequest()) {
  85.             return;
  86.         }
  87.         if (! $event->getRequest()->attributes->has('_route')) {
  88.             return;
  89.         }
  90.         $matchedRoute = (string) $event->getRequest()->attributes->get('_route');
  91.         SentrySdk::getCurrentHub()
  92.             ->configureScope(function (Scope $scope) use ($matchedRoute): void {
  93.                 $scope->setTag('route'$matchedRoute);
  94.             });
  95.     }
  96.     /**
  97.      * @param UserInterface | object | string $user
  98.      * @return array<string, string>
  99.      */
  100.     private function getUserData($user): array
  101.     {
  102.         if ($user instanceof UserInterface) {
  103.             return [
  104.                 'username' => $user->getUsername(),
  105.             ];
  106.         }
  107.         if (is_string($user)) {
  108.             return [
  109.                 'username' => $user,
  110.             ];
  111.         }
  112.         if (is_object($user) && method_exists($user'__toString')) {
  113.             return [
  114.                 'username' => $user->__toString(),
  115.             ];
  116.         }
  117.         return [];
  118.     }
  119. }