EntityCreateAnyAccessCheck.php 3.6 KB

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