-
Notifications
You must be signed in to change notification settings - Fork 335
Creating roles and abilities
Tom Witkowski edited this page Dec 8, 2015
·
2 revisions
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.
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);