Skip to content

Setting the user through a new listener #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 }