Skip to content
This repository has been archived by the owner on Nov 9, 2022. It is now read-only.

Commit

Permalink
store_username_in_server_variable option added
Browse files Browse the repository at this point in the history
Stores username of logged user in $_SERVER['SYMFONY_USERNAME'] - helps you to find out which user encountered the error
  • Loading branch information
Jirka Koutny committed Jun 4, 2015
1 parent b3bd642 commit e68ba5a
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 0 deletions.
3 changes: 3 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public function getConfigTreeBuilder()
->scalarNode('exceptions_directory')
->defaultNull()
->end()
->scalarNode('store_username_in_server_variable')
->defaultNull()
->end()
->end();

return $treeBuilder;
Expand Down
8 changes: 8 additions & 0 deletions DependencyInjection/KutnyTracyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,16 @@ public function load(array $configs, ContainerBuilder $container)
$dir = $container->getParameter('kernel.logs_dir') . '/exceptions';
}

$storeUsernameInServerVariable = $config['store_username_in_server_variable'];

if (null === $storeUsernameInServerVariable)
{
$storeUsernameInServerVariable = false;
}

$container->setParameter('kutny_tracy.exceptions_directory', $dir);
$container->setParameter('kutny_tracy.emails', $config['emails']);
$container->setParameter('kutny_tracy.store_username_in_server_variable', $storeUsernameInServerVariable);

$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.xml');
Expand Down
23 changes: 23 additions & 0 deletions KernelExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,30 @@
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\SecurityContext;
use Tracy\Debugger;

class KernelExceptionListener
{
private $storeUsernameInServerVariable;
private $securityContext;

public function __construct($storeUsernameInServerVariable, SecurityContext $securityContext)
{
$this->storeUsernameInServerVariable = $storeUsernameInServerVariable;
$this->securityContext = $securityContext;
}

public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();

if (!$this->isNotFoundException($exception) && !$this->isAccessDeniedHttpException($exception)) {
if ($this->storeUsernameInServerVariable)
{
$this->storeUsernameInServerVariable();
}

ob_start();
Debugger::exceptionHandler($exception, true);
$event->setResponse(new Response(ob_get_contents()));
Expand All @@ -41,4 +55,13 @@ private function isAccessDeniedHttpException(Exception $exception)
{
return $exception instanceOf AccessDeniedHttpException;
}

private function storeUsernameInServerVariable()
{
$token = $this->securityContext->getToken();

if ($token) {
$_SERVER['SYMFONY_USERNAME'] = $token->getUsername();
}
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ I also recommend you to enable Tracy in a strict mode so it can handle errors of
kutny_tracy:
emails: ['[email protected]'] # error notification recipients
exceptions_directory: <directory> # optional, default directory set to %kernel.logs_dir%/exceptions
store_username_in_server_variable: true|false # optional, default value = false; stores username of logged user in $_SERVER['SYMFONY_USERNAME'] - helps you to find out which user encountered the error

~~~~~

Expand Down
2 changes: 2 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

<services>
<service id="kutny_tracy_kernel_exception_listener" class="Kutny\TracyBundle\KernelExceptionListener">
<argument>%kutny_tracy.store_username_in_server_variable%</argument>
<argument type="service" id="security.context" />
<tag name="kernel.event_listener" event="kernel.exception" method="onKernelException"/>
<tag name="kernel.event_listener" event="console.exception" method="onConsoleException"/>
</service>
Expand Down

0 comments on commit e68ba5a

Please sign in to comment.