diff --git a/core/modules/user/src/PermissionHandler.php b/core/modules/user/src/PermissionHandler.php index d41b05e..a574cc3 100644 --- a/core/modules/user/src/PermissionHandler.php +++ b/core/modules/user/src/PermissionHandler.php @@ -51,6 +51,13 @@ class PermissionHandler implements PermissionHandlerInterface { protected $controllerResolver; /** + * Array of permissions keyed by module name. + * + * @var array + */ + protected $permisisonsCache; + + /** * Constructs a new PermissionHandler. * * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler @@ -84,8 +91,8 @@ protected function getYamlDiscovery() { /** * {@inheritdoc} */ - public function getPermissions() { - $all_permissions = $this->buildPermissionsYaml(); + public function getPermissions($module_name = NULL) { + $all_permissions = $this->buildPermissionsYaml($module_name); return $this->sortPermissions($all_permissions); } @@ -94,32 +101,49 @@ public function getPermissions() { * {@inheritdoc} */ public function moduleProvidesPermissions($module_name) { - // @TODO Static cache this information, see - // https://www.drupal.org/node/2339487 - $permissions = $this->getPermissions(); - + if (!isset($this->permisisonsCache[$module_name])) { + $permissions = $this->getPermissions($module_name); + } + $this->permisisonsCache[$module_name] = FALSE; foreach ($permissions as $permission) { if ($permission['provider'] == $module_name) { - return TRUE; + $this->permisisonsCache[$module_name] = TRUE; + break; } } - return FALSE; + return $this->permisisonsCache[$module_name]; } /** * Builds all permissions provided by .permissions.yml files. * - * @return array[] - * Each return permission is an array with the following keys: - * - title: The title of the permission. - * - description: The description of the permission, defaults to NULL. - * - provider: The provider of the permission. + * @param string $module_name + * (optional) The module to build permissions YAML for. If omitted, builds + * permissions for all modules. + * @return \array[] Each return permission is an array with the following keys: + * Each return permission is an array with the following keys: + * - title: The title of the permission. + * - description: The description of the permission, defaults to NULL. + * - provider: The provider of the permission. */ - protected function buildPermissionsYaml() { + protected function buildPermissionsYaml($module_name = NULL) { $all_permissions = array(); $all_callback_permissions = array(); - foreach ($this->getYamlDiscovery()->findAll() as $provider => $permissions) { + if ($module_name) { + // Just discover the single module. + $directories = $this->moduleHandler->getModuleDirectories(); + $module_directories = []; + if (!empty($directories[$module_name])) { + $module_directories = [$module_name => $directories[$module_name]]; + } + $discovery = new YamlDiscovery('permissions', $module_directories); + } + else { + // Use the default discovery - which handles all enabled modules. + $discovery = $this->getYamlDiscovery(); + } + foreach ($discovery->findAll() as $provider => $permissions) { // The top-level 'permissions_callback' is a list of methods in controller // syntax, see \Drupal\Core\Controller\ControllerResolver. These methods // should return an array of permissions in the same structure. diff --git a/core/modules/user/src/PermissionHandlerInterface.php b/core/modules/user/src/PermissionHandlerInterface.php index 269af11..9a54996 100644 --- a/core/modules/user/src/PermissionHandlerInterface.php +++ b/core/modules/user/src/PermissionHandlerInterface.php @@ -15,6 +15,10 @@ /** * Gets all available permissions. * + * @param string $module_name + * (optional) The module name to get permissions for. If omitted, gets + * permissions for all modules. + * * @return array * An array whose keys are permission names and whose corresponding values * are arrays containing the following key-value pairs: @@ -41,7 +45,7 @@ * information that is specific to the permission you are defining. * - provider: (optional) The provider name of the permission. */ - public function getPermissions(); + public function getPermissions($module_name = NULL); /** * Determines whether a module provides some permissions. @@ -55,4 +59,3 @@ public function getPermissions(); public function moduleProvidesPermissions($module_name); } - diff --git a/core/modules/user/tests/src/Unit/PermissionHandlerTest.php b/core/modules/user/tests/src/Unit/PermissionHandlerTest.php index 1180846..cd7fbcc 100644 --- a/core/modules/user/tests/src/Unit/PermissionHandlerTest.php +++ b/core/modules/user/tests/src/Unit/PermissionHandlerTest.php @@ -92,7 +92,7 @@ public function testBuildPermissionsYaml() { vfsStreamWrapper::setRoot($root); $this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface'); - $this->moduleHandler->expects($this->once()) + $this->moduleHandler->expects($this->any()) ->method('getModuleDirectories') ->willReturn(array( 'module_a' => vfsStream::url('modules/module_a'),