AssetInjectorAccessControlHandler.php in Asset Injector 8.2
File
src/AssetInjectorAccessControlHandler.php
View source
<?php
namespace Drupal\asset_injector;
use Drupal\Component\Plugin\Exception\ContextException;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Condition\ConditionAccessResolverTrait;
use Drupal\Core\Entity\EntityAccessControlHandler;
use Drupal\Core\Entity\EntityHandlerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Plugin\Context\ContextHandlerInterface;
use Drupal\Core\Plugin\Context\ContextRepositoryInterface;
use Drupal\Core\Plugin\ContextAwarePluginInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class AssetInjectorAccessControlHandler extends EntityAccessControlHandler implements EntityHandlerInterface {
use ConditionAccessResolverTrait;
protected $contextHandler;
protected $contextRepository;
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('context.handler'), $container
->get('context.repository'));
}
public function __construct(EntityTypeInterface $entity_type, ContextHandlerInterface $context_handler, ContextRepositoryInterface $context_repository) {
parent::__construct($entity_type);
$this->contextHandler = $context_handler;
$this->contextRepository = $context_repository;
}
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
if ($operation != 'view') {
return parent::checkAccess($entity, $operation, $account);
}
if (!$entity
->status()) {
return AccessResult::forbidden()
->addCacheableDependency($entity);
}
else {
$conditions = [];
foreach ($entity
->getConditionsCollection() as $condition_id => $condition) {
if ($condition_id == 'current_theme') {
continue;
}
if ($condition instanceof ContextAwarePluginInterface) {
try {
$contexts = $this->contextRepository
->getRuntimeContexts(array_values($condition
->getContextMapping()));
$this->contextHandler
->applyContextMapping($condition, $contexts);
} catch (ContextException $e) {
}
}
$conditions[$condition_id] = $condition;
}
$logic = $entity->conditions_require_all || empty($conditions) ? 'and' : 'or';
$conditions_allowed = $this
->resolveConditions($conditions, $logic);
$themes_allowed = $this
->resolveThemeConditions($entity);
if ($entity
->getConditionsCollection()
->has('current_theme')) {
$access = AccessResult::forbidden();
if ($logic == 'and' && ($conditions_allowed && $themes_allowed) || $logic == 'or' && ($conditions_allowed || $themes_allowed)) {
$access = AccessResult::allowed();
}
}
else {
$access = $conditions_allowed ? AccessResult::allowed() : AccessResult::forbidden();
}
$this
->mergeCacheabilityFromConditions($access, $conditions);
return $access
->addCacheableDependency($entity);
}
}
protected function resolveThemeConditions(AssetInjectorInterface $entity) {
$conditions = $entity
->getConditionsCollection();
$theme_conditions = [];
if (!$conditions
->has('current_theme')) {
return TRUE;
}
$theme_condition = $conditions
->get('current_theme');
$config = $theme_condition
->getConfig();
foreach ($config['theme'] as $theme) {
$new_theme_conditions = clone $theme_condition;
$new_theme_conditions
->setConfig('theme', $theme);
$conditions
->set("current_theme_{$theme}", $new_theme_conditions);
$theme_conditions[] = $new_theme_conditions;
}
$logic = $config['negate'] ? 'and' : 'or';
return $this
->resolveConditions($theme_conditions, $logic);
}
protected function mergeCacheabilityFromConditions(AccessResult $access, array $conditions) {
foreach ($conditions as $condition) {
if ($condition instanceof CacheableDependencyInterface) {
$access
->addCacheTags($condition
->getCacheTags());
$access
->addCacheContexts($condition
->getCacheContexts());
$access
->setCacheMaxAge(Cache::mergeMaxAges($access
->getCacheMaxAge(), $condition
->getCacheMaxAge()));
}
}
}
}