Skip to content

Another Role-Based Access Control (RBAC) implementation for AdonisJs.

License

Notifications You must be signed in to change notification settings

alexdonh/adonis-rbac

Repository files navigation

Adonis RBAC

Another Role-Based Access Control (RBAC) implementation for AdonisJs

Installation

$ adonis install git+https://github.com/alexdonh/adonis-rbac.git --as=adonis-rbac

Setup

  1. 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'
]
  1. 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: []
    })
  }
}
  1. Setting up middleware in start/kernel.js file.
const namedMiddleware = {
  ...
  rbac: 'Rbac/Middlewares/AccessControl'
  ...
}
  1. Run the migrations. See https://adonisjs.com/docs/4.1/migrations
$ adonis migration:run

Usage

  1. In start/routes.js:
Route.get('/path/to/action', 'SomeController.someAction').middleware(['auth'])

// or

Route
  .group(() => {
    ...
  })
  .middleware(['auth'])
  1. In controller actions:
if (auth.user.is('administrator')) {
  ...
}

// or

if (auth.user.can('/path/to/action')) {
  ...
}

Credits

Support

Having trouble? Open an issue!

License

The MIT License (MIT). See License File for more information.

About

Another Role-Based Access Control (RBAC) implementation for AdonisJs.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published