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

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 UserDataBag::createFromUserIpAddress($event->getRequest()->getClientIp());
  53.         if (null !== $this->tokenStorage) {
  54.             $token $this->tokenStorage->getToken();
  55.         }
  56.         if (null !== $token && $token->isAuthenticated() && null !== $token->getUser()) {
  57.             $userData->setUsername($this->getUsername($token->getUser()));
  58.         }
  59.         $this->hub->configureScope(static function (Scope $scope) use ($userData): void {
  60.             $scope->setUser($userData);
  61.         });
  62.     }
  63.     /**
  64.      * This method is called for each request handled by the framework and
  65.      * sets the route on the current Sentry scope.
  66.      *
  67.      * @param RequestListenerControllerEvent $event The event
  68.      */
  69.     public function handleKernelControllerEvent(RequestListenerControllerEvent $event): void
  70.     {
  71.         if (!$this->isMainRequest($event)) {
  72.             return;
  73.         }
  74.         $request $event->getRequest();
  75.         if (!$request->attributes->has('_route')) {
  76.             return;
  77.         }
  78.         $this->hub->configureScope(static function (Scope $scope) use ($request): void {
  79.             $scope->setTag('route', (string) $request->attributes->get('_route'));
  80.         });
  81.     }
  82.     /**
  83.      * @param UserInterface|object|string $user
  84.      */
  85.     private function getUsername($user): ?string
  86.     {
  87.         if ($user instanceof UserInterface) {
  88.             return method_exists($user'getUserIdentifier') ? $user->getUserIdentifier() : $user->getUsername();
  89.         }
  90.         if (\is_string($user)) {
  91.             return $user;
  92.         }
  93.         if (\is_object($user) && method_exists($user'__toString')) {
  94.             return (string) $user;
  95.         }
  96.         return null;
  97.     }
  98. }