Roles
Intro
Roles are either system defined or can be custom built by the application admins in the front-end. Roles allocate a set of permissions to users
Adding new system roles
To add a new role to the App you need to edit the `app/Enums/UserRoles.php' Enums file. Adding a value, label & description of the new role.
// UserRoles.php
<?php
...
case NEW_ROLE = 'new-role';
...
public function label(): string
{
return match($this) {
...
UserRoles::NEW_ROLE => 'New Role',
...
};
}
public function description(): string
{
return match($this) {
...
UserRoles::NEW_ROLE => 'A new role that allows the user to do cool things',
...
};
}
Seeding roles
Roles are then seeded in the Database/Seeder/RoleSeeder.php which is also included in the ProductionSeeder.php. This seeder:
1. Runs through all app roles
2. Adds new roles to the App
Seeding role permissions
Pemissions are then seeded into system roles in the Database/Seeder/RolePermissionSeeder.php which is also included in the ProductionSeeder.php. This seeder:
1. Runs through all app roles
2. Assigns app permissions defined in `app/Enums/UserRoles.php'
Using roles in the App
It's recommended to user permissions rather than roles to define access. However you can use the following methods if necessary
User has role check
You can use the hasRole() trait on the User model to validate roles
// MenuController.php
<?php
...
if ($user->hasRole(Role::ADMIN->value)) {
$menu["Manage"][] = self::insightsMenu();
}
Adding new custom roles
Admin users can access the Roles & Permissions area in the app to add and define custom Roles