Version 1
[yaffs-website] / web / core / lib / Drupal / Core / Entity / EntityAccessCheck.php
1 <?php
2
3 namespace Drupal\Core\Entity;
4
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;
10
11 /**
12  * Provides a generic access checker for entities.
13  */
14 class EntityAccessCheck implements AccessInterface {
15
16   /**
17    * Checks access to the entity operation on the given route.
18    *
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'.
22    *
23    * For example, this route configuration invokes a permissions check for
24    * 'update' access to entities of type 'node':
25    * @code
26    * pattern: '/foo/{node}/bar'
27    * requirements:
28    *   _entity_access: 'node.update'
29    * @endcode
30    * And this will check 'delete' access to a dynamic entity type:
31    * @code
32    * example.route:
33    *   path: foo/{entity_type}/{example}
34    *   requirements:
35    *     _entity_access: example.delete
36    *   options:
37    *     parameters:
38    *       example:
39    *         type: entity:{entity_type}
40    * @endcode
41    * The route match parameter corresponding to the stub name is checked to
42    * see if it is entity-like i.e. implements EntityInterface.
43    *
44    * @see \Drupal\Core\ParamConverter\EntityConverter
45    *
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.
52    *
53    * @return \Drupal\Core\Access\AccessResultInterface
54    *   The access result.
55    */
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);
66       }
67     }
68     // No opinion, so other access checks should decide if access should be
69     // allowed or not.
70     return AccessResult::neutral();
71   }
72
73 }