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

Open in your IDE?
  1. <?php
  2. namespace Sentry\SentryBundle\EventListener;
  3. use Sentry\State\Hub;
  4. use Sentry\State\HubInterface;
  5. use Sentry\State\Scope;
  6. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  7. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  8. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  9. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  10. use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;
  11. use Symfony\Component\Security\Core\User\UserInterface;
  12. /**
  13.  * Class RequestListener
  14.  * @package Sentry\SentryBundle\EventListener
  15.  */
  16. final class RequestListener
  17. {
  18.     /** @var HubInterface */
  19.     private $hub;
  20.     /** @var TokenStorageInterface|null */
  21.     private $tokenStorage;
  22.     /** @var AuthorizationCheckerInterface|null */
  23.     private $authorizationChecker;
  24.     /**
  25.      * RequestListener constructor.
  26.      * @param HubInterface $hub
  27.      * @param TokenStorageInterface|null $tokenStorage
  28.      * @param AuthorizationCheckerInterface|null $authorizationChecker
  29.      */
  30.     public function __construct(
  31.         HubInterface $hub,
  32.         ?TokenStorageInterface $tokenStorage,
  33.         ?AuthorizationCheckerInterface $authorizationChecker
  34.     ) {
  35.         $this->hub $hub// not used, needed to trigger instantiation
  36.         $this->tokenStorage $tokenStorage;
  37.         $this->authorizationChecker $authorizationChecker;
  38.     }
  39.     /**
  40.      * Set the username from the security context by listening on core.request
  41.      *
  42.      * @param GetResponseEvent $event
  43.      */
  44.     public function onKernelRequest(GetResponseEvent $event): void
  45.     {
  46.         if (! $event->isMasterRequest()) {
  47.             return;
  48.         }
  49.         $currentClient Hub::getCurrent()->getClient();
  50.         if (null === $currentClient || ! $currentClient->getOptions()->shouldSendDefaultPii()) {
  51.             return;
  52.         }
  53.         $token null;
  54.         if ($this->tokenStorage instanceof TokenStorageInterface) {
  55.             $token $this->tokenStorage->getToken();
  56.         }
  57.         if (
  58.             null !== $token
  59.             && null !== $this->authorizationChecker
  60.             && $token->isAuthenticated()
  61.             && $this->authorizationChecker->isGranted(AuthenticatedVoter::IS_AUTHENTICATED_REMEMBERED)
  62.         ) {
  63.             $userData $this->getUserData($token->getUser());
  64.         }
  65.         $userData['ip_address'] = $event->getRequest()->getClientIp();
  66.         Hub::getCurrent()
  67.             ->configureScope(function (Scope $scope) use ($userData): void {
  68.                 $scope->setUser($userData);
  69.             });
  70.     }
  71.     public function onKernelController(FilterControllerEvent $event): void
  72.     {
  73.         if (! $event->isMasterRequest()) {
  74.             return;
  75.         }
  76.         $matchedRoute $event->getRequest()->attributes->get('_route');
  77.         Hub::getCurrent()
  78.             ->configureScope(function (Scope $scope) use ($matchedRoute): void {
  79.                 $scope->setTag('route'$matchedRoute);
  80.             });
  81.     }
  82.     /**
  83.      * @param UserInterface | object | string $user
  84.      */
  85.     private function getUserData($user): array
  86.     {
  87.         if ($user instanceof UserInterface) {
  88.             return [
  89.                 'username' => $user->getUsername(),
  90.             ];
  91.         }
  92.         if (is_string($user)) {
  93.             return [
  94.                 'username' => $user,
  95.             ];
  96.         }
  97.         if (is_object($user) && method_exists($user'__toString')) {
  98.             return [
  99.                 'username' => $user->__toString(),
  100.             ];
  101.         }
  102.         return [];
  103.     }
  104. }