class EntityAccessCheck

Provides a generic access checker for entities.

Hierarchy

Expanded class hierarchy of EntityAccessCheck

1 file declares its use of EntityAccessCheck
1 string reference to 'EntityAccessCheck'
core.services.yml in drupal/core/core.services.yml
drupal/core/core.services.yml
1 service uses EntityAccessCheck

File

drupal/core/lib/Drupal/Core/Entity/EntityAccessCheck.php, line 18
Contains \Drupal\Core\Entity\EntityAccessCheck.

Namespace

Drupal\Core\Entity
View source
class EntityAccessCheck implements AccessCheckInterface {

  /**
   * {@inheritdoc}
   */
  public function applies(Route $route) {
    return array_key_exists('_entity_access', $route
      ->getRequirements());
  }

  /**
   * Implements \Drupal\Core\Access\AccessCheckInterface::access().
   *
   * The value of the '_entity_access' key must be in the pattern
   * 'entity_type.operation.' The entity type must match the {entity_type}
   * parameter in the route pattern. This will check a node for 'update' access:
   * @code
   * pattern: '/foo/{node}/bar'
   * requirements:
   *   _entity_access: 'node.update'
   * @endcode
   * Available operations are 'view', 'update', 'create', and 'delete'.
   */
  public function access(Route $route, Request $request) {

    // Split the entity type and the operation.
    $requirement = $route
      ->getRequirement('_entity_access');
    list($entity_type, $operation) = explode('.', $requirement);

    // If there is valid entity of the given entity type, check its access.
    if ($request->attributes
      ->has($entity_type)) {
      $entity = $request->attributes
        ->get($entity_type);
      if ($entity instanceof EntityInterface) {
        return $entity
          ->access($operation);
      }
    }

    // No opinion, so other access checks should decide if access should be
    // allowed or not.
    return NULL;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AccessCheckInterface::ALLOW constant Grant access.
AccessCheckInterface::DENY constant Deny access.
AccessCheckInterface::KILL constant Block access.
EntityAccessCheck::access public function Implements \Drupal\Core\Access\AccessCheckInterface::access(). Overrides AccessCheckInterface::access
EntityAccessCheck::applies public function Declares whether the access check applies to a specific route or not. Overrides AccessCheckInterface::applies