EntityAccessCheck.php 2.4 KB

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