Skip to content

Commit 7810579

Browse files
committed
Merge branch 'sw-25013/5.6.4/fix-moving-slt-cookie' into '5.6.4'
SW-25013 - Fix moving the SLT cookie by adding a validity check See merge request shopware/5/product/shopware!216
2 parents 405f3ea + 27e2171 commit 7810579

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
lines changed

engine/Shopware/Components/Privacy/CookieRemoveSubscriber.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@
2727
use Enlight\Event\SubscriberInterface;
2828
use Enlight_Controller_Request_RequestHttp as Request;
2929
use Enlight_Controller_Response_ResponseHttp as Response;
30+
use Shopware\Bundle\CookieBundle\CookieCollection;
3031
use Shopware\Bundle\CookieBundle\CookieGroupCollection;
3132
use Shopware\Bundle\CookieBundle\Services\CookieHandler;
3233
use Shopware\Bundle\CookieBundle\Services\CookieHandlerInterface;
34+
use Shopware\Bundle\CookieBundle\Structs\CookieStruct;
3335
use Shopware_Components_Config as Config;
3436
use Symfony\Component\HttpFoundation\Cookie;
3537

@@ -117,6 +119,8 @@ private function removeCookiesFromPreferences(Request $request, Response $respon
117119

118120
$preferences = json_decode($preferences, true);
119121

122+
$preferences = $this->removeInvalidCookiesFromPreferences($request, $response, $preferences);
123+
120124
$this->removeCookies($request, $response, function (string $cookieName) use ($preferences) {
121125
return $this->cookieHandler->isCookieAllowedByPreferences($cookieName, $preferences);
122126
});
@@ -165,6 +169,43 @@ private function removeCookies(Request $request, Response $response, callable $v
165169
}
166170
}
167171

172+
private function removeInvalidCookiesFromPreferences(Request $request, Response $response, array $preferences): array
173+
{
174+
$allowedCookies = $this->cookieHandler->getCookies();
175+
176+
foreach ($preferences['groups'] as $group) {
177+
foreach ($group['cookies'] as $cookie) {
178+
$cookieCollection = $allowedCookies->getGroupByName($group['name'])->getCookies();
179+
180+
if ($this->hasCookieWithTechnicalName($cookieCollection, $cookie['name'])) {
181+
continue;
182+
}
183+
184+
unset($preferences['groups'][$group['name']]['cookies'][$cookie['name']]);
185+
$this->setNewPreferencesCookie($request, $response, $preferences);
186+
}
187+
}
188+
189+
return $preferences;
190+
}
191+
192+
private function hasCookieWithTechnicalName(CookieCollection $cookieCollection, string $technicalName): bool
193+
{
194+
return $cookieCollection->exists(static function (string $key, CookieStruct $cookieStruct) use ($technicalName) {
195+
return $cookieStruct->getName() === $technicalName;
196+
});
197+
}
198+
199+
private function setNewPreferencesCookie(Request $request, Response $response, array $preferences): void
200+
{
201+
$expire = new \DateTime();
202+
$expire->modify('+180 day');
203+
204+
$response->headers->setCookie(
205+
new Cookie(CookieHandler::PREFERENCES_COOKIE_NAME, json_encode($preferences), $expire, $request->getBasePath() . '/', null, false, false, true)
206+
);
207+
}
208+
168209
private function convertToArray(CookieGroupCollection $cookieGroupCollection): array
169210
{
170211
return json_decode(json_encode($cookieGroupCollection), true);

tests/Functional/Components/Privacy/CookieRemoveSubscriberTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,18 +180,18 @@ public function testPostDispatchShouldRemoveCookiesDueToPreferences(): void
180180
$_COOKIE[CookieHandler::PREFERENCES_COOKIE_NAME] = json_encode([
181181
'groups' => [
182182
[
183-
'name' => 'foo',
183+
'name' => 'comfort',
184184
'cookies' => [
185185
[
186-
'name' => 'removeMe',
186+
'name' => 'sUniqueID',
187187
'active' => false,
188188
],
189189
],
190190
],
191191
],
192192
]);
193193
$controller = $this->getController();
194-
$controller->Response()->setCookie('removeMe', 'foo');
194+
$controller->Response()->setCookie('sUniqueID', 'foo');
195195

196196
$cookieRemoveSubscriber->onPostDispatch($this->getEventArgs($controller));
197197
$responseCookies = $controller->Response()->getCookies();

themes/Frontend/Responsive/frontend/_public/src/js/jquery.cookie-consent-manager.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@
206206

207207
parsePreferences: function () {
208208
var me = this,
209-
groupNames = Object.keys(this.preferences['groups']),
209+
groupNames = Object.keys(me.preferences['groups']),
210210
group,
211211
groupRequired,
212212
cookieNames,
@@ -223,8 +223,8 @@
223223
cookie = me.findCookieByName(cookieName);
224224
me.toggleCookie(cookie, groupRequired || me.preferences['groups'][groupName].cookies[cookieName].active);
225225

226-
me.checkActiveStateForAllCookiesOfGroup(group, me.preferences['groups'][groupName].cookies[cookieName].active);
227-
})
226+
me.checkActiveStateForAllCookiesOfGroup(group, groupRequired || me.preferences['groups'][groupName].cookies[cookieName].active);
227+
});
228228
});
229229
},
230230

0 commit comments

Comments
 (0)