PermissionHandler

Provides the available permissions based on yml files.

To define permissions you can use a $module.permissions.yml file. This file defines machine names, human-readable names, restrict access (if required for security warning), and optionally descriptions for each permission type. The machine names are the canonical way to refer to permissions for access checking.

If your module needs to define dynamic permissions you can use the permission_callbacks key to declare a callable that will return an array of permissions, keyed by machine name. Each item in the array can contain the same keys as an entry in $module.permissions.yml.

Here is an example from the core filter module (comments have been added):

# The key is the permission machine name, and is required.
administer filters:
  # (required) Human readable name of the permission used in the UI.
  title: 'Administer text formats and filters'
  # (optional) Additional description fo the permission used in the UI.
  description: 'Define how text is handled by combining filters into text formats.'
  # (optional) Boolean, when set to true a warning about site security will
  # be displayed on the Permissions page. Defaults to false.
  restrict access: false

# An array of callables used to generate dynamic permissions.
permission_callbacks:
  # Each item in the array should return an associative array with one or
  # more permissions following the same keys as the permission defined above.
  - Drupal\filter\FilterPermissions::permissions

Hierarchy

See also

filter.permissions.yml

\Drupal\filter\FilterPermissions

User accounts, permissions, and roles

File

core/modules/user/src/PermissionHandler.php, line 48

Namespace

Drupal\user

Members

Name Modifiers Type Description
PermissionHandler::$controllerResolver protected property The controller resolver.
PermissionHandler::$moduleHandler protected property The module handler.
PermissionHandler::$yamlDiscovery protected property The YAML discovery class to find all .permissions.yml files.
PermissionHandler::buildPermissionsYaml protected function Builds all permissions provided by .permissions.yml files.
PermissionHandler::getModuleNames protected function Returns all module names.
PermissionHandler::getPermissions public function Gets all available permissions. Overrides PermissionHandlerInterface::getPermissions
PermissionHandler::getYamlDiscovery protected function Gets the YAML discovery.
PermissionHandler::moduleProvidesPermissions public function Determines whether a module provides some permissions. Overrides PermissionHandlerInterface::moduleProvidesPermissions
PermissionHandler::sortPermissions protected function Sorts the given permissions by provider name and title.
PermissionHandler::systemRebuildModuleData protected function Wraps system_rebuild_module_data()
PermissionHandler::__construct public function Constructs a new PermissionHandler.
StringTranslationTrait::$stringTranslation protected property The string translation service.
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use.
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
doc_Drupal
2016-10-29 09:33:39
Comments
Leave a Comment

Please login to continue.