log requests for all logged in users
This commit is contained in:
parent
9101ea36e1
commit
649c788da2
|
@ -1,5 +1,6 @@
|
|||
monolog:
|
||||
channels:
|
||||
- user
|
||||
- deprecation # Deprecations are logged in the dedicated "deprecation" channel when it exists
|
||||
|
||||
when@dev:
|
||||
|
@ -9,7 +10,7 @@ when@dev:
|
|||
type: stream
|
||||
path: "%kernel.logs_dir%/%kernel.environment%.log"
|
||||
level: debug
|
||||
channels: ["!event"]
|
||||
channels: ["!event", "!user"]
|
||||
# uncomment to get logging in your browser
|
||||
# you may have to allow bigger header sizes in your Web server configuration
|
||||
#firephp:
|
||||
|
@ -21,11 +22,12 @@ when@dev:
|
|||
console:
|
||||
type: console
|
||||
process_psr_3_messages: false
|
||||
channels: ["!event", "!doctrine", "!console"]
|
||||
channels: ["!event", "!doctrine", "!console", "!user"]
|
||||
user:
|
||||
type: stream
|
||||
level: debug
|
||||
path: "%kernel.logs_dir%/user.log"
|
||||
channels: [user]
|
||||
|
||||
when@test:
|
||||
monolog:
|
||||
|
@ -35,7 +37,7 @@ when@test:
|
|||
action_level: error
|
||||
handler: nested
|
||||
excluded_http_codes: [404, 405]
|
||||
channels: ["!event"]
|
||||
channels: ["!event", "!user"]
|
||||
nested:
|
||||
type: stream
|
||||
path: "%kernel.logs_dir%/%kernel.environment%.log"
|
||||
|
@ -44,6 +46,7 @@ when@test:
|
|||
type: stream
|
||||
level: debug
|
||||
path: "%kernel.logs_dir%/user.log"
|
||||
channels: [user]
|
||||
|
||||
when@prod:
|
||||
monolog:
|
||||
|
@ -54,6 +57,7 @@ when@prod:
|
|||
handler: nested
|
||||
excluded_http_codes: [404, 405]
|
||||
buffer_size: 50 # How many messages should be saved? Prevent memory leaks
|
||||
channels: ["!user"]
|
||||
nested:
|
||||
type: stream
|
||||
path: php://stderr
|
||||
|
@ -72,4 +76,4 @@ when@prod:
|
|||
type: stream
|
||||
level: debug
|
||||
path: "%kernel.logs_dir%/user.log"
|
||||
formatter: monolog.formatter.json
|
||||
channels: [user]
|
||||
|
|
|
@ -20,6 +20,10 @@ services:
|
|||
- '../src/Entity/'
|
||||
- '../src/Kernel.php'
|
||||
- '../src/Tests/'
|
||||
App\EventListener\RequestListener:
|
||||
tags:
|
||||
- name: monolog.logger
|
||||
channel: user
|
||||
|
||||
# add more service definitions when explicit configuration is needed
|
||||
# please note that last definitions always *replace* previous ones
|
||||
|
|
|
@ -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() ]);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -7,11 +7,9 @@ use Symfony\Component\HttpFoundation\Request;
|
|||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
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\Passport\Badge\CsrfTokenBadge;
|
||||
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\Util\TargetPathTrait;
|
||||
|
||||
|
@ -21,19 +19,14 @@ class UserAuthenticator extends AbstractLoginFormAuthenticator
|
|||
|
||||
public const LOGIN_ROUTE = 'app_login';
|
||||
|
||||
private UrlGeneratorInterface $urlGenerator;
|
||||
|
||||
public function __construct(UrlGeneratorInterface $urlGenerator)
|
||||
{
|
||||
$this->urlGenerator = $urlGenerator;
|
||||
}
|
||||
public function __construct(
|
||||
private UrlGeneratorInterface $urlGenerator
|
||||
) { }
|
||||
|
||||
public function authenticate(Request $request): Passport
|
||||
{
|
||||
$username = $request->request->get('username', '');
|
||||
|
||||
$request->getSession()->set(Security::LAST_USERNAME, $username);
|
||||
|
||||
return new Passport(
|
||||
new UserBadge($username),
|
||||
new EmptyPassword(),
|
||||
|
|
Loading…
Reference in New Issue