fix: keep the log processors working on a stateless firewall
This commit is contained in:
1 parent
30ad5e8488
commit
f073a2014d
4 files changed
+163
-10
No files matched your search
@@ -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(),
|
||||
];
|
||||
|
||||
Reference in new issue
Block a user