fix: keep the log processors working on a stateless firewall

This commit is contained in:
fromme committed 2026-09-24 16:35:12 +02:00
1 parent 30ad5e8488
commit f073a2014d
4 files changed
+163 -10

No files matched your search

+24 -4
View File
@@ -8,8 +8,15 @@ use Symfony\Bundle\SecurityBundle\Security;
class InjectUserProcessor
{
public function __construct(private readonly Security $security)
{
/**
* The SAPI is injected rather than read from PHP_SAPI inside the processor so a test can
* exercise the web path: under PHPUnit php_sapi_name() is always "cli", which made the
* branch below unreachable from every functional test.
*/
public function __construct(
private readonly Security $security,
private readonly string $sapi = \PHP_SAPI,
) {
}
public function __invoke(LogRecord $record): LogRecord
@@ -18,10 +25,9 @@ class InjectUserProcessor
return $record;
}
/** @var User $user */
$user = $this->security->getUser();
if (null === $user || 'cli' === php_sapi_name()) {
if (null === $user || 'cli' === $this->sapi) {
$record->extra['user'] = [
'id' => null,
'username' => 'system',
@@ -31,6 +37,20 @@ class InjectUserProcessor
return $record;
}
// Not every firewall authenticates a row in the user table: the stateless "api" firewall
// resolves its shared key to an in-memory user, which has an identifier and roles and
// nothing else. Reaching for getId() or getEmail() there is a 500 on a request that had
// already stored its data.
if (false === $user instanceof User) {
$record->extra['user'] = [
'id' => null,
'username' => $user->getUserIdentifier(),
'roles' => $user->getRoles(),
];
return $record;
}
$record->extra['user'] = [
'id' => $user->getId(),
'email' => $user->getEmail(),
+15 -6
View File
@@ -2,7 +2,7 @@
namespace App\Logging;
use App\Entity\Core\User;
use App\Entity\User;
use Monolog\Attribute\AsMonologProcessor;
use Monolog\LogRecord;
use Symfony\Bundle\SecurityBundle\Security;
@@ -11,8 +11,15 @@ use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
#[AsMonologProcessor]
class UserDataProcessor
{
public function __construct(private readonly Security $security)
{
/**
* The SAPI is injected rather than read from PHP_SAPI inside the processor so a test can
* exercise the web path: under PHPUnit php_sapi_name() is always "cli", which made
* everything below the check unreachable from every functional test.
*/
public function __construct(
private readonly Security $security,
private readonly string $sapi = \PHP_SAPI,
) {
}
public function __invoke(LogRecord $record): LogRecord
@@ -21,7 +28,7 @@ class UserDataProcessor
return $record;
}
if ('cli' === php_sapi_name()) {
if ('cli' === $this->sapi) {
$record->extra['user'] = [
'id' => null,
'username' => 'SYSTEM',
@@ -31,7 +38,6 @@ class UserDataProcessor
return $record;
}
/** @var User $user */
$user = $this->security->getUser();
if (null === $user) {
@@ -57,7 +63,10 @@ class UserDataProcessor
$username = $user->getUserIdentifier();
}
$record->extra['user'] = [
'id' => $user->getId(),
// Only a user table row has an id: the stateless "api" firewall resolves its shared
// key to an in-memory user, and calling getId() on that is a 500 on a request that
// had already stored its data.
'id' => $user instanceof User ? $user->getId() : null,
'username' => $username,
'roles' => $user->getRoles(),
];