|
27 | 27 | use Enlight\Event\SubscriberInterface; |
28 | 28 | use Enlight_Controller_Request_RequestHttp as Request; |
29 | 29 | use Enlight_Controller_Response_ResponseHttp as Response; |
| 30 | +use Shopware\Bundle\CookieBundle\CookieCollection; |
30 | 31 | use Shopware\Bundle\CookieBundle\CookieGroupCollection; |
31 | 32 | use Shopware\Bundle\CookieBundle\Services\CookieHandler; |
32 | 33 | use Shopware\Bundle\CookieBundle\Services\CookieHandlerInterface; |
| 34 | +use Shopware\Bundle\CookieBundle\Structs\CookieStruct; |
33 | 35 | use Shopware_Components_Config as Config; |
34 | 36 | use Symfony\Component\HttpFoundation\Cookie; |
35 | 37 |
|
@@ -117,6 +119,8 @@ private function removeCookiesFromPreferences(Request $request, Response $respon |
117 | 119 |
|
118 | 120 | $preferences = json_decode($preferences, true); |
119 | 121 |
|
| 122 | + $preferences = $this->removeInvalidCookiesFromPreferences($request, $response, $preferences); |
| 123 | + |
120 | 124 | $this->removeCookies($request, $response, function (string $cookieName) use ($preferences) { |
121 | 125 | return $this->cookieHandler->isCookieAllowedByPreferences($cookieName, $preferences); |
122 | 126 | }); |
@@ -165,6 +169,43 @@ private function removeCookies(Request $request, Response $response, callable $v |
165 | 169 | } |
166 | 170 | } |
167 | 171 |
|
| 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 | + |
168 | 209 | private function convertToArray(CookieGroupCollection $cookieGroupCollection): array |
169 | 210 | { |
170 | 211 | return json_decode(json_encode($cookieGroupCollection), true); |
|
0 commit comments