Skip to content

Permission

Defined in: src/core/Permission.ts:43

Represents the resolved Permission for a query — the granted (or denied) access for the target role(s) and resource. Obtain one in two ways:

  1. The chainable form via AccessControl#can(), which returns a Permission once an action method such as .createAny() is called:
    const permission = ac.can('user').createAny('video');
    console.log(permission.granted); // boolean
  2. The one-shot form via AccessControl#check(), passing a fulfilled IQueryInfo object:
    const permission = ac.check({
    role: 'user',
    resource: 'video',
    action: 'create:any'
    });
    console.log(permission.granted); // boolean

get roles(): string[]

Defined in: src/core/Permission.ts:132

Specifies the roles for which the permission is queried for. Even if the permission is queried for a single role, this will still return an array.

If the returned array has multiple roles, this does not necessarily mean that the queried permission is granted or denied for each and all roles. Note that when a permission is queried for multiple roles, attributes are unioned (merged) for all given roles. This means “at least one of these roles” have the permission for this action and resource attribute.

string[]


get resource(): string

Defined in: src/core/Permission.ts:140

Specifies the target resource for which the permission is queried for.

string


get action(): string

Defined in: src/core/Permission.ts:149

The action the permission was checked for — the bare verb, with any :possession suffix stripped (e.g. read for read:any, publish for a custom publish:own).

string


get possession(): "own" | "any"

Defined in: src/core/Permission.ts:161

The possession that effectively granted access — 'own' or 'any'. Because any ⊇ own, a query for own that matched via an any grant resolves to 'any'. On denial, the requested possession is echoed back.

"own" | "any"


get attributes(): string[]

Defined in: src/core/Permission.ts:174

Gets an array of allowed attributes which are defined via Glob notation. If access is not granted, this will be an empty array.

Note that when a permission is queried for multiple roles, attributes are unioned (merged) for all given roles. This means “at least one of these roles” have the permission for this action and resource attribute.

string[]


get granted(): boolean

Defined in: src/core/Permission.ts:186

Specifies whether the permission is granted. If true, this means at least one attribute of the target resource is allowed.

boolean


get reason(): DenyReason | undefined

Defined in: src/core/Permission.ts:211

Why the permission was denied — undefined when granted. Also carried on the access event for audit logs.

'out_of_schedule' is worth distinguishing in UIs: the check failed only because of a during schedule, so the same request would be granted at a covered instant (“granted, but not now” — e.g. show “outside access hours” instead of a generic denial). Any other reason means the grant itself doesn’t apply.

const permission = ac.can('editor', ctx).updateAny('post');
if (!permission.granted) {
msg = permission.reason === 'out_of_schedule'
? 'You are outside your access hours.'
: 'You do not have permission to do that.';
}

DenyReason | undefined


get grantedAsync(): Promise<boolean>

Defined in: src/core/Permission.ts:229

Async counterpart of Permission#granted. Resolves custom/async { fn } conditions (and works for fully-declarative checks too). After it resolves, the sync attributes/granted/filter accessors are usable.

It resolves the query this permission was made from: later calls on the same Query, or changes to the object or role array you passed, never reach it. The context is copied one level deep, so a change to a nested object in it (ctx.user.admin = true) before this resolves does.

if (await ac.can('user', ctx).readAny('post').grantedAsync) { … }

Promise<boolean>

filter(data): UnknownObject | UnknownObject[]

Defined in: src/core/Permission.ts:240

Filters the given data object (or array of objects) by the permission attributes and returns this data with allowed attributes.

UnknownObject | UnknownObject[]

Data object to be filtered. Either a single object or array of objects.

UnknownObject | UnknownObject[]

  • The filtered data object.