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