log requests for all logged in users

This commit is contained in:
b1ek 2024-10-04 17:48:36 +10:00
parent 9101ea36e1
commit 649c788da2
Signed by: blek
GPG Key ID: A622C22C9BC616B2
4 changed files with 51 additions and 14 deletions

View File

@ -1,5 +1,6 @@
monolog: monolog:
channels: channels:
- user
- deprecation # Deprecations are logged in the dedicated "deprecation" channel when it exists - deprecation # Deprecations are logged in the dedicated "deprecation" channel when it exists
when@dev: when@dev:
@ -9,7 +10,7 @@ when@dev:
type: stream type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log" path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug level: debug
channels: ["!event"] channels: ["!event", "!user"]
# uncomment to get logging in your browser # uncomment to get logging in your browser
# you may have to allow bigger header sizes in your Web server configuration # you may have to allow bigger header sizes in your Web server configuration
#firephp: #firephp:
@ -21,11 +22,12 @@ when@dev:
console: console:
type: console type: console
process_psr_3_messages: false process_psr_3_messages: false
channels: ["!event", "!doctrine", "!console"] channels: ["!event", "!doctrine", "!console", "!user"]
user: user:
type: stream type: stream
level: debug level: debug
path: "%kernel.logs_dir%/user.log" path: "%kernel.logs_dir%/user.log"
channels: [user]
when@test: when@test:
monolog: monolog:
@ -35,7 +37,7 @@ when@test:
action_level: error action_level: error
handler: nested handler: nested
excluded_http_codes: [404, 405] excluded_http_codes: [404, 405]
channels: ["!event"] channels: ["!event", "!user"]
nested: nested:
type: stream type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log" path: "%kernel.logs_dir%/%kernel.environment%.log"
@ -44,6 +46,7 @@ when@test:
type: stream type: stream
level: debug level: debug
path: "%kernel.logs_dir%/user.log" path: "%kernel.logs_dir%/user.log"
channels: [user]
when@prod: when@prod:
monolog: monolog:
@ -54,6 +57,7 @@ when@prod:
handler: nested handler: nested
excluded_http_codes: [404, 405] excluded_http_codes: [404, 405]
buffer_size: 50 # How many messages should be saved? Prevent memory leaks buffer_size: 50 # How many messages should be saved? Prevent memory leaks
channels: ["!user"]
nested: nested:
type: stream type: stream
path: php://stderr path: php://stderr
@ -72,4 +76,4 @@ when@prod:
type: stream type: stream
level: debug level: debug
path: "%kernel.logs_dir%/user.log" path: "%kernel.logs_dir%/user.log"
formatter: monolog.formatter.json channels: [user]

View File

@ -20,6 +20,10 @@ services:
- '../src/Entity/' - '../src/Entity/'
- '../src/Kernel.php' - '../src/Kernel.php'
- '../src/Tests/' - '../src/Tests/'
App\EventListener\RequestListener:
tags:
- name: monolog.logger
channel: user
# add more service definitions when explicit configuration is needed # add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones # please note that last definitions always *replace* previous ones

View File

@ -0,0 +1,36 @@
<?php
namespace App\EventListener;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\Security\Core\Security;
#[AsEventListener(event: RequestEvent::class)]
class RequestListener
{
public function __construct(
private Security $security,
private LoggerInterface $logger
) {}
public function onKernelRequest(RequestEvent $event): void
{
$req = $event->getRequest();
if (!$event->isMainRequest()) {
return;
}
if (!($req->isMethod('GET') || $req->isMethod('POST'))) {
return;
}
if (!$req->getSession()->has(Security::LAST_USERNAME)) {
return;
}
$user = $this->security->getUser();
if ($user !== null) {
$this->logger->info('Handled request for user with log flag', [ $req->getMethod(), $req->getUri(), $user->getUserIdentifier() ]);
}
}
}

View File

@ -7,11 +7,9 @@ use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator; use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge; use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
use Symfony\Component\Security\Http\Authenticator\Passport\Passport; use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
use Symfony\Component\Security\Http\Util\TargetPathTrait; use Symfony\Component\Security\Http\Util\TargetPathTrait;
@ -21,19 +19,14 @@ class UserAuthenticator extends AbstractLoginFormAuthenticator
public const LOGIN_ROUTE = 'app_login'; public const LOGIN_ROUTE = 'app_login';
private UrlGeneratorInterface $urlGenerator; public function __construct(
private UrlGeneratorInterface $urlGenerator
public function __construct(UrlGeneratorInterface $urlGenerator) ) { }
{
$this->urlGenerator = $urlGenerator;
}
public function authenticate(Request $request): Passport public function authenticate(Request $request): Passport
{ {
$username = $request->request->get('username', ''); $username = $request->request->get('username', '');
$request->getSession()->set(Security::LAST_USERNAME, $username);
return new Passport( return new Passport(
new UserBadge($username), new UserBadge($username),
new EmptyPassword(), new EmptyPassword(),