Laravel Eloquent roles and abilities.
Bouncer is an elegant, framework-agnostic approach to managing roles and abilities for any app using Eloquent models.
Bouncer is an elegant, framework-agnostic approach to managing roles and abilities for any app using Eloquent models. With an expressive and fluent syntax, it stays out of your way as much as possible: use it when you want, ignore it when you don't.
For a quick, glanceable list of Bouncer's features, check out the cheat sheet.
Bouncer works well with other abilities you have hard-coded in your own app. Your code always takes precedence: if your code allows an action, Bouncer will not interfere.
Once installed, you can simply tell the bouncer what you want to allow at the gate:
// Give a user the ability to create posts
Bouncer::allow($user)->to('create', Post::class);
// Alternatively, do it through a role
Bouncer::allow('admin')->to('create', Post::class);
Bouncer::assign('admin')->to($user);
// You can also grant an ability only to a specific model
Bouncer::allow($user)->to('edit', $post);When you check abilities at Laravel's gate, Bouncer will automatically be consulted. If Bouncer sees an ability that has been granted to the current user (whether directly, or through a role) it'll authorize the check.
Note: Bouncer v1.0.2 requires PHP 8.2+ and Laravel/Eloquent 11+.
If you're on Laravel v6-v10, use Bouncer v1.0.1. If you're on Laravel v5.5-v5.8, use Bouncer RC6.
Install Bouncer with composer:
composer require silber/bouncerAdd Bouncer's trait to your user model:
use Silber\Bouncer\Database\HasRolesAndAbilities;
class User extends Model
{
use HasRolesAndAbilities;
}Now, to run Bouncer's migrations. First publish the migrations into your app's migrations directory, by running the following command:
php artisan vendor:publish --tag="bouncer.migrations"Finally, run the migrations:
php artisan migrateWhenever you use the Bouncer facade in your code, remember to add this line to your namespace imports at the top of the file:
use Bouncer;For more information about Laravel Facades, refer to the Laravel documentation.
Install Bouncer with composer:
composer require silber/bouncerSet up the database with the Eloquent Capsule component:
use Illuminate\Database\Capsule\Manager as Capsule;
$capsule = new Capsule;
$capsule->addConnection([/* connection config */]);
$capsule->setAsGlobal();Refer to the Eloquent Capsule documentation for more details.
Run the migrations by either of the following methods:
Use a tool such as vagabond to run Laravel migrations outside of a Laravel app. You'll find the necessary migrations in the migrations stub file.
Alternatively, you can run the raw SQL directly in your database.
Add Bouncer's trait to your user model:
use Illuminate\Database\Eloquent\Model;
use Silber\Bouncer\Database\HasRolesAndAbilities;
class User extends Model
{
use HasRolesAndAbilities;
}Create an instance of Bouncer:
use Silber\Bouncer\Bouncer;
$bouncer = Bouncer::create();
// If you are in a request with a current user
// that you'd wish to check permissions for,
// pass that user to the "create" method:
$bouncer = Bouncer::create($user); If you're using dependency injection in your app, you may register the Bouncer instance as a singleton in the container:
use Silber\Bouncer\Bouncer;
use Illuminate\Container\Container;
Container::getInstance()->singleton(Bouncer::class, function () {
return Bouncer::create();
}); You can now inject Bouncer into any class that needs it.
The create method creates a Bouncer instance with sensible defaults. To fully customize it, use the make method to get a factory instance. Call create() on the factory to create the Bouncer instance:
use Silber\Bouncer\Bouncer;
$bouncer = Bouncer::make()
->withCache($customCacheInstance)
->create(); Check out the Factory class to see all the customizations available.
Set which model is used as the user model throughout your app:
$bouncer->useUserModel(User::class);For additional configuration, check out the Configuration section below.
By default, Bouncer's queries are cached for the current request. For better performance, you may want to enable cross-request caching.
Adding roles and abilities to users is made extremely easy. You do not have to create a role or an ability in advance. Simply pass the name of the role/ability, and Bouncer will create it if it doesn't exist.
Note: the examples below all use the
Bouncerfacade. If you don't use facades, you can instead inject an instance ofSilber\Bouncer\Bouncerinto your class.
Let's create a role called admin and give it the ability to ban-users from our site:
Bouncer::allow('admin')->to('ban-users');That's it. Behind the scenes, Bouncer will create both a Role model and an Ability model for you.
If you want to add additional attributes to the role/ability, such as a human-readable title, you can manually create them using the role and ability methods on the Bouncer class:
$admin = Bouncer::role()->firstOrCreate([
'name' => 'admin',
'title' => 'Administrator',
]);
$ban = Bouncer::ability()->firstOrCreate([
'name' => 'ban-users',
'title' => 'Ban users',
]);
Bouncer::allow($admin)->to($ban);To now give the admin role to a user, simply tell the bouncer that the given user should be assigned the admin role:
Bouncer::assign('admin')->to($user);Alternatively, you can call the assign method directly on the user:
$user->assign('admin');Sometimes you might want to give a user an ability directly, without using a role:
Bouncer::allow($user)->to('ban-users');Here too you can accomplish the same directly off of the user:
$user->allow('ban-users');Sometimes you might want to restrict an ability to a specific model type. Simply pass the model name as a second argument:
Bouncer::allow($user)->to('edit', Post::class);If you want to restrict the ability to a specific model instance, pass in the actual model instead:
Bouncer::allow($user)->to('edit', $post);Use the toOwn method to allow users to manage their own models:
Bouncer::allow($user)->toOwn(Post::class);Now, when checking at the gate whether the user may perform an action on a given post, the post's user_id will be compared to the logged-in user's id (this can be customized). If they match, the gate will allow the action.
The above will grant all abilities on a user's "owned" models. You can restrict the abilities by following it up with a call to the to method:
Bouncer::allow($user)->toOwn(Post::class)->to('view');
// Or pass it an array of abilities:
Bouncer::allow($user)->toOwn(Post::class)->to(['view', 'update']);You can also allow users to own all types of models in your application:
Bouncer::allow($user)->toOwnEverything();
// And to restrict ownership to a given ability
Bouncer::allow($user)->toOwnEverything()->to('view');The bouncer can also retract a previously-assigned role from a user:
Bouncer::retract('admin')->from($user);Or do it directly on the user:
$user->retract('admin');The bouncer can also remove an ability previously granted to a user:
Bouncer::disallow($user)->to('ban-users');Or directly on the user:
$user->disallow('ban-users');Note: if the user has a role that allows them to `ba
No open issues yet, or sync has not completed.