From 9be3ce88584f26661ecbbb0566dbcee3ef3fff7b Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Sun, 15 Sep 2013 12:53:54 +0100 Subject: [PATCH] Issue #2041333 by tstoeckler, tim.plunkett: Inject the module handler into the entity access controller. --- .../Core/Entity/EntityAccessController.php | 20 +++++++++++++++++-- .../EntityAccessControllerInterface.php | 12 +++++++++++ core/lib/Drupal/Core/Entity/EntityManager.php | 6 +++++- .../lib/Drupal/node/NodeAccessController.php | 16 ++------------- 4 files changed, 37 insertions(+), 17 deletions(-) diff --git a/core/lib/Drupal/Core/Entity/EntityAccessController.php b/core/lib/Drupal/Core/Entity/EntityAccessController.php index 4658d50f197..47573c20a6c 100644 --- a/core/lib/Drupal/Core/Entity/EntityAccessController.php +++ b/core/lib/Drupal/Core/Entity/EntityAccessController.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Entity; +use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Language\Language; use Drupal\Core\Session\AccountInterface; @@ -29,6 +30,13 @@ class EntityAccessController implements EntityAccessControllerInterface { */ protected $entityType; + /** + * The module handler service. + * + * @var \Drupal\Core\Extension\ModuleHandlerInterface + */ + protected $moduleHandler; + /** * Constructs an access controller instance. * @@ -58,7 +66,7 @@ class EntityAccessController implements EntityAccessControllerInterface { // We grant access to the entity if both of these conditions are met: // - No modules say to deny access. // - At least one module says to grant access. - $access = module_invoke_all($entity->entityType() . '_access', $entity, $operation, $account, $langcode); + $access = $this->moduleHandler->invokeAll($entity->entityType() . '_access', array($entity, $operation, $account, $langcode)); if (($return = $this->processAccessHookResults($access)) === NULL) { // No module had an opinion about the access, so let's the access @@ -196,7 +204,7 @@ class EntityAccessController implements EntityAccessControllerInterface { // We grant access to the entity if both of these conditions are met: // - No modules say to deny access. // - At least one module says to grant access. - $access = module_invoke_all($this->entity_type . '_create_access', $account, $context['langcode']); + $access = $this->moduleHandler->invokeAll($this->entity_type . '_create_access', array($account, $context['langcode'])); if (($return = $this->processAccessHookResults($access)) === NULL) { // No module had an opinion about the access, so let's the access @@ -244,4 +252,12 @@ class EntityAccessController implements EntityAccessControllerInterface { return $account; } + /** + * {@inheritdoc} + */ + public function setModuleHandler(ModuleHandlerInterface $module_handler) { + $this->moduleHandler = $module_handler; + return $this; + } + } diff --git a/core/lib/Drupal/Core/Entity/EntityAccessControllerInterface.php b/core/lib/Drupal/Core/Entity/EntityAccessControllerInterface.php index c0296dd99dc..749b9a03fc7 100644 --- a/core/lib/Drupal/Core/Entity/EntityAccessControllerInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityAccessControllerInterface.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Entity; +use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Language\Language; use Drupal\Core\Session\AccountInterface; @@ -58,4 +59,15 @@ interface EntityAccessControllerInterface { */ public function resetCache(); + /** + * Sets the module handler for this form. + * + * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler + * The module handler. + * + * @return self + * The entity access controller. + */ + public function setModuleHandler(ModuleHandlerInterface $module_handler); + } diff --git a/core/lib/Drupal/Core/Entity/EntityManager.php b/core/lib/Drupal/Core/Entity/EntityManager.php index 85cce4c734e..fc45202dcbf 100644 --- a/core/lib/Drupal/Core/Entity/EntityManager.php +++ b/core/lib/Drupal/Core/Entity/EntityManager.php @@ -329,7 +329,11 @@ class EntityManager extends PluginManagerBase { * A access controller instance. */ public function getAccessController($entity_type) { - return $this->getController($entity_type, 'access'); + if (!isset($this->controllers['access'][$entity_type])) { + $controller = $this->getController($entity_type, 'access'); + $controller->setModuleHandler($this->moduleHandler); + } + return $this->controllers['access'][$entity_type]; } /** diff --git a/core/modules/node/lib/Drupal/node/NodeAccessController.php b/core/modules/node/lib/Drupal/node/NodeAccessController.php index b1f120aeeb4..9b5a40321c5 100644 --- a/core/modules/node/lib/Drupal/node/NodeAccessController.php +++ b/core/modules/node/lib/Drupal/node/NodeAccessController.php @@ -10,7 +10,6 @@ namespace Drupal\node; use Drupal\Core\Database\Connection; use Drupal\Core\Database\Query\SelectInterface; use Drupal\Core\Entity\EntityControllerInterface; -use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Language\Language; use Drupal\Core\Entity\EntityAccessController; use Drupal\Core\Entity\EntityInterface; @@ -31,13 +30,6 @@ class NodeAccessController extends EntityAccessController implements NodeAccessC */ protected $grantStorage; - /** - * The module handler. - * - * @var \Drupal\Core\Extension\ModuleHandlerInterface - */ - protected $moduleHandler; - /** * Constructs a NodeAccessController object. * @@ -45,13 +37,10 @@ class NodeAccessController extends EntityAccessController implements NodeAccessC * The entity type of the access controller instance. * @param \Drupal\node\NodeGrantDatabaseStorageInterface $grant_storage * The node grant storage. - * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler - * The module handler to invoke the alter hook with. */ - public function __construct($entity_type, NodeGrantDatabaseStorageInterface $grant_storage, ModuleHandlerInterface $module_handler) { + public function __construct($entity_type, NodeGrantDatabaseStorageInterface $grant_storage) { parent::__construct($entity_type); $this->grantStorage = $grant_storage; - $this->moduleHandler = $module_handler; } /** @@ -60,8 +49,7 @@ class NodeAccessController extends EntityAccessController implements NodeAccessC public static function createInstance(ContainerInterface $container, $entity_type, array $entity_info) { return new static( $entity_type, - $container->get('node.grant_storage'), - $container->get('module_handler') + $container->get('node.grant_storage') ); }