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

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