From f9526d15746429d935e26a63166cce9e43a6ec36 Mon Sep 17 00:00:00 2001 From: roadiz-ci Date: Wed, 7 Feb 2024 14:56:21 +0000 Subject: [PATCH] fix: Fixed stateless with some listeners --- config/packages/security.yaml | 1 + config/routing.yaml | 4 ++++ src/EventSubscriber/LocaleSubscriber.php | 19 ++++++++++++------- src/EventSubscriber/UserLocaleSubscriber.php | 7 +++++++ 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/config/packages/security.yaml b/config/packages/security.yaml index 7727b107..8324dc43 100644 --- a/config/packages/security.yaml +++ b/config/packages/security.yaml @@ -25,6 +25,7 @@ security: # https://symfony.com/bundles/LexikJWTAuthenticationBundle/current/index.html#configure-application-routing api_login: + stateless: true pattern: ^/api/token provider: all_users login_throttling: diff --git a/config/routing.yaml b/config/routing.yaml index 361fab01..2de42814 100644 --- a/config/routing.yaml +++ b/config/routing.yaml @@ -5,12 +5,14 @@ api_custom_forms_item_definition: methods: [GET] path: /api/custom_forms/{id}/definition controller: RZ\Roadiz\CoreBundle\Controller\CustomFormController::definitionAction + stateless: true requirements: id: "[0-9]+" api_custom_forms_item_post: methods: [POST] path: /api/custom_forms/{id}/post controller: RZ\Roadiz\CoreBundle\Controller\CustomFormController::postAction + stateless: true requirements: id: "[0-9]+" @@ -28,6 +30,7 @@ customFormSentAction: healthCheckAction: methods: [GET] path: /health-check + stateless: true controller: RZ\Roadiz\CoreBundle\Controller\HealthCheckController roadiz_core_themes: @@ -36,4 +39,5 @@ roadiz_core_themes: api_login_check: methods: [POST] + stateless: true path: /api/token diff --git a/src/EventSubscriber/LocaleSubscriber.php b/src/EventSubscriber/LocaleSubscriber.php index cfdd022b..c135ab01 100644 --- a/src/EventSubscriber/LocaleSubscriber.php +++ b/src/EventSubscriber/LocaleSubscriber.php @@ -43,21 +43,26 @@ public function onKernelRequest(RequestEvent $event): void $request = $event->getRequest(); $locale = $request->query->get('_locale') ?? $request->attributes->get('_locale'); - if ($request->hasPreviousSession()) { + /* + * Set default locale + */ + if (null !== $locale && $locale !== '') { + $this->setLocale($event, $locale); + return; + } + + if (!$request->attributes->getBoolean('_stateless') && $request->hasPreviousSession()) { $locale = $request->getSession()->get('_locale', null); if (null !== $locale) { $this->setLocale($event, $locale); + return; } } - /* - * Set default locale - */ - if (null !== $locale && $locale !== '') { - $this->setLocale($event, $locale); - } elseif (null !== $translation = $this->getDefaultTranslation()) { + if (null !== $translation = $this->getDefaultTranslation()) { $shortLocale = $translation->getLocale(); $this->setLocale($event, $shortLocale); + return; } } diff --git a/src/EventSubscriber/UserLocaleSubscriber.php b/src/EventSubscriber/UserLocaleSubscriber.php index 2fed01d2..3604eda3 100644 --- a/src/EventSubscriber/UserLocaleSubscriber.php +++ b/src/EventSubscriber/UserLocaleSubscriber.php @@ -44,6 +44,10 @@ public static function getSubscribedEvents(): array */ public function onInteractiveLogin(InteractiveLoginEvent $event): void { + if ($this->requestStack->getMainRequest()?->attributes->getBoolean('_stateless')) { + return; + } + $user = $event->getAuthenticationToken()->getUser(); if ( @@ -59,6 +63,9 @@ public function onInteractiveLogin(InteractiveLoginEvent $event): void */ public function onUserUpdated(FilterUserEvent $event): void { + if ($this->requestStack->getMainRequest()?->attributes->getBoolean('_stateless')) { + return; + } $user = $event->getUser(); if (