annotate core/lib/Drupal/Core/Entity/EntityAccessCheck.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Core\Entity;
Chris@0 4
Chris@0 5 use Drupal\Core\Access\AccessResult;
Chris@0 6 use Drupal\Core\Routing\Access\AccessInterface;
Chris@0 7 use Drupal\Core\Routing\RouteMatchInterface;
Chris@0 8 use Drupal\Core\Session\AccountInterface;
Chris@0 9 use Symfony\Component\Routing\Route;
Chris@0 10
Chris@0 11 /**
Chris@0 12 * Provides a generic access checker for entities.
Chris@0 13 */
Chris@0 14 class EntityAccessCheck implements AccessInterface {
Chris@0 15
Chris@0 16 /**
Chris@0 17 * Checks access to the entity operation on the given route.
Chris@0 18 *
Chris@0 19 * The route's '_entity_access' requirement must follow the pattern
Chris@0 20 * 'entity_stub_name.operation', where available operations are:
Chris@0 21 * 'view', 'update', 'create', and 'delete'.
Chris@0 22 *
Chris@0 23 * For example, this route configuration invokes a permissions check for
Chris@0 24 * 'update' access to entities of type 'node':
Chris@0 25 * @code
Chris@0 26 * pattern: '/foo/{node}/bar'
Chris@0 27 * requirements:
Chris@0 28 * _entity_access: 'node.update'
Chris@0 29 * @endcode
Chris@0 30 * And this will check 'delete' access to a dynamic entity type:
Chris@0 31 * @code
Chris@0 32 * example.route:
Chris@0 33 * path: foo/{entity_type}/{example}
Chris@0 34 * requirements:
Chris@0 35 * _entity_access: example.delete
Chris@0 36 * options:
Chris@0 37 * parameters:
Chris@0 38 * example:
Chris@0 39 * type: entity:{entity_type}
Chris@0 40 * @endcode
Chris@0 41 * The route match parameter corresponding to the stub name is checked to
Chris@0 42 * see if it is entity-like i.e. implements EntityInterface.
Chris@0 43 *
Chris@0 44 * @see \Drupal\Core\ParamConverter\EntityConverter
Chris@0 45 *
Chris@0 46 * @param \Symfony\Component\Routing\Route $route
Chris@0 47 * The route to check against.
Chris@0 48 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
Chris@0 49 * The parametrized route
Chris@0 50 * @param \Drupal\Core\Session\AccountInterface $account
Chris@0 51 * The currently logged in account.
Chris@0 52 *
Chris@0 53 * @return \Drupal\Core\Access\AccessResultInterface
Chris@0 54 * The access result.
Chris@0 55 */
Chris@0 56 public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account) {
Chris@0 57 // Split the entity type and the operation.
Chris@0 58 $requirement = $route->getRequirement('_entity_access');
Chris@0 59 list($entity_type, $operation) = explode('.', $requirement);
Chris@0 60 // If $entity_type parameter is a valid entity, call its own access check.
Chris@0 61 $parameters = $route_match->getParameters();
Chris@0 62 if ($parameters->has($entity_type)) {
Chris@0 63 $entity = $parameters->get($entity_type);
Chris@0 64 if ($entity instanceof EntityInterface) {
Chris@0 65 return $entity->access($operation, $account, TRUE);
Chris@0 66 }
Chris@0 67 }
Chris@0 68 // No opinion, so other access checks should decide if access should be
Chris@0 69 // allowed or not.
Chris@0 70 return AccessResult::neutral();
Chris@0 71 }
Chris@0 72
Chris@0 73 }