Upgraded drupal core with security updates
[yaffs-website] / web / core / lib / Drupal / Core / Entity / EntityCreateAccessCheck.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 entity creation.
13  */
14 class EntityCreateAccessCheck implements AccessInterface {
15
16   /**
17    * The entity manager.
18    *
19    * @var \Drupal\Core\Entity\EntityManagerInterface
20    */
21   protected $entityManager;
22
23   /**
24    * The key used by the routing requirement.
25    *
26    * @var string
27    */
28   protected $requirementsKey = '_entity_create_access';
29
30   /**
31    * Constructs a EntityCreateAccessCheck object.
32    *
33    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
34    *   The entity manager.
35    */
36   public function __construct(EntityManagerInterface $entity_manager) {
37     $this->entityManager = $entity_manager;
38   }
39
40   /**
41    * Checks access to create the entity type and bundle for the given route.
42    *
43    * @param \Symfony\Component\Routing\Route $route
44    *   The route to check against.
45    * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
46    *   The parametrized route.
47    * @param \Drupal\Core\Session\AccountInterface $account
48    *   The currently logged in account.
49    *
50    * @return \Drupal\Core\Access\AccessResultInterface
51    *   The access result.
52    */
53   public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account) {
54     list($entity_type, $bundle) = explode(':', $route->getRequirement($this->requirementsKey) . ':');
55
56     // The bundle argument can contain request argument placeholders like
57     // {name}, loop over the raw variables and attempt to replace them in the
58     // bundle name. If a placeholder does not exist, it won't get replaced.
59     if ($bundle && strpos($bundle, '{') !== FALSE) {
60       foreach ($route_match->getRawParameters()->all() as $name => $value) {
61         $bundle = str_replace('{' . $name . '}', $value, $bundle);
62       }
63       // If we were unable to replace all placeholders, deny access.
64       if (strpos($bundle, '{') !== FALSE) {
65         return AccessResult::neutral();
66       }
67     }
68     return $this->entityManager->getAccessControlHandler($entity_type)->createAccess($bundle, $account, [], TRUE);
69   }
70
71 }