From 8aca0ab5f20f77db7c53427d10cf6d59273bf01e Mon Sep 17 00:00:00 2001 From: Maurizio Weis Date: Wed, 22 Nov 2023 17:09:06 +0100 Subject: [PATCH] #4 Fix redirect in different languages --- src/Service/PasswordPathService.php | 70 ++++++++++++++++++++++ src/Subscriber/CheckPasswordSubscriber.php | 68 ++++----------------- src/iMidiPasswordSite.php | 7 ++- 3 files changed, 87 insertions(+), 58 deletions(-) create mode 100644 src/Service/PasswordPathService.php diff --git a/src/Service/PasswordPathService.php b/src/Service/PasswordPathService.php new file mode 100644 index 0000000..a136550 --- /dev/null +++ b/src/Service/PasswordPathService.php @@ -0,0 +1,70 @@ +getPath(); + if (!$path) { + if ($category->getCustomFields() !== null && array_key_exists('password_site_password', $category->getCustomFields())) { + $this->checkAuthenticated($event, $category->getId()); + } + return; + } + + $path .= $category->getId(); + $parents = array_reverse(array_slice(explode('|', $path), 1)); + + + foreach ($parents as $parentId) { + + $parent = $this->categoryRepository + ->search(new Criteria([$parentId]), $context)->first(); + + if ($parent->getCustomFields() !== null + && array_key_exists('password_site_password', $parent->getCustomFields())) { + + $this->checkAuthenticated($event, $parent->getId()); + break; + } + } + } + + private function checkAuthenticated($event, string $navigationId) + { + $session = $event->getRequest()->getSession(); + $session->set('redirect', $event->getRequest()->server->get('REQUEST_URI')); + + if (!$session->has(self::AUTH_SESSION_PREFIX . $navigationId)) { + $this->passwordPageController->redirectToLogin($navigationId); + } + } + + public function findNavigation(mixed $navigationId, HttpCacheHitEvent $event) + { + $category = $this->categoryRepository->search(new Criteria([$navigationId]), Context::createDefaultContext())->first(); + $this->checkPasswordInPath($category, $event); + } +} diff --git a/src/Subscriber/CheckPasswordSubscriber.php b/src/Subscriber/CheckPasswordSubscriber.php index 79c17dd..80c7e82 100644 --- a/src/Subscriber/CheckPasswordSubscriber.php +++ b/src/Subscriber/CheckPasswordSubscriber.php @@ -2,12 +2,8 @@ namespace iMidiPasswordSite\Subscriber; -use iMidiPasswordSite\Storefront\Controller\PasswordPageController; -use Shopware\Core\Content\Category\CategoryEntity; -use Shopware\Core\Framework\Context; -use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository; -use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria; -use Shopware\Storefront\Framework\Cache\Event\HttpCacheHitEvent; +use iMidiPasswordSite\Service\PasswordPathService; +use Shopware\Core\Framework\Adapter\Cache\Event\HttpCacheHitEvent; use Shopware\Storefront\Framework\Routing\Router; use Shopware\Storefront\Page\GenericPageLoadedEvent; use Shopware\Storefront\Page\PageLoadedEvent; @@ -15,20 +11,12 @@ class CheckPasswordSubscriber implements EventSubscriberInterface { - const AUTH_SESSION_PREFIX = 'auth_'; - - private $router; - private $categoryRepository; - private $passwordPageController; public function __construct( - Router $router, - EntityRepository $categoryRepository, - PasswordPageController $passwordPageController) + private Router $router, + private PasswordPathService $passwordPathService, + ) { - $this->router = $router; - $this->categoryRepository = $categoryRepository; - $this->passwordPageController = $passwordPageController; } public static function getSubscribedEvents(): array @@ -42,21 +30,25 @@ public static function getSubscribedEvents(): array public function onPageLoaded(PageLoadedEvent $event) { $request = $event->getRequest(); + + //early return for login page being loaded if ($request->attributes->get('_route') === 'frontend.password.restricted') { return; } + //???? $page = $event->getPage(); if (!$page->getHeader()) { return; } $activeNavigation = $page->getHeader()->getNavigation()->getActive(); - $this->checkPasswordInPath($activeNavigation, $event); + $this->passwordPathService->checkPasswordInPath($activeNavigation, $event); } public function onCachedPageLoaded(HttpCacheHitEvent $event) { + $requestUri = $event->getRequest()->attributes->get('resolved-uri'); if ($this->router instanceof RequestMatcherInterface) { @@ -67,47 +59,9 @@ public function onCachedPageLoaded(HttpCacheHitEvent $event) if ($parameters['_route'] === 'frontend.navigation.page') { $navigationId = $parameters['navigationId']; - $category = $this->categoryRepository->search(new Criteria([$navigationId]), Context::createDefaultContext())->first(); - $this->checkPasswordInPath($category, $event); - } - } - - private function checkAuthenticated($event, string $navigationId) - { - $session = $event->getRequest()->getSession(); - $session->set('redirect', $event->getRequest()->server->get('REQUEST_URI')); - - if (!$session->has(self::AUTH_SESSION_PREFIX . $navigationId)) { - $this->passwordPageController->redirectToLogin($navigationId); + $this->passwordPathService->findNavigation($navigationId, $event); } } - private function checkPasswordInPath(CategoryEntity $category, $event) - { - //include router - $context = Context::createDefaultContext(); - if ($event instanceof PageLoadedEvent) { - $context = $event->getContext(); - } - - $path = $category->getPath(); - if (!$path) { - if ($category->getCustomFields() !== null && array_key_exists('password_site_password', $category->getCustomFields())) { - $this->checkAuthenticated($event, $category->getId()); - } - return; - } - - $path .= $category->getId(); - $parents = array_reverse(array_slice(explode('|', $path), 1)); - foreach ($parents as $parentId) { - $parent = $this->categoryRepository - ->search(new Criteria([$parentId]), $context)->first(); - if ($parent->getCustomFields() !== null && array_key_exists('password_site_password', $parent->getCustomFields())) { - $this->checkAuthenticated($event, $parent->getId()); - break; - } - } - } } diff --git a/src/iMidiPasswordSite.php b/src/iMidiPasswordSite.php index 8db725c..14cfdab 100644 --- a/src/iMidiPasswordSite.php +++ b/src/iMidiPasswordSite.php @@ -64,7 +64,7 @@ private function createCustomFields(InstallContext $installContext) ] ]; - $customFieldSetRepository = $this->container->get('custom_field_set.repository'); + $customFieldSetRepository = $this->getCustomFieldRepository(); if (!$this->customFieldsExist($customFieldSetRepository, $installContext->getContext())) { foreach ($customFields as $customFieldSet) { @@ -94,4 +94,9 @@ private function customFieldsExist(EntityRepository $customFieldSetRepository, C return $ids->getTotal() > 0 ? $ids : null; } + private function getCustomFieldRepository() + { + return $this->container->get('custom_field_set.repository'); + } + }