Access
Two ways to say who may do what. Bouncer keeps abilities and roles in four tables (prefixed
bouncer_, because both packages would create a roles table), Laratrust keeps roles
and permissions in its own. The seeder gave the three users the same rules in both, and the matrices below
ask each package through its own API.
Bouncer
| User | posts.view | posts.edit | posts.delete | users.manage |
|---|---|---|---|---|
| Ada Adminadmin | allowed | allowed | allowed | allowed |
| Emre Editoreditor | allowed | allowed | denied | denied |
| Rita Readerreader | allowed | denied | denied | denied |
Computed with bouncer.From(ctx).Can(ctx, entity, ability).
Laratrust
| User | posts.view | posts.edit | posts.delete | users.manage |
|---|---|---|---|---|
| Ada Adminadmin | allowed | allowed | allowed | allowed |
| Emre Editoreditor | allowed | allowed | denied | denied |
| Rita Readerreader | allowed | denied | denied | denied |
Computed with laratrust.From(ctx).User(subject).IsAbleTo(ctx, permission).
Middleware
Bouncer: CanAbility
route.Middleware(bouncer.CanAbility[Member]{Ability: "posts.edit"}).
Get("/bouncer", handler)
Guests get 401, users without the ability 403. Try the route as each user:
Laratrust: Permission
route.Middleware(laratrust.Permission[Member]("posts.edit")).
Get("/laratrust", handler)
A user without the permission gets the abort code of LARATRUST_ABORT_CODE (403).
The demo has no login form, so a small middleware signs in the user named by ?as= for these two
routes only. A real application would use its session guard.