Skip to content
Snippets Groups Projects
Select Git revision
  • 03f46efe541d2d77217e26fc03ce9239cc60ff95
  • 11.x default protected
  • 11.2.x protected
  • 10.6.x protected
  • 10.5.x protected
  • 11.1.x protected
  • 10.4.x protected
  • 11.0.x protected
  • 10.3.x protected
  • 7.x protected
  • 10.2.x protected
  • 10.1.x protected
  • 9.5.x protected
  • 10.0.x protected
  • 9.4.x protected
  • 9.3.x protected
  • 9.2.x protected
  • 9.1.x protected
  • 8.9.x protected
  • 9.0.x protected
  • 8.8.x protected
  • 10.5.1 protected
  • 11.2.2 protected
  • 11.2.1 protected
  • 11.2.0 protected
  • 10.5.0 protected
  • 11.2.0-rc2 protected
  • 10.5.0-rc1 protected
  • 11.2.0-rc1 protected
  • 10.4.8 protected
  • 11.1.8 protected
  • 10.5.0-beta1 protected
  • 11.2.0-beta1 protected
  • 11.2.0-alpha1 protected
  • 10.4.7 protected
  • 11.1.7 protected
  • 10.4.6 protected
  • 11.1.6 protected
  • 10.3.14 protected
  • 10.4.5 protected
  • 11.0.13 protected
41 results

EntityAccessCheck.php

Blame
  • Nathaniel Catchpole's avatar
    Issue #2778809 by chx: _entity_access is actually usable but you'd never tell...
    catch authored
    Issue #2778809 by chx: _entity_access is actually usable but you'd never tell that from the class doxygen
    03f46efe
    History
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    EntityAccessCheck.php 2.21 KiB
    <?php
    
    namespace Drupal\Core\Entity;
    
    use Drupal\Core\Access\AccessResult;
    use Drupal\Core\Routing\Access\AccessInterface;
    use Drupal\Core\Routing\RouteMatchInterface;
    use Drupal\Core\Session\AccountInterface;
    use Symfony\Component\Routing\Route;
    
    /**
     * Provides a generic access checker for entities.
     */
    class EntityAccessCheck implements AccessInterface {
    
      /**
       * Checks access to the entity operation on the given route.
       *
       * The value of the '_entity_access' key must be in the pattern
       * 'entity_slug_name.operation.' For example, this will check a node for
       * 'update' access:
       * @code
       * pattern: '/foo/{node}/bar'
       * requirements:
       *   _entity_access: 'node.update'
       * @endcode
       * And this will check a dynamic entity type:
       * @code
       * example.route:
       *   path: foo/{entity_type}/{example}
       *   requirements:
       *     _entity_access: example.update
       *   options:
       *     parameters:
       *       example:
       *         type: entity:{entity_type}
       * @endcode
       * @see \Drupal\Core\ParamConverter\EntityConverter
       *
       * Available operations are 'view', 'update', 'create', and 'delete'.
       *
       * @param \Symfony\Component\Routing\Route $route
       *   The route to check against.
       * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
       *   The parametrized route
       * @param \Drupal\Core\Session\AccountInterface $account
       *   The currently logged in account.
       *
       * @return \Drupal\Core\Access\AccessResultInterface
       *   The access result.
       */
      public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account) {
        // 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.
        $parameters = $route_match->getParameters();
        if ($parameters->has($entity_type)) {
          $entity = $parameters->get($entity_type);
          if ($entity instanceof EntityInterface) {
            return $entity->access($operation, $account, TRUE);
          }
        }
        // No opinion, so other access checks should decide if access should be
        // allowed or not.
        return AccessResult::neutral();
      }
    
    }