diff --git a/composer.json b/composer.json index 57cfeed6..dc1c5baa 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ "require": { "php": ">=5.3.3", "sentry/sentry": ">=0.18.0", - "symfony/symfony": ">=2.2.0" + "symfony/symfony": ">=2.4.0" }, "require-dev": { "fabpot/php-cs-fixer": "^1.8.0", diff --git a/src/Sentry/SentryBundle/EventListener/ExceptionListener.php b/src/Sentry/SentryBundle/EventListener/ExceptionListener.php index 9da01610..24e6691f 100644 --- a/src/Sentry/SentryBundle/EventListener/ExceptionListener.php +++ b/src/Sentry/SentryBundle/EventListener/ExceptionListener.php @@ -2,33 +2,106 @@ namespace Sentry\SentryBundle\EventListener; +use Symfony\Component\HttpKernel\Event\GetResponseEvent; use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; +use Symfony\Component\HttpKernel\HttpKernelInterface; +use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; +use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; +use Symfony\Component\Security\Core\User\UserInterface; +/** + * Class ExceptionListener + * @package Sentry\SentryBundle\EventListener + */ class ExceptionListener { - public function __construct(\Raven_Client $client = null) - { + /** @var TokenStorageInterface */ + private $tokenStorage; + + /** @var AuthorizationCheckerInterface */ + private $authorizationChecker; + + /** @var \Raven_Client */ + private $client; + + /** + * ExceptionListener constructor. + * @param TokenStorageInterface $tokenStorage + * @param AuthorizationCheckerInterface $authorizationChecker + * @param \Raven_Client $client + */ + public function __construct( + TokenStorageInterface $tokenStorage, + AuthorizationCheckerInterface $authorizationChecker, + \Raven_Client $client = null + ) { if (!$client) { $client = new \Raven_Client(); } + + $this->tokenStorage = $tokenStorage; + $this->authorizationChecker = $authorizationChecker; $this->client = $client; } + /** + * @param \Raven_Client $client + */ public function setClient(\Raven_Client $client) { $this->client = $client; } + /** + * Set the username from the security context by listening on core.request + * + * @param GetResponseEvent $event + */ + public function onKernelRequest(GetResponseEvent $event) + { + if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) { + return; + } + + if (null === $this->tokenStorage || null === $this->authorizationChecker) { + return; + } + + $token = $this->tokenStorage->getToken(); + if (null !== $token && $this->authorizationChecker->isGranted('IS_AUTHENTICATED_REMEMBERED')) { + $this->setUserValue($token->getUser()); + } + } + + /** + * @param GetResponseForExceptionEvent $event + */ public function onKernelException(GetResponseForExceptionEvent $event) { $exception = $event->getException(); - // dont capture HTTP responses - if ($exception instanceof HttpException) { + // don't capture HTTP responses + if ($exception instanceof HttpExceptionInterface) { return; } $this->client->captureException($exception); } + + /** + * @param UserInterface | object | string $user + */ + private function setUserValue($user) + { + switch (true) { + case $user instanceof UserInterface: + $this->client->set_user_data($user->getUsername()); + return; + case is_object($user): + case is_string($user): + $this->client->set_user_data((string) $user); + return; + } + } } diff --git a/src/Sentry/SentryBundle/Resources/config/services.yml b/src/Sentry/SentryBundle/Resources/config/services.yml index 1a295d56..7d07e858 100644 --- a/src/Sentry/SentryBundle/Resources/config/services.yml +++ b/src/Sentry/SentryBundle/Resources/config/services.yml @@ -10,6 +10,10 @@ services: sentry.exception_listener: class: '%sentry.exception_listener%' - arguments: ['@sentry.client'] + arguments: + - '@security.token_storage' + - '@security.authorization_checker' + - '@sentry.client' tags: + - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest } - { name: kernel.event_listener, event: kernel.exception, method: onKernelException }