diff --git a/config/packages/monolog.yaml b/config/packages/monolog.yaml index 5862130..0878b63 100644 --- a/config/packages/monolog.yaml +++ b/config/packages/monolog.yaml @@ -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] diff --git a/config/services.yaml b/config/services.yaml index ef07b76..58cbf1b 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -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 diff --git a/src/EventListener/RequestListener.php b/src/EventListener/RequestListener.php new file mode 100644 index 0000000..b28b321 --- /dev/null +++ b/src/EventListener/RequestListener.php @@ -0,0 +1,36 @@ +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() ]); + } + } +} \ No newline at end of file diff --git a/src/Security/UserAuthenticator.php b/src/Security/UserAuthenticator.php index 49f559c..d042bcd 100644 --- a/src/Security/UserAuthenticator.php +++ b/src/Security/UserAuthenticator.php @@ -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(),