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

Commit e68ba5a

Browse files
author
Jirka Koutny
committed
store_username_in_server_variable option added
Stores username of logged user in $_SERVER['SYMFONY_USERNAME'] - helps you to find out which user encountered the error
1 parent b3bd642 commit e68ba5a

File tree

5 files changed

+37
-0
lines changed

5 files changed

+37
-0
lines changed

DependencyInjection/Configuration.php

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ public function getConfigTreeBuilder()
2828
->scalarNode('exceptions_directory')
2929
->defaultNull()
3030
->end()
31+
->scalarNode('store_username_in_server_variable')
32+
->defaultNull()
33+
->end()
3134
->end();
3235

3336
return $treeBuilder;

DependencyInjection/KutnyTracyExtension.php

+8
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,16 @@ public function load(array $configs, ContainerBuilder $container)
2121
$dir = $container->getParameter('kernel.logs_dir') . '/exceptions';
2222
}
2323

24+
$storeUsernameInServerVariable = $config['store_username_in_server_variable'];
25+
26+
if (null === $storeUsernameInServerVariable)
27+
{
28+
$storeUsernameInServerVariable = false;
29+
}
30+
2431
$container->setParameter('kutny_tracy.exceptions_directory', $dir);
2532
$container->setParameter('kutny_tracy.emails', $config['emails']);
33+
$container->setParameter('kutny_tracy.store_username_in_server_variable', $storeUsernameInServerVariable);
2634

2735
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
2836
$loader->load('services.xml');

KernelExceptionListener.php

+23
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,30 @@
88
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
99
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
1010
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
11+
use Symfony\Component\Security\Core\SecurityContext;
1112
use Tracy\Debugger;
1213

1314
class KernelExceptionListener
1415
{
16+
private $storeUsernameInServerVariable;
17+
private $securityContext;
18+
19+
public function __construct($storeUsernameInServerVariable, SecurityContext $securityContext)
20+
{
21+
$this->storeUsernameInServerVariable = $storeUsernameInServerVariable;
22+
$this->securityContext = $securityContext;
23+
}
1524

1625
public function onKernelException(GetResponseForExceptionEvent $event)
1726
{
1827
$exception = $event->getException();
1928

2029
if (!$this->isNotFoundException($exception) && !$this->isAccessDeniedHttpException($exception)) {
30+
if ($this->storeUsernameInServerVariable)
31+
{
32+
$this->storeUsernameInServerVariable();
33+
}
34+
2135
ob_start();
2236
Debugger::exceptionHandler($exception, true);
2337
$event->setResponse(new Response(ob_get_contents()));
@@ -41,4 +55,13 @@ private function isAccessDeniedHttpException(Exception $exception)
4155
{
4256
return $exception instanceOf AccessDeniedHttpException;
4357
}
58+
59+
private function storeUsernameInServerVariable()
60+
{
61+
$token = $this->securityContext->getToken();
62+
63+
if ($token) {
64+
$_SERVER['SYMFONY_USERNAME'] = $token->getUsername();
65+
}
66+
}
4467
}

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ I also recommend you to enable Tracy in a strict mode so it can handle errors of
7070
kutny_tracy:
7171
emails: ['[email protected]'] # error notification recipients
7272
exceptions_directory: <directory> # optional, default directory set to %kernel.logs_dir%/exceptions
73+
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
7374

7475
~~~~~
7576

Resources/config/services.xml

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
<services>
88
<service id="kutny_tracy_kernel_exception_listener" class="Kutny\TracyBundle\KernelExceptionListener">
9+
<argument>%kutny_tracy.store_username_in_server_variable%</argument>
10+
<argument type="service" id="security.context" />
911
<tag name="kernel.event_listener" event="kernel.exception" method="onKernelException"/>
1012
<tag name="kernel.event_listener" event="console.exception" method="onConsoleException"/>
1113
</service>

0 commit comments

Comments
 (0)