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'); + } + }