Download the PHP package alexya-framework/roles without Composer
On this page you can find all versions of the php package alexya-framework/roles. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package roles
Roles
Alexya's Role Based Access System utilities.
Contents
- Creating permissions
- Creating roles
- Adding permissions to roles
- Creating users
Creating permissions
Permissions are the way to authorized users to perform certain actions.
The class \Alexya\Roles\Permission
represents a permission that can be assigned to a specific role.
A permission consists of an identifier, a title and a status flag.
You can easily extend this class to provide more functionality such as alternative names, ranks...
For example, in a file system, each user should have permissions to read/write to certain files:
Creating roles
Roles are the containers of various permissions.
The class \Alexya\Roles\Role
represents a role by itself.
It has an identifier for the role, a title and an array of Permission
with the granted permissions for this role.
Alternatively, a role can have a parent role for inheritance.
It also has an integer representing the priority of the role, the bigger the number, the most priority it has.
The method hasPermission
accepts an identifier of a permission or an instance of the permission and checks that this role has been granted with the permission.
For a shorter version, you can use can
, 'cuz shorter > longer.
Example:
Adding permissions to roles
Once you have the permissions and roles you need to specify which roles have granted which permissions.
A permission can be enabled, disabled or inherited. If a permission is inherited the role will check the status of that permission in the parent role, if there's no parent role it will be treated as disabled.
Adding permissions to roles is as easy as calling the addPermission
method with the permission to add.
Example:
To check if a role has granted a certain permission you can use the hasPermission
or can
methods:
The method getPermission
returns a permission from the role:
Creating users
Now that we have the roles and permissions we need users to assign them.
The class \Alexya\Roles\User
represents an user, where the roles are assigned.
It's the class that you should extend in order to add roles to your users as it provides the hasPermission
and can
methods for checking if this user has any permission granted.
It also has the methods addRole
and removeRole
to add/remove roles.
Example: