access_policy-1.0.x-dev/src/AccessPolicyAccessControlHandler.php
src/AccessPolicyAccessControlHandler.php
<?php namespace Drupal\access_policy; use Drupal\Core\Access\AccessResult; use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\CacheableDependencyInterface; use Drupal\Core\Condition\ConditionAccessResolverTrait; use Drupal\Core\Condition\ConditionManager; 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\ContextHandler; use Drupal\Core\Plugin\Context\ContextRepositoryInterface; use Drupal\Core\Session\AccountInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Access controller for the AccessPolicy entity. * * @see \Drupal\access_policy\Entity\AccessPolicy. */ class AccessPolicyAccessControlHandler extends EntityAccessControlHandler implements EntityHandlerInterface { use ConditionAccessResolverTrait; /** * The condition manager. * * @var \Drupal\Core\Condition\ConditionManager */ protected $conditionManager; /** * The context handler. * * @var \Drupal\Core\Plugin\Context\ContextHandler */ protected $contextHandler; /** * The context repository. * * @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface */ protected $contextRepository; /** * {@inheritdoc} */ public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) { return new static( $entity_type, $container->get('plugin.manager.condition'), $container->get('context.handler'), $container->get('context.repository') ); } /** * AccessPolicyAccessControlHandler constructor. * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type * The entity type definition. * @param \Drupal\Core\Condition\ConditionManager $condition_manager * The condition plugin manager. * @param \Drupal\Core\Plugin\Context\ContextHandler $context_handler * The context handler. * @param \Drupal\Core\Plugin\Context\ContextRepositoryInterface $context_repository * The context repository. */ public function __construct(EntityTypeInterface $entity_type, ConditionManager $condition_manager, ContextHandler $context_handler, ContextRepositoryInterface $context_repository) { parent::__construct($entity_type); $this->conditionManager = $condition_manager; $this->contextHandler = $context_handler; $this->contextRepository = $context_repository; } /** * {@inheritdoc} */ protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) { // If the access policy is required (such as with the "Me only" policy), do // not allow it to be deleted. return AccessResult::allowedIfHasPermission($account, 'administer access policy entities'); } /** * Merges cacheable metadata from conditions onto the access result object. * * @param \Drupal\Core\Access\AccessResult $access * The access result object. * @param \Drupal\Core\Condition\ConditionInterface[] $conditions * List of conditions. */ 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())); } } } /** * {@inheritdoc} */ protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) { return AccessResult::allowedIfHasPermission($account, 'administer access policy entities'); } }