Using User Rights in Add-ons#
To use the authorization system in your own add-on, not much logic needs to be written. Only an "auth" class isys_auth_example with some boilerplate code is required. Additionally, the add-on base class must implement the interface "\idoit\AddOn\AuthableInterface" and provide the defined method "getAuth".
Adapting the Add-on Base Class#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
The Custom Auth Class#
The class isys_auth_example mentioned in the example above must look as follows so that i-doit can use it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | |
The most important method of this class is "get_auth_methods", which defines the available rights. The remaining code should be adopted as completely as possible and only changed where appropriate (e.g., the constants used).
Don't forget the autoloader
Since these are classes in legacy format, they must be registered in the legacy autoloader.
Defining Custom Permissions via get_auth_methods#
i-doit provides some default permission types that we can reuse in the "get_auth_methods" method without writing additional code:
1 2 3 4 5 6 7 8 9 10 11 | |
Always use lowercase for permission identifiers
Please ensure that the permission identifiers (here "example_action") consist only of lowercase letters and underscores.
In this example, we use the "boolean" type. This has no parameters but works like a boolean value: the right either exists or it does not.
The remaining parameters take the following roles:
- "title": The string stored here (language constant) is displayed in the authorization system GUI.
- "type": Defines the parameters displayed in the GUI:
- boolean: Displays no parameter.
- object: Displays an object browser. Objects can be passed as parameters during the rights check.
- object_type: Displays a dialog with all object types. These can be passed as parameters during the rights check.
- category: Displays a dialog with all categories. These can be passed as parameters during the rights check.
- dialog_tables: Displays a dialog with all dialog fields. These can be passed as parameters during the rights check.
- custom_dialog_tables: Displays a dialog with all custom dialog fields. These can be passed as parameters during the rights check.
- "rights": Defines which rights are available for this definition:
- isys_auth::VIEW
- isys_auth::EDIT
- isys_auth::DELETE
- isys_auth::EXECUTE
- isys_auth::ARCHIVE
- isys_auth::CREATE
- isys_auth::SUPERVISOR
- "default": The rights stored here are pre-selected by default when the user adds the right in the GUI.
Checking Defined Rights in Code#
You can check defined rights in code via two approaches:
- isys_auth_example->is_allowed_to(
, ' ') This method returns a boolean "true" or "false" and can be used in conditionals. - isys_auth_example->check(
, ' ') This method will throw an exception (typically of type "isys_exception_auth") if the right does not exist. On success, it returns boolean "true".
The code for our example right can look as follows:
1 2 3 4 5 | |
If a right needs to be checked with a parameter, this is possible as follows:
1 2 3 4 5 | |
Defining a Custom Rights Audit#
With some custom code, you can build a custom rights audit instead of relying on the i-doit internal logic. Behind the key of a right (in our example "example_action"), there can optionally be a custom method that is used to process rights queries.
At the core of the authorization system is the "$m_paths" array. It contains all saved permissions for the currently logged-in user in the context of the add-on.
This means that when we store a right for our user for the add-on isys_module_example, we will find this right as an array within "$m_paths". Since we created the available right "example_action" with type "boolean", it will contain the parameter "empty-id". If we output "$m_paths", we get approximately the following result:
1 2 3 4 5 6 7 8 9 10 | |
To check this right in code, the corresponding method within isys_auth_example would need to look as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | |
Due to the underlying architecture, custom permission methods can technically be called directly instead of via "is_allowed_to()" or "check()", but we strongly advise against this!
See also#
- Developing Add-ons -- Guide to add-on development
- Software Development -- Overview of developer documentation
- API Add-on -- Interface for external access