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
| Command | Description |
|---|---|
permissions:sync | Sync roles and permissions from the catalog. |
permissions:sync-permissions | Sync only permissions. |
permissions:sync-roles | Sync 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-reset | Clear the permission cache. |
Sync flags:
| Flag | Description |
|---|---|
--guard | Guard to sync. |
--team-id | Team to sync (when teams is enabled). Not available on sync-permissions. |
--force | Skip confirmation and delete assigned items. |
--no-prune | Do 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 })
