diff core/lib/Drupal/Core/Entity/EntityCreateAccessCheck.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/core/lib/Drupal/Core/Entity/EntityCreateAccessCheck.php	Wed Nov 29 16:09:58 2017 +0000
@@ -0,0 +1,71 @@
+<?php
+
+namespace Drupal\Core\Entity;
+
+use Drupal\Core\Access\AccessResult;
+use Drupal\Core\Routing\Access\AccessInterface;
+use Drupal\Core\Routing\RouteMatchInterface;
+use Drupal\Core\Session\AccountInterface;
+use Symfony\Component\Routing\Route;
+
+/**
+ * Defines an access checker for entity creation.
+ */
+class EntityCreateAccessCheck implements AccessInterface {
+
+  /**
+   * The entity manager.
+   *
+   * @var \Drupal\Core\Entity\EntityManagerInterface
+   */
+  protected $entityManager;
+
+  /**
+   * The key used by the routing requirement.
+   *
+   * @var string
+   */
+  protected $requirementsKey = '_entity_create_access';
+
+  /**
+   * Constructs a EntityCreateAccessCheck object.
+   *
+   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
+   *   The entity manager.
+   */
+  public function __construct(EntityManagerInterface $entity_manager) {
+    $this->entityManager = $entity_manager;
+  }
+
+  /**
+   * Checks access to create the entity type and bundle for the given route.
+   *
+   * @param \Symfony\Component\Routing\Route $route
+   *   The route to check against.
+   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
+   *   The parametrized route.
+   * @param \Drupal\Core\Session\AccountInterface $account
+   *   The currently logged in account.
+   *
+   * @return \Drupal\Core\Access\AccessResultInterface
+   *   The access result.
+   */
+  public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account) {
+    list($entity_type, $bundle) = explode(':', $route->getRequirement($this->requirementsKey) . ':');
+
+    // The bundle argument can contain request argument placeholders like
+    // {name}, loop over the raw variables and attempt to replace them in the
+    // bundle name. If a placeholder does not exist, it won't get replaced.
+    if ($bundle && strpos($bundle, '{') !== FALSE) {
+      foreach ($route_match->getRawParameters()->all() as $name => $value) {
+        $bundle = str_replace('{' . $name . '}', $value, $bundle);
+      }
+      // If we were unable to replace all placeholders, deny access.
+      if (strpos($bundle, '{') !== FALSE) {
+        return AccessResult::neutral();
+      }
+    }
+    return $this->entityManager->getAccessControlHandler($entity_type)->createAccess($bundle, $account, [], TRUE);
+  }
+
+}