annotate core/lib/Drupal/Core/Entity/EntityCreateAccessCheck.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
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@18 6 use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait;
Chris@0 7 use Drupal\Core\Routing\Access\AccessInterface;
Chris@0 8 use Drupal\Core\Routing\RouteMatchInterface;
Chris@0 9 use Drupal\Core\Session\AccountInterface;
Chris@0 10 use Symfony\Component\Routing\Route;
Chris@0 11
Chris@0 12 /**
Chris@0 13 * Defines an access checker for entity creation.
Chris@0 14 */
Chris@0 15 class EntityCreateAccessCheck implements AccessInterface {
Chris@18 16 use DeprecatedServicePropertyTrait;
Chris@0 17
Chris@0 18 /**
Chris@18 19 * {@inheritdoc}
Chris@18 20 */
Chris@18 21 protected $deprecatedProperties = ['entityManager' => 'entity.manager'];
Chris@18 22
Chris@18 23 /**
Chris@18 24 * The entity type manager service.
Chris@0 25 *
Chris@18 26 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
Chris@0 27 */
Chris@18 28 protected $entityTypeManager;
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_access';
Chris@0 36
Chris@0 37 /**
Chris@0 38 * Constructs a EntityCreateAccessCheck object.
Chris@0 39 *
Chris@18 40 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
Chris@18 41 * The entity type manager service.
Chris@0 42 */
Chris@18 43 public function __construct(EntityTypeManagerInterface $entity_type_manager) {
Chris@18 44 if ($entity_type_manager instanceof EntityManagerInterface) {
Chris@18 45 @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);
Chris@18 46 $this->entityTypeManager = \Drupal::entityTypeManager();
Chris@18 47 }
Chris@18 48 else {
Chris@18 49 $this->entityTypeManager = $entity_type_manager;
Chris@18 50 }
Chris@18 51 $this->entityTypeManager = $entity_type_manager;
Chris@0 52 }
Chris@0 53
Chris@0 54 /**
Chris@0 55 * Checks access to create the entity type and bundle for the given route.
Chris@0 56 *
Chris@0 57 * @param \Symfony\Component\Routing\Route $route
Chris@0 58 * The route to check against.
Chris@0 59 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
Chris@0 60 * The parametrized route.
Chris@0 61 * @param \Drupal\Core\Session\AccountInterface $account
Chris@0 62 * The currently logged in account.
Chris@0 63 *
Chris@0 64 * @return \Drupal\Core\Access\AccessResultInterface
Chris@0 65 * The access result.
Chris@0 66 */
Chris@0 67 public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account) {
Chris@0 68 list($entity_type, $bundle) = explode(':', $route->getRequirement($this->requirementsKey) . ':');
Chris@0 69
Chris@0 70 // The bundle argument can contain request argument placeholders like
Chris@0 71 // {name}, loop over the raw variables and attempt to replace them in the
Chris@0 72 // bundle name. If a placeholder does not exist, it won't get replaced.
Chris@0 73 if ($bundle && strpos($bundle, '{') !== FALSE) {
Chris@0 74 foreach ($route_match->getRawParameters()->all() as $name => $value) {
Chris@0 75 $bundle = str_replace('{' . $name . '}', $value, $bundle);
Chris@0 76 }
Chris@0 77 // If we were unable to replace all placeholders, deny access.
Chris@0 78 if (strpos($bundle, '{') !== FALSE) {
Chris@17 79 return AccessResult::neutral(sprintf("Could not find '%s' request argument, therefore cannot check create access.", $bundle));
Chris@0 80 }
Chris@0 81 }
Chris@18 82 return $this->entityTypeManager->getAccessControlHandler($entity_type)->createAccess($bundle, $account, [], TRUE);
Chris@0 83 }
Chris@0 84
Chris@0 85 }