EntityCreateAccessCheck.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace Drupal\Core\Entity;
  3. use Drupal\Core\Access\AccessResult;
  4. use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait;
  5. use Drupal\Core\Routing\Access\AccessInterface;
  6. use Drupal\Core\Routing\RouteMatchInterface;
  7. use Drupal\Core\Session\AccountInterface;
  8. use Symfony\Component\Routing\Route;
  9. /**
  10. * Defines an access checker for entity creation.
  11. */
  12. class EntityCreateAccessCheck implements AccessInterface {
  13. use DeprecatedServicePropertyTrait;
  14. /**
  15. * {@inheritdoc}
  16. */
  17. protected $deprecatedProperties = ['entityManager' => 'entity.manager'];
  18. /**
  19. * The entity type manager service.
  20. *
  21. * @var \Drupal\Core\Entity\EntityTypeManagerInterface
  22. */
  23. protected $entityTypeManager;
  24. /**
  25. * The key used by the routing requirement.
  26. *
  27. * @var string
  28. */
  29. protected $requirementsKey = '_entity_create_access';
  30. /**
  31. * Constructs a EntityCreateAccessCheck object.
  32. *
  33. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
  34. * The entity type manager service.
  35. */
  36. public function __construct(EntityTypeManagerInterface $entity_type_manager) {
  37. if ($entity_type_manager instanceof EntityManagerInterface) {
  38. @trigger_error('Passing the entity.manager service to EntityCreateAccessCheck::__construct() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Pass the new dependencies instead. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
  39. $this->entityTypeManager = \Drupal::entityTypeManager();
  40. }
  41. else {
  42. $this->entityTypeManager = $entity_type_manager;
  43. }
  44. $this->entityTypeManager = $entity_type_manager;
  45. }
  46. /**
  47. * Checks access to create the entity type and bundle for the given route.
  48. *
  49. * @param \Symfony\Component\Routing\Route $route
  50. * The route to check against.
  51. * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
  52. * The parametrized route.
  53. * @param \Drupal\Core\Session\AccountInterface $account
  54. * The currently logged in account.
  55. *
  56. * @return \Drupal\Core\Access\AccessResultInterface
  57. * The access result.
  58. */
  59. public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account) {
  60. list($entity_type, $bundle) = explode(':', $route->getRequirement($this->requirementsKey) . ':');
  61. // The bundle argument can contain request argument placeholders like
  62. // {name}, loop over the raw variables and attempt to replace them in the
  63. // bundle name. If a placeholder does not exist, it won't get replaced.
  64. if ($bundle && strpos($bundle, '{') !== FALSE) {
  65. foreach ($route_match->getRawParameters()->all() as $name => $value) {
  66. $bundle = str_replace('{' . $name . '}', $value, $bundle);
  67. }
  68. // If we were unable to replace all placeholders, deny access.
  69. if (strpos($bundle, '{') !== FALSE) {
  70. return AccessResult::neutral(sprintf("Could not find '%s' request argument, therefore cannot check create access.", $bundle));
  71. }
  72. }
  73. return $this->entityTypeManager->getAccessControlHandler($entity_type)->createAccess($bundle, $account, [], TRUE);
  74. }
  75. }