Basic Usage

Catalog & Sync

Declare roles and permissions in config and sync them with the database.

Instead of seeding by hand, describe your roles and permissions in config/permissions.ts:

config/permissions.ts
defineConfig({
  // ...
  catalog: {
    permissions: ['view articles', 'create articles', 'edit articles', 'delete articles'],
    roles: {
      admin: ['view articles', 'create articles', 'edit articles', 'delete articles'],
      editor: ['view articles', 'create articles', 'edit articles'],
    },
  },
})

Then synchronize:

node ace permissions:sync

Missing roles and permissions are created, role permissions are synced, and entries not in the catalog are pruned. If a pruned item is still assigned to a model, the command stops and asks for confirmation.

Commands

CommandDescription
permissions:syncSync roles and permissions from the catalog.
permissions:sync-permissionsSync only permissions.
permissions:sync-rolesSync only roles and their permissions.
permissions:create-permission <name> [guard]Create a permission.
permissions:create-role <name> [guard] [permissions]Create a role; permissions separated by | or ,.
permissions:show [guard]Print roles and permissions in a table.
permissions:cache-resetClear the permission cache.

Sync flags:

FlagDescription
--guardGuard to sync.
--team-idTeam to sync (when teams is enabled). Not available on sync-permissions.
--forceSkip confirmation and delete assigned items.
--no-pruneDo not delete items missing from the catalog.

Programmatic usage

import app from '@adonisjs/core/services/app'

const syncer = await app.container.make('permission.syncer')
const report = await syncer.inspect()
await syncer.syncAll({ force: false })