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

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.     /**
  17.      * @var HubInterface The current hub
  18.      */
  19.     private $hub;
  20.     /**
  21.      * @var TokenStorageInterface|null The token storage
  22.      */
  23.     private $tokenStorage;
  24.     /**
  25.      * Constructor.
  26.      *
  27.      * @param HubInterface               $hub          The current hub
  28.      * @param TokenStorageInterface|null $tokenStorage The token storage
  29.      */
  30.     public function __construct(HubInterface $hub, ?TokenStorageInterface $tokenStorage)
  31.     {
  32.         $this->hub $hub;
  33.         $this->tokenStorage $tokenStorage;
  34.     }
  35.     /**
  36.      * This method is called for each request handled by the framework and
  37.      * fills the Sentry scope with information about the current user.
  38.      *
  39.      * @param RequestListenerRequestEvent $event The event
  40.      */
  41.     public function handleKernelRequestEvent(RequestListenerRequestEvent $event): void
  42.     {
  43.         if (!$event->isMasterRequest()) {
  44.             return;
  45.         }
  46.         $client $this->hub->getClient();
  47.         if (null === $client || !$client->getOptions()->shouldSendDefaultPii()) {
  48.             return;
  49.         }
  50.         $token null;
  51.         $userData UserDataBag::createFromUserIpAddress($event->getRequest()->getClientIp());
  52.         if (null !== $this->tokenStorage) {
  53.             $token $this->tokenStorage->getToken();
  54.         }
  55.         if (null !== $token && $token->isAuthenticated() && null !== $token->getUser()) {
  56.             $userData->setUsername($this->getUsername($token->getUser()));
  57.         }
  58.         $this->hub->configureScope(static function (Scope $scope) use ($userData): void {
  59.             $scope->setUser($userData);
  60.         });
  61.     }
  62.     /**
  63.      * This method is called for each request handled by the framework and
  64.      * sets the route on the current Sentry scope.
  65.      *
  66.      * @param RequestListenerControllerEvent $event The event
  67.      */
  68.     public function handleKernelControllerEvent(RequestListenerControllerEvent $event): void
  69.     {
  70.         if (!$event->isMasterRequest()) {
  71.             return;
  72.         }
  73.         $request $event->getRequest();
  74.         if (!$request->attributes->has('_route')) {
  75.             return;
  76.         }
  77.         $this->hub->configureScope(static function (Scope $scope) use ($request): void {
  78.             $scope->setTag('route', (string) $request->attributes->get('_route'));
  79.         });
  80.     }
  81.     /**
  82.      * @param UserInterface|object|string $user
  83.      */
  84.     private function getUsername($user): ?string
  85.     {
  86.         if ($user instanceof UserInterface) {
  87.             return $user->getUsername();
  88.         }
  89.         if (\is_string($user)) {
  90.             return $user;
  91.         }
  92.         if (\is_object($user) && method_exists($user'__toString')) {
  93.             return (string) $user;
  94.         }
  95.         return null;
  96.     }
  97. }