8000 feature #24026 [Security] add impersonator_user to "User was reloaded… · symfony/symfony@80ac529 · GitHub
[go: up one dir, main page]

Skip to content

Commit 80ac529

Browse files
committed
feature #24026 [Security] add impersonator_user to "User was reloaded" log message (gharlan)
This PR was squashed before being merged into the 3.4 branch (closes #24026). Discussion ---------- [Security] add impersonator_user to "User was reloaded" log message | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | My main concern is this: I use the swift monolog handler to get emails for exceptions. I would like to see the impersonator in these mails. But I'm not sure, if this is a good place for the log message. Commits ------- fc44215 [Security] add impersonator_user to "User was reloaded" log message
2 parents 9cce236 + fc44215 commit 80ac529

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/Symfony/Component/Security/Http/Firewall/ContextListener.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
2424
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
2525
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
26+
use Symfony\Component\Security\Core\Role\SwitchUserRole;
2627
use Symfony\Component\Security\Core\User\UserInterface;
2728
use Symfony\Component\Security\Core\User\UserProviderInterface;
2829
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
@@ -91,7 +92,10 @@ public function handle(GetResponseEvent $event)
9192
$token = unserialize($token);
9293

9394
if (null !== $this->logger) {
94-
$this->logger->debug('Read existing security token from the session.', array('key' => $this->sessionKey));
95+
$this->logger->debug('Read existing security token from the session.', array(
96+
'key' => $this->sessionKey,
97+
'token_class' => is_object($token) ? get_class($token) : null,
98+
));
9599
}
96100

97101
if ($token instanceof TokenInterface) {
@@ -169,7 +173,16 @@ protected function refreshUser(TokenInterface $token)
169173
$token->setUser($refreshedUser);
170174

171175
if (null !== $this->logger) {
172-
$this->logger->debug('User was reloaded from a user provider.', array('username' => $refreshedUser->getUsername(), 'provider' => get_class($provider)));
176+
$context = array('provider' => get_class($provider), 'username' => $refreshedUser->getUsername());
177+
178+
foreach ($token->getRoles() as $role) {
179+
if ($role instanceof SwitchUserRole) {
180+
$context['impersonator_username'] = $role->getSource()->getUsername();
181+
break;
182+
}
183+
}
184+
185+
$this->logger->debug('User was reloaded from a user provider.', $context);
173186
}
174187

175188
return $token;

0 commit comments

Comments
 (0)
0