vendor/shopware/storefront/Framework/Routing/StorefrontSubscriber.php line 424
],
];
}
public function startSession(): void
{
$master = $this->requestStack->getMainRequest();
if (!$master) {
return;
}
if (!$master->attributes->get(SalesChannelRequest::ATTRIBUTE_IS_SALES_CHANNEL_REQUEST)) {
return;
}
if (!$master->hasSession()) {
return;
}
$session = $master->getSession();
if (!$session->isStarted()) {
$session->setName('session-');
$session->start();
$session->set('sessionId', $session->getId());
}
$salesChannelId = $master->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_ID);
if ($salesChannelId === null) {
/** @var SalesChannelContext|null $salesChannelContext */
$salesChannelContext = $master->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
if ($salesChannelContext !== null) {
$salesChannelId = $salesChannelContext->getSalesChannel()->getId();
}
}
if ($this->shouldRenewToken($session, $salesChannelId)) {
$token = Random::getAlphanumericString(32);
$session->set(PlatformRequest::HEADER_CONTEXT_TOKEN, $token);
$session->set(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_ID, $salesChannelId);
}
$master->headers->set(
PlatformRequest::HEADER_CONTEXT_TOKEN,
$session->get(PlatformRequest::HEADER_CONTEXT_TOKEN)
);
}
public function updateSessionAfterLogin(CustomerLoginEvent $event): void
{
$token = $event->getContextToken();
$this->updateSession($token);
}
public function updateSessionAfterLogout(): void
{
$newToken = Random::getAlphanumericString(32);
$this->updateSession($newToken, true);
}
public function updateSession(string $token, bool $destroyOldSession = false): void
{
$master = $this->requestStack->getMainRequest();
if (!$master) {
return;
}
if (!$master->attributes->get(SalesChannelRequest::ATTRIBUTE_IS_SALES_CHANNEL_REQUEST)) {
return;
}
if (!$master->hasSession()) {
return;
}
$session = $master->getSession();
$session->migrate($destroyOldSession);
$session->set('sessionId', $session->getId());
$session->set(PlatformRequest::HEADER_CONTEXT_TOKEN, $token);
$master->headers->set(PlatformRequest::HEADER_CONTEXT_TOKEN, $token);
}
public function customerNotLoggedInHandler(ExceptionEvent $event): void
{
if (!$event->getRequest()->attributes->has(SalesChannelRequest::ATTRIBUTE_IS_SALES_CHANNEL_REQUEST)) {
return;
}
if (!$event->getThrowable() instanceof CustomerNotLoggedInException) {
return;
}
$request = $event->getRequest();
$parameters = [
'redirectTo' => $request->attributes->get('_route'),
'redirectParameters' => json_encode($request->attributes->get('_route_params'), \JSON_THROW_ON_ERROR),
];
$redirectResponse = new RedirectResponse($this->router->generate('frontend.account.login.page', $parameters));
$event->setResponse($redirectResponse);
}
public function maintenanceResolver(RequestEvent $event): void
{
if ($this->maintenanceModeResolver->shouldRedirect($event->getRequest())) {
$event->setResponse(
new RedirectResponse(
$this->router->generate('frontend.maintenance.page'),
RedirectResponse::HTTP_TEMPORARY_REDIRECT
)
);
}
}
public function preventPageLoadingFromXmlHttpRequest(ControllerEvent $event): void
{
if (!$event->getRequest()->isXmlHttpRequest()) {
return;
}
/** @var list<string> $scope */
$scope = $event->getRequest()->attributes->get(PlatformRequest::ATTRIBUTE_ROUTE_SCOPE, []);
if (!\in_array(StorefrontRouteScope::ID, $scope, true)) {
return;
}
/** @var callable(): Response $controller */
$controller = $event->getController();
// happens if Controller is a closure
if (!\is_array($controller)) {
return;
}
$isAllowed = $event->getRequest()->attributes->getBoolean('XmlHttpRequest');
if ($isAllowed) {
return;
}
throw new AccessDeniedHttpException('PageController can\'t be requested via XmlHttpRequest.');
}
// used to switch session token - when the context token expired
public function replaceContextToken(SalesChannelContextResolvedEvent $event): void
{
$context = $event->getSalesChannelContext();
// only update session if token expired and switched
if ($event->getUsedToken() === $context->getToken()) {
return;
}
$this->updateSession($context->getToken());
}
public function setCanonicalUrl(BeforeSendResponseEvent $event): void
{
if (!$event->getResponse()->isSuccessful()) {
return;
}
if ($canonical = $event->getRequest()->attributes->get(SalesChannelRequest::ATTRIBUTE_CANONICAL_LINK)) {
\assert(\is_string($canonical));
$canonical = sprintf('<%s>; rel="canonical"', $canonical);
$event->getResponse()->headers->set('Link', $canonical);
}
}
public function addHreflang(StorefrontRenderEvent $event): void
{
$request = $event->getRequest();
$route = $request->attributes->get('_route');
if ($route === null) {
return;
}
$routeParams = $request->attributes->get('_route_params', []);
$salesChannelContext = $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
$parameter = new HreflangLoaderParameter($route, $routeParams, $salesChannelContext);
$event->setParameter('hrefLang', $this->hreflangLoader->load($parameter));
}
public function addShopIdParameter(StorefrontRenderEvent $event): void
{
if (!$this->activeAppsLoader->getActiveApps()) {
return;
}
try {
$shopId = $this->shopIdProvider->getShopId();
} catch (AppUrlChangeDetectedException) {
return;
}
$event->setParameter('appShopId', $shopId);
}
public function addIconSetConfig(StorefrontRenderEvent $event): void
{
$request = $event->getRequest();
// get name if theme is not inherited
$theme = $request->attributes->get(SalesChannelRequest::ATTRIBUTE_THEME_NAME);
if (!$theme) {
// get theme name from base theme because for inherited themes the name is always null
$theme = $request->attributes->get(SalesChannelRequest::ATTRIBUTE_THEME_BASE_NAME);
}
if (!$theme) {
return;
}
$themeConfig = $this->themeRegistry->getConfigurations()->getByTechnicalName($theme);
if (!$themeConfig) {
return;
}
$iconConfig = [];
foreach ($themeConfig->getIconSets() as $pack => $path) {
$iconConfig[$pack] = [
'path' => $path,
'namespace' => $theme,
];
}
$event->setParameter('themeIconConfig', $iconConfig);
}
private function shouldRenewToken(SessionInterface $session, ?string $salesChannelId = null): bool
{
if (!$session->has(PlatformRequest::HEADER_CONTEXT_TOKEN) || $salesChannelId === null) {
return true;
}
if ($this->systemConfigService->get('core.systemWideLoginRegistration.isCustomerBoundToSalesChannel')) {
return $session->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_ID) !== $salesChannelId;
}
return false;
}
}