Another Role-Based Access Control (RBAC) implementation for AdonisJs
$ adonis install git+/~https://github.com/alexdonh/adonis-rbac.git --as=adonis-rbac
- Register RBAC providers in
start/app.js
file.
const providers = [
...
'@adonisjs/lucid/providers/LucidProvider',
'adonis-rbac/providers/RbacProvider'
]
const aceProviders = [
...
'@adonisjs/lucid/providers/MigrationsProvider',
'adonis-rbac/providers/CommandsProvider'
]
- Setting up trait in
/app/Models/User.js
model.
class User extends Model {
static get traits() {
return [
'@provider:Rbac/Traits/User'
]
}
// or if you need to customize the properties
static boot () {
super.boot()
this.addTrait('@provider:Rbac/Traits/User', {
cache: false, // or cache component. See /~https://github.com/alexdonh/adonis-cache.git
cacheKeyPrefix: 'rbac/user/',
cacheDuration: 60 * 24,
allowActions: []
})
}
}
- Setting up middleware in
start/kernel.js
file.
const namedMiddleware = {
...
rbac: 'Rbac/Middlewares/AccessControl'
...
}
- Run the migrations. See https://adonisjs.com/docs/4.1/migrations
$ adonis migration:run
- In
start/routes.js
:
Route.get('/path/to/action', 'SomeController.someAction').middleware(['auth'])
// or
Route
.group(() => {
...
})
.middleware(['auth'])
- In controller actions:
if (auth.user.is('administrator')) {
...
}
// or
if (auth.user.can('/path/to/action')) {
...
}
Having trouble? Open an issue!
The MIT License (MIT). See License File for more information.