Skip to content

Commit 3314c20

Browse files
authored
Support logging the impersonator user, if any (#647)
1 parent 0c6eee6 commit 3314c20

File tree

5 files changed

+234
-64
lines changed

5 files changed

+234
-64
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
## Unreleased
44

55
- Add support for tracing of the Symfony HTTP client requests (#606)
6+
- Support logging the impersonator user, if any (#647)
67

78
## 4.3.0 (2022-05-30)
8-
- Fix compatibility issue with Symfony >= 6.1.0 (#635)
9+
10+
- Fix compatibility issue with Symfony `>= 6.1.0` (#635)
911
- Add `TracingDriverConnectionInterface::getNativeConnection()` method to get the original driver connection (#597)
1012
- Add `options.http_timeout` and `options.http_connect_timeout` configuration options (#593)
1113

phpstan-baseline.neon

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -160,16 +160,6 @@ parameters:
160160
count: 1
161161
path: src/EventListener/RequestListener.php
162162

163-
-
164-
message: "#^Cannot call method getUser\\(\\) on Symfony\\\\Component\\\\Security\\\\Core\\\\Authentication\\\\Token\\\\TokenInterface\\|null\\.$#"
165-
count: 1
166-
path: src/EventListener/RequestListener.php
167-
168-
-
169-
message: "#^Parameter \\#1 \\$user of method Sentry\\\\SentryBundle\\\\EventListener\\\\RequestListener\\:\\:getUsername\\(\\) expects object\\|string, Symfony\\\\Component\\\\Security\\\\Core\\\\User\\\\UserInterface\\|null given\\.$#"
170-
count: 1
171-
path: src/EventListener/RequestListener.php
172-
173163
-
174164
message: "#^Call to an undefined method Symfony\\\\Component\\\\HttpKernel\\\\Event\\\\KernelEvent\\:\\:isMasterRequest\\(\\)\\.$#"
175165
count: 1
@@ -315,6 +305,21 @@ parameters:
315305
count: 1
316306
path: tests/EventListener/RequestListenerTest.php
317307

308+
-
309+
message: "#^Parameter \\#3 \\$roles of class Symfony\\\\Component\\\\Security\\\\Core\\\\Authentication\\\\Token\\\\SwitchUserToken constructor expects array\\<string\\>, string given\\.$#"
310+
count: 1
311+
path: tests/EventListener/RequestListenerTest.php
312+
313+
-
314+
message: "#^Parameter \\#4 \\$originalToken of class Symfony\\\\Component\\\\Security\\\\Core\\\\Authentication\\\\Token\\\\SwitchUserToken constructor expects Symfony\\\\Component\\\\Security\\\\Core\\\\Authentication\\\\Token\\\\TokenInterface, array\\<int, string\\> given\\.$#"
315+
count: 1
316+
path: tests/EventListener/RequestListenerTest.php
317+
318+
-
319+
message: "#^Parameter \\#5 \\$originatedFromUri of class Symfony\\\\Component\\\\Security\\\\Core\\\\Authentication\\\\Token\\\\SwitchUserToken constructor expects string\\|null, Sentry\\\\SentryBundle\\\\Tests\\\\EventListener\\\\AuthenticatedTokenStub given\\.$#"
320+
count: 1
321+
path: tests/EventListener/RequestListenerTest.php
322+
318323
-
319324
message: "#^Call to an undefined method Symfony\\\\Component\\\\HttpKernel\\\\Event\\\\KernelEvent\\:\\:isMasterRequest\\(\\)\\.$#"
320325
count: 1

src/EventListener/RequestListener.php

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Symfony\Component\HttpKernel\Event\ControllerEvent;
1111
use Symfony\Component\HttpKernel\Event\RequestEvent;
1212
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
13+
use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
1314
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1415
use Symfony\Component\Security\Core\User\UserInterface;
1516

@@ -62,16 +63,11 @@ public function handleKernelRequestEvent(RequestEvent $event): void
6263
return;
6364
}
6465

65-
$token = null;
6666
$userData = new UserDataBag();
6767
$userData->setIpAddress($event->getRequest()->getClientIp());
6868

6969
if (null !== $this->tokenStorage) {
70-
$token = $this->tokenStorage->getToken();
71-
}
72-
73-
if ($this->isTokenAuthenticated($token)) {
74-
$userData->setUsername($this->getUsername($token->getUser()));
70+
$this->setUserData($userData, $this->tokenStorage->getToken());
7571
}
7672

7773
$this->hub->configureScope(static function (Scope $scope) use ($userData): void {
@@ -103,7 +99,7 @@ public function handleKernelControllerEvent(ControllerEvent $event): void
10399
}
104100

105101
/**
106-
* @param UserInterface|object|string $user
102+
* @param UserInterface|object|string|null $user
107103
*/
108104
private function getUsername($user): ?string
109105
{
@@ -128,12 +124,32 @@ private function getUsername($user): ?string
128124
return null;
129125
}
130126

131-
private function isTokenAuthenticated(?TokenInterface $token): bool
127+
private function getImpersonatorUser(TokenInterface $token): ?string
132128
{
133-
if (null === $token) {
134-
return false;
129+
if (!$token instanceof SwitchUserToken) {
130+
return null;
135131
}
136132

133+
return $this->getUsername($token->getOriginalToken()->getUser());
134+
}
135+
136+
private function setUserData(UserDataBag $userData, ?TokenInterface $token): void
137+
{
138+
if (null === $token || !$this->isTokenAuthenticated($token)) {
139+
return;
140+
}
141+
142+
$userData->setUsername($this->getUsername($token->getUser()));
143+
144+
$impersonatorUser = $this->getImpersonatorUser($token);
145+
146+
if (null !== $impersonatorUser) {
147+
$userData->setMetadata('impersonator_username', $impersonatorUser);
148+
}
149+
}
150+
151+
private function isTokenAuthenticated(TokenInterface $token): bool
152+
{
137153
if (method_exists($token, 'isAuthenticated') && !$token->isAuthenticated(false)) {
138154
return false;
139155
}

tests/EventListener/Fixtures/UserWithIdentifierStub.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,24 @@
88

99
final class UserWithIdentifierStub implements UserInterface
1010
{
11+
/**
12+
* @var string
13+
*/
14+
private $username;
15+
16+
public function __construct(string $username = 'foo_user')
17+
{
18+
$this->username = $username;
19+
}
20+
1121
public function getUserIdentifier(): string
1222
{
1323
return $this->getUsername();
1424
}
1525

1626
public function getUsername(): string
1727
{
18-
return 'foo_user';
28+
return $this->username;
1929
}
2030

2131
public function getRoles(): array

0 commit comments

Comments
 (0)