Skip to content

Commit

Permalink
Additional listener method for intercepting the user
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean85 committed May 18, 2016
1 parent 0d19adf commit 1b71b3f
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 6 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
81 changes: 77 additions & 4 deletions src/Sentry/SentryBundle/EventListener/ExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
6 changes: 5 additions & 1 deletion src/Sentry/SentryBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

0 comments on commit 1b71b3f

Please sign in to comment.