Skip to content

Commit 40ca074

Browse files
authored
Merge pull request #212 from andersonjeccel/ui-Displaying-Elements-Based-on-User-Permissions
[UI] Displaying elements based on user permissions in Twig
2 parents 8a9e169 + c634df6 commit 40ca074

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
Displaying elements based on User permissions
2+
#############################################
3+
4+
In Mautic, it's possible to control the visibility of elements on the user interface based on the User's permissions. This allows for showing or hiding certain features, links, or sections depending on the User's Role and the permissions associated with that Role.
5+
6+
This approach enhances security and provides a tailored experience for each User based on their Role and access level.
7+
8+
Using the ``securityIsGranted`` function
9+
****************************************
10+
11+
To display elements conditionally based on User permissions, use the ``securityIsGranted`` function in Twig templates. The ``securityIsGranted`` function checks if the current User has the specified permission and returns a boolean value indicating whether the User has the permission granted or not.
12+
13+
Here's the basic syntax:
14+
15+
.. code-block:: twig
16+
17+
{% if securityIsGranted('permission:string') %}
18+
<!-- Content to display if the user has the specified permission -->
19+
{% endif %}
20+
21+
In this structure, ``permission:string`` represents the permission to verify. Mautic uses a hierarchical permission system, in the format of ``bundle:level:permission``.
22+
23+
Displaying a User invitation link as example
24+
============================================
25+
26+
Here's a practical example of how to use this function to display a link for inviting new Users to the platform. This link should only be visible to Users who have the permission to create new User accounts.
27+
28+
In this example, the ``securityIsGranted`` function verifies if the current User has the ``user:users:create`` permission. The structure of the permission string verifies if the User has the ability to create new Users within the User management system.
29+
30+
.. code-block:: twig
31+
32+
{% if securityIsGranted('user:users:create') %}
33+
<li>
34+
<a href="{{ path('mautic_user_action', {objectAction: 'new'}) }}">
35+
<i class="ri-team-line"></i>
36+
<span>{{ 'mautic.user.profile.invite'|trans }}</span>
37+
</a>
38+
</li>
39+
{% endif %}
40+
41+
If the current User has the ``user:users:create`` permission, the code inside the if block renders, displaying the link to invite new users. The path function creates the link, which generates a URL based on the specified route - ``mautic_user_action``` - and any additional parameters - ``{objectAction: 'new'}``.
42+
43+
The ``'mautic.user.profile.invite'|trans`` expression is used to translate the text 'Invite your team' using Mautic's translation system. This ensures that the text is displayed in the appropriate language based on the user's locale settings.
44+
45+
This not only prevents unauthorized access but also keeps the interface clean and relevant for each User's Role.
46+
47+
When implementing permission-based displays, it's also essential to secure the backend routes and actions that these interface elements might trigger. The frontend permission verification must be an additional layer of security and user experience enhancement, not the sole method of access control.
48+
49+
Locating defined permissions
50+
============================
51+
52+
Mautic organizes its permissions on a per-bundle basis. Each bundle typically defines its own set of permissions in a dedicated PHP file. The standard location for these permission definitions is:
53+
54+
``[BundleName]/Security/[BundleName]Permissions.php``
55+
56+
For example:
57+
58+
- User permissions: ``UserBundle/Security/UserPermissions.php``
59+
- Email permissions: ``EmailBundle/Security/EmailPermissions.php``
60+
- SMS permissions: ``SmsBundle/Security/SmsPermissions.php``
61+
62+
These PHP files contain classes that extend ``AbstractPermissions`` and define the specific permissions available for that bundle. They usually include methods for building the permission matrix and checking individual permissions.
63+
64+
Examining permission files
65+
==========================
66+
67+
When opening one of these permission files, they'll typically find:
68+
69+
- A ``definePermissions`` method that outlines all available permissions for the bundle.
70+
- Constants defining permission levels - for example, ``LEVEL_VIEW, LEVEL_EDIT, LEVEL_FULL``.
71+
- Methods for checking specific permissions - for example, ``canViewUsers``, ``canEditEmails``.
72+
73+
For example, in the ``UserPermissions.php`` file, the ``UserPermissions`` class defines the available permissions for the ``UserBundle`` using a more structured approach. Here are the important parts:
74+
75+
.. code-block:: php
76+
77+
$this->permissions = [
78+
'profile' => [
79+
'editusername' => 1,
80+
'editemail' => 2,
81+
'editposition' => 4,
82+
'editname' => 8,
83+
'full' => 1024,
84+
],
85+
];
86+
87+
In this example, the profile key represents the permission Category, and the nested array defines the specific permission levels for actions like editing the username, email, position, name, and having full access to the User profile.
88+
89+
To use these permission keys with the ``securityIsGranted`` function in Twig templates, construct the appropriate permission string. The permission string follows the format: ``[bundle]:[level]:[permission]``.
90+
91+
Map the permission keys from the UserPermissions class to the corresponding permission strings:
92+
93+
- ``editusername`` => ``user:profile:editusername``
94+
- ``editemail`` => ``user:profile:editemail``
95+
- ``editposition`` => ``user:profile:editposition``
96+
- ``editname`` => ``user:profile:editname``
97+
- ``full`` => ``user:profile:full``
98+
99+
In each if statement, you pair the ``securityIsGranted`` function with the corresponding permission string. If the current User has the specified permission, the code inside the if block runs, displaying the relevant Form Fields for editing the User profile information.
100+
101+
For more information, refer to the Security documentation.

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ There are several ways to support Mautic other than contributing with code.
5858

5959
design/accordion
6060
design/availability
61+
design/displaying_elements_based_on_user_permissions
6162
design/feedback
6263
design/labelling
6364
design/notifications

0 commit comments

Comments
 (0)