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

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