Getting Started

Configuration

Every option available in config/permissions.ts.

The configuration lives in config/permissions.ts and is created with defineConfig:

config/permissions.ts
import { defineConfig } from '@eienjs/adonisjs-permission'

export default defineConfig({
  models: {
    role: () => import('#models/role'),
    permission: () => import('#models/permission'),
  },
  morphMap: {
    user: () => import('#models/user'),
  },
  defaultGuard: 'web',
  teams: false,
  enableWildcardPermission: false,
  superAdmin: { enabled: false, role: 'Super-Admin' },
  events: { enabled: false },
  cache: { enabled: true, key: 'adonisjs_permission_cache', ttl: '24h' },
  catalog: { permissions: [], roles: {} },
})

Options

OptionDefaultDescription
models.role / models.permissionrequiredLazy loaders returning your Role and Permission models.
morphMap{}Alias → model loader. Aliases are stored in model_type instead of the table/class name.
defaultGuard'web'Guard used when none is given.
teamsfalseEnables team scoping. See Teams.
teamForeignKey'team_id'Team column name.
enableWildcardPermissionfalseSee Wildcard permissions.
superAdmin.enabled / superAdmin.rolefalse / 'Super-Admin'See Super-Admin.
events.enabledfalseEmit events when roles/permissions change.
cache.enabled / cache.key / cache.ttltrue / 'adonisjs_permission_cache' / '24h'In-memory permission cache.
catalogemptyDeclarative roles and permissions. See Catalog.
tableNamessee belowOverride table names.
columnNamessee belowOverride column names.

Custom table and column names

defineConfig({
  // ...
  tableNames: {
    roles: 'roles',
    permissions: 'permissions',
    roleHasPermissions: 'role_has_permissions',
    modelHasRoles: 'model_has_roles',
    modelHasPermissions: 'model_has_permissions',
  },
  columnNames: {
    rolePivotKey: 'role_id',
    permissionPivotKey: 'permission_id',
    modelMorphKey: 'model_id',
    modelMorphType: 'model_type',
    teamForeignKey: 'team_id',
  },
})
Change table/column names before running the migrations, and keep your migrations in sync with the config.

Primary key types

Roles, permissions, models and teams can use increments, bigIncrements or uuid primary keys, even mixed in the same app. Polymorphic columns (model_id, team_id) are generated as string so they hold any of them.

For UUIDs, let the model assign its own key:

import { randomUUID } from 'node:crypto'
import { compose } from '@adonisjs/core/helpers'
import { BaseModel, beforeCreate, column } from '@adonisjs/lucid/orm'
import { Roleable } from '@eienjs/adonisjs-permission'

export default class Role extends compose(BaseModel, Roleable) {
  static selfAssignPrimaryKey = true

  @column({ isPrimary: true })
  declare id: string

  @beforeCreate()
  static assignUuid(role: Role) {
    role.id ||= randomUUID()
  }
}

Then change table.increments('id') to table.uuid('id').primary() and the pivot foreign keys to table.uuid(...) in your migrations. For bigIncrements, consume the value as BigInt with @column({ isPrimary: true, consume: BigInt }).