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

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.     use KernelEventForwardCompatibilityTrait;
  35.     /** @var HubInterface */
  36.     private $hub;
  37.     /** @var TokenStorageInterface|null */
  38.     private $tokenStorage;
  39.     /**
  40.      * RequestListener constructor.
  41.      * @param HubInterface $hub
  42.      * @param TokenStorageInterface|null $tokenStorage
  43.      */
  44.     public function __construct(
  45.         HubInterface $hub,
  46.         ?TokenStorageInterface $tokenStorage
  47.     ) {
  48.         $this->hub $hub// not used, needed to trigger instantiation
  49.         $this->tokenStorage $tokenStorage;
  50.     }
  51.     /**
  52.      * Set the username from the security context by listening on core.request
  53.      *
  54.      * @param RequestListenerRequestEvent $event
  55.      */
  56.     public function onKernelRequest(RequestListenerRequestEvent $event): void
  57.     {
  58.         if (! $this->isMainRequest($event)) {
  59.             return;
  60.         }
  61.         $currentClient SentrySdk::getCurrentHub()->getClient();
  62.         if (null === $currentClient || ! $currentClient->getOptions()->shouldSendDefaultPii()) {
  63.             return;
  64.         }
  65.         $token null;
  66.         if ($this->tokenStorage instanceof TokenStorageInterface) {
  67.             $token $this->tokenStorage->getToken();
  68.         }
  69.         $userData = [];
  70.         if (
  71.             null !== $token
  72.             && $token->isAuthenticated()
  73.             && $token->getUser()
  74.         ) {
  75.             $userData $this->getUserData($token->getUser());
  76.         }
  77.         $userData['ip_address'] = $event->getRequest()->getClientIp();
  78.         SentrySdk::getCurrentHub()
  79.             ->configureScope(function (Scope $scope) use ($userData): void {
  80.                 $scope->setUser($userDatatrue);
  81.             });
  82.     }
  83.     public function onKernelController(RequestListenerControllerEvent $event): void
  84.     {
  85.         if (! $this->isMainRequest($event)) {
  86.             return;
  87.         }
  88.         if (! $event->getRequest()->attributes->has('_route')) {
  89.             return;
  90.         }
  91.         $matchedRoute = (string) $event->getRequest()->attributes->get('_route');
  92.         SentrySdk::getCurrentHub()
  93.             ->configureScope(function (Scope $scope) use ($matchedRoute): void {
  94.                 $scope->setTag('route'$matchedRoute);
  95.             });
  96.     }
  97.     /**
  98.      * @param UserInterface | object | string $user
  99.      * @return array<string, string>
  100.      */
  101.     private function getUserData($user): array
  102.     {
  103.         if ($user instanceof UserInterface) {
  104.             return [
  105.                 'username' => method_exists($user'getUserIdentifier') ? $user->getUserIdentifier() : $user->getUsername(),
  106.             ];
  107.         }
  108.         if (is_string($user)) {
  109.             return [
  110.                 'username' => $user,
  111.             ];
  112.         }
  113.         if (is_object($user) && method_exists($user'__toString')) {
  114.             return [
  115.                 'username' => $user->__toString(),
  116.             ];
  117.         }
  118.         return [];
  119.     }
  120. }