3 namespace Drupal\Core\Entity;
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Routing\Access\AccessInterface;
7 use Drupal\Core\Routing\RouteMatchInterface;
8 use Drupal\Core\Session\AccountInterface;
9 use Symfony\Component\Routing\Route;
12 * Provides a generic access checker for entities.
14 class EntityAccessCheck implements AccessInterface {
17 * Checks access to the entity operation on the given route.
19 * The route's '_entity_access' requirement must follow the pattern
20 * 'entity_stub_name.operation', where available operations are:
21 * 'view', 'update', 'create', and 'delete'.
23 * For example, this route configuration invokes a permissions check for
24 * 'update' access to entities of type 'node':
26 * pattern: '/foo/{node}/bar'
28 * _entity_access: 'node.update'
30 * And this will check 'delete' access to a dynamic entity type:
33 * path: foo/{entity_type}/{example}
35 * _entity_access: example.delete
39 * type: entity:{entity_type}
41 * The route match parameter corresponding to the stub name is checked to
42 * see if it is entity-like i.e. implements EntityInterface.
44 * @see \Drupal\Core\ParamConverter\EntityConverter
46 * @param \Symfony\Component\Routing\Route $route
47 * The route to check against.
48 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
49 * The parametrized route
50 * @param \Drupal\Core\Session\AccountInterface $account
51 * The currently logged in account.
53 * @return \Drupal\Core\Access\AccessResultInterface
56 public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account) {
57 // Split the entity type and the operation.
58 $requirement = $route->getRequirement('_entity_access');
59 list($entity_type, $operation) = explode('.', $requirement);
60 // If $entity_type parameter is a valid entity, call its own access check.
61 $parameters = $route_match->getParameters();
62 if ($parameters->has($entity_type)) {
63 $entity = $parameters->get($entity_type);
64 if ($entity instanceof EntityInterface) {
65 return $entity->access($operation, $account, TRUE);
68 // No opinion, so other access checks should decide if access should be
70 return AccessResult::neutral();