Chris@0: currentUser = $current_user; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Adds a cache tag if the 'user.permissions' cache context is present. Chris@0: * Chris@0: * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event Chris@0: * The event to process. Chris@0: */ Chris@0: public function onRespond(FilterResponseEvent $event) { Chris@0: if (!$event->isMasterRequest()) { Chris@0: return; Chris@0: } Chris@0: Chris@0: if (!$this->currentUser->isAnonymous()) { Chris@0: return; Chris@0: } Chris@0: Chris@0: $response = $event->getResponse(); Chris@0: if (!$response instanceof CacheableResponseInterface) { Chris@0: return; Chris@0: } Chris@0: Chris@0: // The 'user.permissions' cache context ensures that if the permissions for Chris@0: // a role are modified, users are not served stale render cache content. Chris@0: // But, when entire responses are cached in reverse proxies, the value for Chris@0: // the cache context is never calculated, causing the stale response to not Chris@0: // be invalidated. Therefore, when varying by permissions and the current Chris@0: // user is the anonymous user, also add the cache tag for the 'anonymous' Chris@0: // role. Chris@0: if (in_array('user.permissions', $response->getCacheableMetadata()->getCacheContexts())) { Chris@0: $per_permissions_response_for_anon = new CacheableMetadata(); Chris@0: $per_permissions_response_for_anon->setCacheTags(['config:user.role.anonymous']); Chris@0: $response->addCacheableDependency($per_permissions_response_for_anon); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Registers the methods in this class that should be listeners. Chris@0: * Chris@0: * @return array Chris@0: * An array of event listener definitions. Chris@0: */ Chris@0: public static function getSubscribedEvents() { Chris@0: // Priority 5, so that it runs before FinishResponseSubscriber, but after Chris@0: // event subscribers that add the associated cacheability metadata (which Chris@0: // have priority 10). This one is conditional, so must run after those. Chris@0: $events[KernelEvents::RESPONSE][] = ['onRespond', 5]; Chris@0: return $events; Chris@0: } Chris@0: Chris@0: }