Skip to content

Commit

Permalink
#4 Fix redirect in different languages
Browse files Browse the repository at this point in the history
  • Loading branch information
iMiMWeis committed Nov 23, 2023
1 parent 8aca0ab commit 520759e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
8 changes: 6 additions & 2 deletions src/Service/PasswordPathService.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public function __construct(
public function checkPasswordInPath(CategoryEntity $category, PageLoadedEvent|HttpCacheHitEvent $event)
{
$context = Context::createDefaultContext();
if ($event instanceof PageLoadedEvent) {
$context = $event->getContext();
}

$path = $category->getPath();
if (!$path) {
Expand Down Expand Up @@ -52,10 +55,11 @@ public function checkPasswordInPath(CategoryEntity $category, PageLoadedEvent|Ht
}
}

private function checkAuthenticated($event, string $navigationId)
private function checkAuthenticated(PageLoadedEvent|HttpCacheHitEvent $event, string $navigationId)
{
$session = $event->getRequest()->getSession();
$session->set('redirect', $event->getRequest()->server->get('REQUEST_URI'));
$redirect = $event->getSalesChannelContext() . $event->getRequest()->server->get('REQUEST_URI');
$session->set('redirect', $redirect);

if (!$session->has(self::AUTH_SESSION_PREFIX . $navigationId)) {
$this->passwordPageController->redirectToLogin($navigationId);
Expand Down
22 changes: 12 additions & 10 deletions src/Storefront/Controller/PasswordPageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace iMidiPasswordSite\Storefront\Controller;

use iMidiPasswordSite\Subscriber\CheckPasswordSubscriber;
use iMidiPasswordSite\Service\PasswordPathService;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Storefront\Controller\NavigationController;
use Shopware\Storefront\Controller\StorefrontController;
use Shopware\Storefront\Page\GenericPageLoader;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -18,13 +19,13 @@
*/
class PasswordPageController extends StorefrontController
{
private EntityRepository $categoryRepository;
private GenericPageLoader $genericPageLoader;

public function __construct(EntityRepository $categoryRepository, GenericPageLoader $genericPageLoader)
public function __construct(
private EntityRepository $categoryRepository,
private GenericPageLoader $genericPageLoader,
private NavigationController $navigationController,
)
{
$this->categoryRepository = $categoryRepository;
$this->genericPageLoader = $genericPageLoader;
}

/**
Expand All @@ -43,7 +44,7 @@ public function showLogin(Request $request, SalesChannelContext $context): Respo
/**
* @Route("/login", name="frontend.password.login", methods={"POST"})
*/
public function login(Request $request, Context $context): Response
public function login(Request $request, SalesChannelContext $context): Response
{
$navigationId = $request->request->get('navigationId');

Expand All @@ -54,14 +55,15 @@ public function login(Request $request, Context $context): Response

$password = $request->request->get('password');

$sitepassword = $this->getCategoryPassword($navigationId, $context);
$sitepassword = $this->getCategoryPassword($navigationId, $context->getContext());

if ($password === $sitepassword) {
$request->getSession()->set(CheckPasswordSubscriber::AUTH_SESSION_PREFIX . $navigationId, true);
return $this->redirect($request->getSession()->get('redirect'));
$request->getSession()->set(PasswordPathService::AUTH_SESSION_PREFIX . $navigationId, true);
return $this->navigationController->index($context, $request);
}

$this->addFlash(self::DANGER, $this->trans('imidi.password-incorrect'));

return $this->redirectToRoute('frontend.password.restricted', ['navigationId' => $navigationId]);
}

Expand Down
1 change: 1 addition & 0 deletions src/iMidiPasswordSite.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ private function createCustomFields(InstallContext $installContext)
'en-GB' => 'Password Protected Page',
'de-DE' => 'Passwortgeschützte Seite'
],
'translated' => false
],
'customFields' => [
[
Expand Down

0 comments on commit 520759e

Please sign in to comment.