Same filename and directory in other branches
- 8.9.x core/lib/Drupal/Core/EventSubscriber/AnonymousUserResponseSubscriber.php
- 9 core/lib/Drupal/Core/EventSubscriber/AnonymousUserResponseSubscriber.php
Namespace
Drupal\Core\EventSubscriber
File
core/lib/Drupal/Core/EventSubscriber/AnonymousUserResponseSubscriber.php
View source
<?php
namespace Drupal\Core\EventSubscriber;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Cache\CacheableResponseInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class AnonymousUserResponseSubscriber implements EventSubscriberInterface {
protected $currentUser;
public function __construct(AccountInterface $current_user) {
$this->currentUser = $current_user;
}
public function onRespond(ResponseEvent $event) {
if (!$event
->isMainRequest()) {
return;
}
if (!$this->currentUser
->isAnonymous()) {
return;
}
$response = $event
->getResponse();
if (!$response instanceof CacheableResponseInterface) {
return;
}
if (in_array('user.permissions', $response
->getCacheableMetadata()
->getCacheContexts())) {
$per_permissions_response_for_anon = new CacheableMetadata();
$per_permissions_response_for_anon
->setCacheTags([
'config:user.role.anonymous',
]);
$response
->addCacheableDependency($per_permissions_response_for_anon);
}
}
public static function getSubscribedEvents() : array {
$events[KernelEvents::RESPONSE][] = [
'onRespond',
5,
];
return $events;
}
}
Classes