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

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sentry\SentryBundle\EventListener;
  4. use Sentry\State\HubInterface;
  5. use Sentry\State\Scope;
  6. use Sentry\UserDataBag;
  7. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. /**
  10.  * This listener ensures that a new {@see \Sentry\State\Scope} is created for
  11.  * each request and that it is filled with useful information, e.g. the IP
  12.  * address of the client.
  13.  */
  14. final class RequestListener
  15. {
  16.     use KernelEventForwardCompatibilityTrait;
  17.     /**
  18.      * @var HubInterface The current hub
  19.      */
  20.     private $hub;
  21.     /**
  22.      * @var TokenStorageInterface|null The token storage
  23.      */
  24.     private $tokenStorage;
  25.     /**
  26.      * Constructor.
  27.      *
  28.      * @param HubInterface               $hub          The current hub
  29.      * @param TokenStorageInterface|null $tokenStorage The token storage
  30.      */
  31.     public function __construct(HubInterface $hub, ?TokenStorageInterface $tokenStorage)
  32.     {
  33.         $this->hub $hub;
  34.         $this->tokenStorage $tokenStorage;
  35.     }
  36.     /**
  37.      * This method is called for each request handled by the framework and
  38.      * fills the Sentry scope with information about the current user.
  39.      *
  40.      * @param RequestListenerRequestEvent $event The event
  41.      */
  42.     public function handleKernelRequestEvent(RequestListenerRequestEvent $event): void
  43.     {
  44.         if (!$this->isMainRequest($event)) {
  45.             return;
  46.         }
  47.         $client $this->hub->getClient();
  48.         if (null === $client || !$client->getOptions()->shouldSendDefaultPii()) {
  49.             return;
  50.         }
  51.         $token null;
  52.         $userData = new UserDataBag();
  53.         $userData->setIpAddress($event->getRequest()->getClientIp());
  54.         if (null !== $this->tokenStorage) {
  55.             $token $this->tokenStorage->getToken();
  56.         }
  57.         if (null !== $token && $token->isAuthenticated() && null !== $token->getUser()) {
  58.             $userData->setUsername($this->getUsername($token->getUser()));
  59.         }
  60.         $this->hub->configureScope(static function (Scope $scope) use ($userData): void {
  61.             $scope->setUser($userData);
  62.         });
  63.     }
  64.     /**
  65.      * This method is called for each request handled by the framework and
  66.      * sets the route on the current Sentry scope.
  67.      *
  68.      * @param RequestListenerControllerEvent $event The event
  69.      */
  70.     public function handleKernelControllerEvent(RequestListenerControllerEvent $event): void
  71.     {
  72.         if (!$this->isMainRequest($event)) {
  73.             return;
  74.         }
  75.         $request $event->getRequest();
  76.         if (!$request->attributes->has('_route')) {
  77.             return;
  78.         }
  79.         $this->hub->configureScope(static function (Scope $scope) use ($request): void {
  80.             $scope->setTag('route', (string) $request->attributes->get('_route'));
  81.         });
  82.     }
  83.     /**
  84.      * @param UserInterface|object|string $user
  85.      */
  86.     private function getUsername($user): ?string
  87.     {
  88.         if ($user instanceof UserInterface) {
  89.             if (method_exists($user'getUserIdentifier')) {
  90.                 return $user->getUserIdentifier();
  91.             }
  92.             if (method_exists($user'getUsername')) {
  93.                 return $user->getUsername();
  94.             }
  95.         }
  96.         if (\is_string($user)) {
  97.             return $user;
  98.         }
  99.         if (\is_object($user) && method_exists($user'__toString')) {
  100.             return (string) $user;
  101.         }
  102.         return null;
  103.     }
  104. }