Download the PHP package kerigard/laravel-roles without Composer
On this page you can find all versions of the php package kerigard/laravel-roles. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download kerigard/laravel-roles
More information about kerigard/laravel-roles
Files in kerigard/laravel-roles
Package laravel-roles
Short Description Permissions and roles for Laravel
License MIT
Homepage https://github.com/kerigard/laravel-roles
Informations about the package laravel-roles
Laravel Roles
Permissions and roles for Laravel 9.20 and up.
Installation
Install package via composer:
Publish the configuration and migration files using the vendor:publish artisan command:
Customize the roles.php configuration file according to your requirements. After that run the migrations:
Usage
Connecting traits
To start using permission and role checking, your User model must use the Kerigard\LaravelRoles\Traits\HasRoles and Kerigard\LaravelRoles\Traits\HasPermissions traits:
It is not necessary to connect both traits at the same time.
Creating roles and permissions
Create roles and permissions, after which create a relationship between them:
You can override models through a config file.
Connect a role or permission to a user:
You can disable a role or permission for a user:
Or just sync the specified roles or permissions. Any roles or permissions that are not listed will be disabled:
Use this method if you don't want old roles or permissions to be disabled when syncing:
Permissions check
To check for permission, run:
All permissions are registered with Laravel Gates, so you can use the can function:
In a controller, you can use the authorize function to throw an exception if the user doesn't have permissions:
Roles check
To check if a role exists, run:
If you want to check the role in the controller and raise an exception if it is missing, then you need to replace the trait import in the app\Http\Controllers\Controller.php file:
After that, you can use the authorizeRole function in all controllers to check the role:
Blade directives
You can use directives in blade files to write conditions conveniently:
Middlewares
In the app/Http/Kernel.php file, you can specify a middleware for checking roles and permissions:
Then you can secure your routes:
Custom statuses
When throwing exceptions by default, Laravel returns a 403 error code with the message This action is unauthorized. You can specify your own error codes and messages for each role and permission:
Super admin
In the configuration, you can enable the super admin role. For users with this role, all permissions and role checks will be true.
Changelog
Please see the CHANGELOG for more information on what has changed recently.
License
MIT. Please see the LICENSE FILE for more information.