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 * Defines an access checker for creating an entity of any bundle.
|
Chris@0
|
13 */
|
Chris@0
|
14 class EntityCreateAnyAccessCheck implements AccessInterface {
|
Chris@0
|
15
|
Chris@0
|
16 /**
|
Chris@0
|
17 * The entity type manager.
|
Chris@0
|
18 *
|
Chris@0
|
19 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
|
Chris@0
|
20 */
|
Chris@0
|
21 protected $entityTypeManager;
|
Chris@0
|
22
|
Chris@0
|
23 /**
|
Chris@0
|
24 * The entity type bundle info.
|
Chris@0
|
25 *
|
Chris@0
|
26 * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
|
Chris@0
|
27 */
|
Chris@0
|
28 protected $entityTypeBundleInfo;
|
Chris@0
|
29
|
Chris@0
|
30 /**
|
Chris@0
|
31 * The key used by the routing requirement.
|
Chris@0
|
32 *
|
Chris@0
|
33 * @var string
|
Chris@0
|
34 */
|
Chris@0
|
35 protected $requirementsKey = '_entity_create_any_access';
|
Chris@0
|
36
|
Chris@0
|
37 /**
|
Chris@0
|
38 * Constructs a EntityCreateAnyAccessCheck object.
|
Chris@0
|
39 *
|
Chris@0
|
40 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
|
Chris@0
|
41 * The entity type manager.
|
Chris@0
|
42 * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
|
Chris@0
|
43 * The entity type bundle info.
|
Chris@0
|
44 */
|
Chris@0
|
45 public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info) {
|
Chris@0
|
46 $this->entityTypeManager = $entity_type_manager;
|
Chris@0
|
47 $this->entityTypeBundleInfo = $entity_type_bundle_info;
|
Chris@0
|
48 }
|
Chris@0
|
49
|
Chris@0
|
50 /**
|
Chris@0
|
51 * Checks access to create an entity of any bundle for the given route.
|
Chris@0
|
52 *
|
Chris@0
|
53 * @param \Symfony\Component\Routing\Route $route
|
Chris@0
|
54 * The route to check against.
|
Chris@0
|
55 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
|
Chris@0
|
56 * The parameterized route.
|
Chris@0
|
57 * @param \Drupal\Core\Session\AccountInterface $account
|
Chris@0
|
58 * The currently logged in account.
|
Chris@0
|
59 *
|
Chris@0
|
60 * @return \Drupal\Core\Access\AccessResultInterface
|
Chris@0
|
61 * The access result.
|
Chris@0
|
62 */
|
Chris@0
|
63 public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account) {
|
Chris@0
|
64 $entity_type_id = $route->getRequirement($this->requirementsKey);
|
Chris@0
|
65 $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
|
Chris@0
|
66 $access_control_handler = $this->entityTypeManager->getAccessControlHandler($entity_type_id);
|
Chris@0
|
67
|
Chris@0
|
68 // In case there is no "bundle" entity key, check create access with no
|
Chris@0
|
69 // bundle specified.
|
Chris@0
|
70 if (!$entity_type->hasKey('bundle')) {
|
Chris@0
|
71 return $access_control_handler->createAccess(NULL, $account, [], TRUE);
|
Chris@0
|
72 }
|
Chris@0
|
73
|
Chris@0
|
74 $access = AccessResult::neutral();
|
Chris@0
|
75 $bundles = array_keys($this->entityTypeBundleInfo->getBundleInfo($entity_type_id));
|
Chris@0
|
76
|
Chris@0
|
77 // Include list cache tag as access might change if more bundles are added.
|
Chris@0
|
78 if ($entity_type->getBundleEntityType()) {
|
Chris@0
|
79 $access->addCacheTags($this->entityTypeManager->getDefinition($entity_type->getBundleEntityType())->getListCacheTags());
|
Chris@0
|
80
|
Chris@17
|
81 if (empty($route->getOption('_ignore_create_bundle_access'))) {
|
Chris@17
|
82 // Check if the user is allowed to create new bundles. If so, allow
|
Chris@17
|
83 // access, so the add page can show a link to create one.
|
Chris@17
|
84 // @see \Drupal\Core\Entity\Controller\EntityController::addPage()
|
Chris@17
|
85 $bundle_access_control_handler = $this->entityTypeManager->getAccessControlHandler($entity_type->getBundleEntityType());
|
Chris@17
|
86 $access = $access->orIf($bundle_access_control_handler->createAccess(NULL, $account, [], TRUE));
|
Chris@17
|
87 if ($access->isAllowed()) {
|
Chris@17
|
88 return $access;
|
Chris@17
|
89 }
|
Chris@0
|
90 }
|
Chris@0
|
91 }
|
Chris@0
|
92
|
Chris@0
|
93 // Check whether an entity of any bundle may be created.
|
Chris@0
|
94 foreach ($bundles as $bundle) {
|
Chris@0
|
95 $access = $access->orIf($access_control_handler->createAccess($bundle, $account, [], TRUE));
|
Chris@0
|
96 // In case there is a least one bundle user can create entities for,
|
Chris@0
|
97 // access is allowed.
|
Chris@0
|
98 if ($access->isAllowed()) {
|
Chris@0
|
99 break;
|
Chris@0
|
100 }
|
Chris@0
|
101 }
|
Chris@0
|
102
|
Chris@0
|
103 return $access;
|
Chris@0
|
104 }
|
Chris@0
|
105
|
Chris@0
|
106 }
|