Permissions for Admin
How to create permissions for Admin
Development only
This guide requires Dev Server to be running.
We're setting up permissions for the admin
role to help you control who can access specific parts of the admin panel.
Structure of Permissions
Permissions are divided into groups, and each group contains a set of permissions.
For example, there's a group called settings, which includes permissions like can_manage_settings_email
or can_edit_email
.
Each navigation item in the Admin Control Panel (AdminCP) has a permission
formatted as can_manage_{your_nav_id}
. This prefix also applies to groups.
Example:
Group | Permissions | AdminCP Navigation Item | Description |
---|---|---|---|
settings | can_manage_settings_email | {your_plugin} => Settings => Email | Permission to show the email settings. |
settings | can_manage_settings_general | {your_plugin} => Settings => General | Permission to show the general settings. |
can_manage_settings | - | {your_plugin} => Settings | Permission to show the settings group. |
settings | can_edit_email | N/A | Own [permission to edit the email settings. |
Explanation:
- Group: The category under which permissions are organized (e.g., settings).
- Permission: Specific actions that can be allowed or denied (e.g., can_manage_settings_email).
- AdminCP Navigation Item: The path in the admin panel where the permission applies.
- Description: Details about what the permission allows.
By configuring these permissions, you can precisely manage admin access to various sections of the admin panel.
Create Permissions
First we need to create a permission for our plugin.
Plugin Configuration
Edit the permissions_admin
in the config.json
file from your plugin.
Internationalization (i18n)
Now we need to add the language key inside the langs
folder in apps/frontend/src/plugins/{your_plugin_code}/langs/en.json
file in the frontend.
Default language is English en
. If you have more then one language, you need to add the language key in other languages as well.
Based on our example above, we need to add the following keys:
Usage
Frontend
To check if admin has a specific permission, you can use the checkAdminPermissionPage(permission)
for page.
Build-in catch error feature in NextJS
When backend throws an error with 403
code using throw new ForbiddenException()
, NextJS will render 403
page. You don't need to handle
this error in your page. Use checkAdminPermissionPage()
to check if admin
has a specific permission when you haven't API endpoint.
Permissions in Metadata
To check if admin has a specific permission, you can use the checkAdminPermissionPageMetadata(permission)
for metadata.
Permissions in Component
To check if admin has a specific permission to display a component, you can use the isInAdminPermission(permission)
.
Backend
To check if admin has a specific permission, you can use the @AdminPermission()
decorator.
Admin required
@AdminPermission()
decorator will not work if route is not protected for
admins. See more how to protect routes for
admins.