From 328556db94c067397ceb72431781bdba08be49b4 Mon Sep 17 00:00:00 2001 From: Sveneld Date: Wed, 13 Nov 2024 20:56:52 +0100 Subject: [PATCH] update form fields modify debug mailer remove 404 errors from log --- config/packages/security.php | 2 ++ config/services.php | 10 ++++++++ src/App/EventListener/ErrorListener.php | 26 +++++++++++++++++++++ src/App/Security/TokenProvider.php | 5 ++-- src/Controller/SecurityController.php | 2 +- src/Mail/DebugMailSender.php | 14 ++++++++++- templates/security/login.html.twig | 4 ++-- templates/security/reset_password.html.twig | 2 +- 8 files changed, 57 insertions(+), 8 deletions(-) create mode 100644 src/App/EventListener/ErrorListener.php diff --git a/config/packages/security.php b/config/packages/security.php index ada7dd2..e0a1599 100644 --- a/config/packages/security.php +++ b/config/packages/security.php @@ -27,6 +27,8 @@ $mainFirewall->anonymous(); $mainFirewall ->formLogin() + ->usernameParameter('number') + ->passwordParameter('password') ->loginPath('login') ->checkPath('login'); $mainFirewall diff --git a/config/services.php b/config/services.php index b627e05..a42290d 100644 --- a/config/services.php +++ b/config/services.php @@ -5,6 +5,7 @@ namespace Symfony\Component\DependencyInjection\Loader\Configurator; use BikeShare\App\Configuration; +use BikeShare\App\EventListener\ErrorListener; use BikeShare\Credit\CodeGenerator\CodeGenerator; use BikeShare\Credit\CodeGenerator\CodeGeneratorInterface; use BikeShare\Credit\CreditSystem; @@ -41,6 +42,14 @@ param('kernel.environment'), ]); + $services->set('exception_listener', ErrorListener::class) + ->args([ + param('kernel.error_controller'), + service('logger'), + param('kernel.debug'), + ]) + ->tag('kernel.event_subscriber'); + $services->set(Configuration::class) ->args([__DIR__ . '/../config.php']); @@ -49,6 +58,7 @@ '../src/Db/MysqliDbResult.php', '../src/SmsConnector/SmsGateway/SmsGateway.php', '../src/App/Configuration.php', + '../src/App/EventListener/ErrorListener.php', '../src/App/Kernel.php', '../src/App/Entity', ]); diff --git a/src/App/EventListener/ErrorListener.php b/src/App/EventListener/ErrorListener.php new file mode 100644 index 0000000..9df5649 --- /dev/null +++ b/src/App/EventListener/ErrorListener.php @@ -0,0 +1,26 @@ +logger) { + if (!$exception instanceof HttpExceptionInterface || $exception->getStatusCode() >= 500) { + $this->logger->critical($message, ['exception' => $exception]); + } elseif ($exception instanceof NotFoundHttpException) { + //do not log 404 errors + return; + } else { + $this->logger->error($message, ['exception' => $exception]); + } + } + } +} diff --git a/src/App/Security/TokenProvider.php b/src/App/Security/TokenProvider.php index 4354856..ba82014 100644 --- a/src/App/Security/TokenProvider.php +++ b/src/App/Security/TokenProvider.php @@ -54,8 +54,7 @@ public function updateToken(string $series, string $tokenValue, \DateTime $lastU $token = new PersistentToken( $currentToken->getClass(), - method_exists($currentToken, 'getUserIdentifier') ? - $currentToken->getUserIdentifier() : $currentToken->getUsername(), + $currentToken->getUserIdentifier(), $series, $tokenValue, $lastUsed @@ -83,7 +82,7 @@ public function createNewToken(PersistentTokenInterface $token) $this->db->query( "INSERT INTO remember_me_tokens (class, username, series, value, lastUsed) VALUES ('{$token->getClass()}', - '{$token->getUsername()}', + '{$token->getUserIdentifier()}', '{$token->getSeries()}', '{$token->getTokenValue()}', '{$token->getLastUsed()->format('Y-m-d H:i:s')}')" diff --git a/src/Controller/SecurityController.php b/src/Controller/SecurityController.php index 55f0ad4..ad0bbdf 100644 --- a/src/Controller/SecurityController.php +++ b/src/Controller/SecurityController.php @@ -58,7 +58,7 @@ public function resetPassword( TranslatorInterface $translator ) { if ($request->isMethod('POST')) { - $number = $request->request->get('_username'); + $number = $request->request->get('number'); try { $user = $userProvider->loadUserByIdentifier($number); diff --git a/src/Mail/DebugMailSender.php b/src/Mail/DebugMailSender.php index b3bb01c..d17d51c 100644 --- a/src/Mail/DebugMailSender.php +++ b/src/Mail/DebugMailSender.php @@ -1,11 +1,23 @@ logger = $logger; + } + public function sendMail($recipient, $subject, $message) { - echo $recipient, ' | ', $subject, ' | ', $message . PHP_EOL; + $this->logger->debug('Sending email', compact('recipient', 'subject', 'message')); } } diff --git a/templates/security/login.html.twig b/templates/security/login.html.twig index dd3dcd1..a7a4a0e 100644 --- a/templates/security/login.html.twig +++ b/templates/security/login.html.twig @@ -11,7 +11,7 @@ - +
@@ -21,7 +21,7 @@ ({{ 'Forgotten? Reset password'|trans }}) - +
diff --git a/templates/security/reset_password.html.twig b/templates/security/reset_password.html.twig index e785169..1611ca0 100644 --- a/templates/security/reset_password.html.twig +++ b/templates/security/reset_password.html.twig @@ -14,7 +14,7 @@ - +