Skip to content

Latest commit

 

History

History
162 lines (121 loc) · 3.96 KB

instructions.md

File metadata and controls

162 lines (121 loc) · 3.96 KB

Instructions

  1. Add Cerberus into providers Array, inside start/app.js file of your project:
  const providers = [
    ...
    '@quantumlabs/adonisjs-cerberus/providers/CerberusProvider',
    ...
  ]
  1. Add Cerberus into aceProviders Array, inside start/app.js file of your project:
  const aceProviders = [
    ...
    '@quantumlabs/adonisjs-cerberus/providers/CommandsProvider'
    ...
  ]
  1. Add Cerberus Middleware into namedMiddleware Array, inside start/kernel.js file of your project:
  const namedMiddleware = [
    ...
    guard: 'Cerberus/Middleware/Guard'
    ...
  ]
  1. Add Cerberus special Traits inside your app/Models/User Model, like this:
class User extends Model {
  static boot () {
    super.boot()

    this.addTrait('@provider:Cerberus/Traits/Role')
    this.addTrait('@provider:Cerberus/Traits/Permission')
  }
...
  1. If you're using Objection.js's Snake case to camel conversion, you should add this property at the end of your project config/database.js file:
  ...
  usingSnakeCaseMappers: true
}
  1. Finally, you need to run:
  adonis cerberus:init

This command will copy all library specific migrations into your project migrations folder.

Commands

Init

  adonis cerberus:init

As mentioned before, this command should (and we hope it will) copy all library specific migrations into your project migrations folder. There's no special flags or parameters for this command.

Role

adonis cerberus:role [name] [slug]

Usage:
  cerberus:role <name> [slug] [options]

Arguments:
  name   Name of the role
  slug   Short name for role

This command create a new role into roles table. You only need to specify the name of role, slug argument is optional.

Resource

adonis cerberus:resource [name] [slug] [options]

Arguments:
  name               Name of the resource
  slug               Short name for resource

Options:
  -p, --permission   Generate permissions
  -a, --always-ask   Ask which permissions give in each Resource once (false by default)
  --from-models      Generate a resource for each app Model
  --from-controllers Generate a resource for each app Controller

This command create a new resource into resources table. You only need to specify the name of resource, slug argument is optional. The options are: -p, --permission - Generate permissions -a, --always-ask - Ask which permissions give in each Resource once (false by default) --from-models - Generate a resource for each app Model --from-controllers - Generate a resource for each app Controller

Permission

cerberus:permission [options]

Options:
  -a, --all               Run permission creation for each Resource in database
  --resource-name <value> Name of resource

This command create a new permission into permissions table. You need to specify a Resource name then, Cerberus will create a permission record with the specified Resource name.

Usage

After creating your Permissions, you'll be able to start using Guard middleware in your routes.

It's simple to bind a permission into a route, look:

/**
* Get all users
* GET users
*/
Route
  .get('', 'UserController.index')
  .middleware(['auth', 'guard: user.read'])

You can also use multiple permissions in your route:

/**
* Create a new user
* POST users/register
*/
Route
  .post('register', 'UserController.register')
  .middleware(['auth', 'guard: users.create, users.read'])

And it's possible to use multiple resources too:

/**
* Fetch all posts with user profile
* GET posts
*/
Route
  .get('', 'PostController.index')
  .middleware(['auth', 'guard: post.read, user.read'])

Warning: You need to add the default auth middleware before Cerberus guard!