Bouncer is a Laravel package that provides a simple and flexible way to manage roles and abilities for user authorization using Eloquent models.
Laravel Eloquent roles and abilities.
This tool is primarily used by Laravel developers to implement role-based access control (RBAC) in their applications, allowing them to define and check user permissions easily. It is ideal for projects requiring fine-grained authorization logic tied to user roles and abilities within the Laravel framework.
Bouncer requires Laravel and Eloquent ORM; ensure your application uses Laravel's authentication system for seamless integration. Best practice includes defining roles and abilities clearly to maintain manageable authorization logic. Keep migrations up to date and review permissions regularly to avoid privilege escalation.
Run composer require silber/bouncer
Add the service provider to the providers array in config/app.php (for Laravel versions before 5.5)
Publish the migration files using php artisan vendor:publish --provider="Silber\Bouncer\BouncerServiceProvider" --tag=migrations
Run php artisan migrate to create necessary tables
Optionally, publish the config file with php artisan vendor:publish --provider="Silber\Bouncer\BouncerServiceProvider" --tag=config
$user->assign('admin');
Assigns the 'admin' role to the user.
$user->allow('edit-articles');
Grants the user the ability to 'edit-articles'.
$user->can('edit-articles');
Checks if the user has the ability to 'edit-articles'.
Bouncer::allow('admin')->to('delete-users');
Gives the 'admin' role the ability to 'delete-users'.
$user->retract('admin');
Removes the 'admin' role from the user.
Bouncer::disallow('admin')->to('delete-users');
Revokes the 'delete-users' ability from the 'admin' role.