How to create custom permissions on Drupal?
We may need to create permissions that do not exist natively on Drupal or that are not precise enough. For example, Drupal will offer a permission that includes several features when we want to offer only one. The custom permissions module allows you to precisely manage permissions
Drupal allows from the UI (i.e. the admin or the back office) to manage permissions by role. For example, you can assign a "content editor" role the right to delete content of a certain type, edit it, publish it, etc. The list of possibilities is very long and also depends on the modules you have installed. Installed custom modules add permissions (administer the module, etc.). For example, if you install the webform module, you will have access to a long list of permissions such as “administer the configuration”, “edit” or “delete” a form.
The ergonomics in the UI is not always excellent because it involves ticking checkboxes in a very (too?) long list of possibilities. But it is functional and it allows to correctly manage what users have the right to do.
We can sometimes meet a limit. A role will have a permission like manage site configuration. The user will have access to features that we do not want to grant him.
A module allows you to manage these permissions more precisely: Custom Permissions
With this module, you can add specific permissions from the road without giving access to a whole set of features.
Find the route of a feature
There are several solutions. you can find the route to a core feature in:
For example for performance management:
_permission: 'administer site configuration'
The route is:
For contrib modules, routes can be found in the .routing.yml file, for example for webform:
_title: 'Webforms: Forms configuration'
_permission: 'administer webform'
The route is:
Use the Devel module to find a route
With the Devel module, you can use the “routes info” menu which will list the available routes.
Install the "custom permissions" module
We install the module with composer:
composer require drupal/config_perms
How to create a custom permission
Go to People > Custom permissions
Click add permission. We indicate a name for the permission. You can indicate what you want, this name is made to name your permission. Then enter the route. Then you have to activate it.
We then find this new personalized permission in the list of permissions. It can therefore be attributed to a specific role.
Go to /admin/people/custom-permissions/list
Click on “add permission”
A new line appears.
Give a name like “administer basic site settings”
Specify the system.site_information_settings route
Check the “enable” box and click save.
Custom rights management
then go to the rights management page: /admin/people/permissions
There is a section with “custom permissions”.