class PermissionsByEntityKernelEventSubscriber in Permissions by Term 8
Same name and namespace in other branches
- 8.2 modules/permissions_by_entity/src/EventSubscriber/PermissionsByEntityKernelEventSubscriber.php \Drupal\permissions_by_entity\EventSubscriber\PermissionsByEntityKernelEventSubscriber
Class PermissionsByEntityKernelEventSubscriber.
@package Drupal\permissions_by_entity\EventSubscriber
Hierarchy
- class \Drupal\permissions_by_entity\EventSubscriber\PermissionsByEntityKernelEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of PermissionsByEntityKernelEventSubscriber
1 string reference to 'PermissionsByEntityKernelEventSubscriber'
- permissions_by_entity.services.yml in modules/
permissions_by_entity/ permissions_by_entity.services.yml - modules/permissions_by_entity/permissions_by_entity.services.yml
1 service uses PermissionsByEntityKernelEventSubscriber
File
- modules/
permissions_by_entity/ src/ EventSubscriber/ PermissionsByEntityKernelEventSubscriber.php, line 19
Namespace
Drupal\permissions_by_entity\EventSubscriberView source
class PermissionsByEntityKernelEventSubscriber implements EventSubscriberInterface {
/**
* The access checker.
*
* @var \Drupal\permissions_by_entity\Service\AccessCheckerInterface
*/
private $accessChecker;
/**
* The core string translator.
*
* @var \Drupal\Core\StringTranslation\TranslationInterface
*/
private $translation;
/**
* The cache for checked entities.
*
* @var \Drupal\permissions_by_entity\Service\CheckedEntityCache
*/
private $checkedEntityCache;
/**
* PermissionsByEntityKernelEventSubscriber constructor.
*
* @param \Drupal\permissions_by_entity\Service\AccessCheckerInterface $access_checker
* The service to check if the current user is allowed to access an entity.
* @param \Drupal\Core\StringTranslation\TranslationInterface $translation
* The core string translator.
* @param \Drupal\permissions_by_entity\Service\CheckedEntityCache $checked_entity_cache
* The cache for checked entities.
*/
public function __construct(AccessCheckerInterface $access_checker, TranslationInterface $translation, CheckedEntityCache $checked_entity_cache) {
$this->accessChecker = $access_checker;
$this->translation = $translation;
$this->checkedEntityCache = $checked_entity_cache;
}
/**
* {@inheritdoc}
*
* @see DynamicPageCacheSubscriber
*
* This is required to run before the DynamicPageCacheSubscriber as otherwise
* the response would be cached which can lead to false access.
*/
public static function getSubscribedEvents() {
return [
KernelEvents::REQUEST => [
'onKernelRequest',
28,
],
];
}
/**
* Callback method for the KernelEvents::REQUEST event.
*
* @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
* The event instance.
*/
public function onKernelRequest(GetResponseEvent $event) {
// Get the current request from the event.
$request = $event
->getRequest();
// Get the entity.
/** @var \Drupal\Core\Entity\FieldableEntityInterface $entity */
$entity = NULL;
if ($request->attributes
->has('node')) {
$entity = $request->attributes
->get('node');
}
elseif ($request->attributes
->has('_entity')) {
$entity = $request->attributes
->get('_entity');
}
// If there is no entity abort here.
if (!$entity instanceof FieldableEntityInterface) {
return;
}
// If we already checked this entity, we do nothing.
if ($this->checkedEntityCache
->isChecked($entity)) {
return;
}
// Add this entity to the cache.
$this->checkedEntityCache
->add($entity);
// Check if the current user is allowed to access this entity.
if ($entity && $entity instanceof FieldableEntityInterface && !$this->accessChecker
->isAccessAllowed($entity)) {
// If the current user is not allowed to access this entity,
// we throw an AccessDeniedHttpException.
throw new AccessDeniedHttpException($this->translation
->translate('You are not allowed to view content of this entity type.'));
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PermissionsByEntityKernelEventSubscriber:: |
private | property | The access checker. | |
PermissionsByEntityKernelEventSubscriber:: |
private | property | The cache for checked entities. | |
PermissionsByEntityKernelEventSubscriber:: |
private | property | The core string translator. | |
PermissionsByEntityKernelEventSubscriber:: |
public static | function | This is required to run before the DynamicPageCacheSubscriber as otherwise the response would be cached which can lead to false access. | |
PermissionsByEntityKernelEventSubscriber:: |
public | function | Callback method for the KernelEvents::REQUEST event. | |
PermissionsByEntityKernelEventSubscriber:: |
public | function | PermissionsByEntityKernelEventSubscriber constructor. |