Skip to content

Class: SubjectRbac<T, G> ​

Defined in: packages/core/src/rbac.ts:1017

Roles & permissions of a single subject. Obtain with rbac.for(user).

Type Parameters ​

Type ParameterDefault type
T extends RbacTypeConfigDefaultRbacTypes
G extends stringDefaultGuardName<T>

Constructors ​

Constructor ​

ts
new SubjectRbac<T, G>(
   rbac, 
   subject, 
   guardName, 
   teamId, 
   actor?
): SubjectRbac<T, G>;

Defined in: packages/core/src/rbac.ts:1021

Parameters ​

ParameterType
rbacRbac<T>
subjectSubject
guardNamestring
teamIdTeamId
actor?RbacActor

Returns ​

SubjectRbac<T, G>

Properties ​

PropertyModifierTypeDefined in
guardNamereadonlystringpackages/core/src/rbac.ts:1024
subjectreadonlySubjectpackages/core/src/rbac.ts:1023
teamIdreadonlyTeamIdpackages/core/src/rbac.ts:1025

Methods ​

assignRole() ​

ts
assignRole(...roles): Promise<SubjectRbac<T, G>>;

Defined in: packages/core/src/rbac.ts:1055

Assigns roles. A last { expiresAt } argument makes the assignments temporary (see ExpiryOptions); without it they are permanent, also for roles the subject already held with an expiry (the latest call wins).

Parameters ​

ParameterType
...rolesExpiryArgs<Many<RoleInput<RolesOf<GuardTypes<T, G>>>>>

Returns ​

Promise<SubjectRbac<T, G>>


can() ​

ts
can(permission): Promise<boolean>;

Defined in: packages/core/src/rbac.ts:1315

Authorization check: runs the before hook and superAdminRoles first, then falls back to hasPermissionTo, returning false instead of throwing.

Parameters ​

ParameterType
permissionPermissionInput<PermissionName<T, G>>

Returns ​

Promise<boolean>


canAll() ​

ts
canAll(...permissions): Promise<boolean>;

Defined in: packages/core/src/rbac.ts:1333

Parameters ​

ParameterType
...permissionsMany<PermissionInput<PermissionName<T, G>>>[]

Returns ​

Promise<boolean>


canAny() ​

ts
canAny(...permissions): Promise<boolean>;

Defined in: packages/core/src/rbac.ts:1326

Parameters ​

ParameterType
...permissionsMany<PermissionInput<PermissionName<T, G>>>[]

Returns ​

Promise<boolean>


cannot() ​

ts
cannot(permission): Promise<boolean>;

Defined in: packages/core/src/rbac.ts:1322

Parameters ​

ParameterType
permissionPermissionInput<PermissionName<T, G>>

Returns ​

Promise<boolean>


checkPermissionTo() ​

ts
checkPermissionTo(permission): Promise<boolean>;

Defined in: packages/core/src/rbac.ts:1307

hasPermissionTo that returns false instead of throwing for an unknown permission or another guard (spatie: checkPermissionTo). Pure data check: no before hook or super-admin.

Parameters ​

ParameterType
permissionPermissionInput<PermissionName<T, G>>

Returns ​

Promise<boolean>


getAllPermissions() ​

ts
getAllPermissions(): Promise<Permission[]>;

Defined in: packages/core/src/rbac.ts:1260

Returns ​

Promise<Permission[]>


getDirectPermissionAssignments() ​

ts
getDirectPermissionAssignments(): Promise<PermissionAssignment[]>;

Defined in: packages/core/src/rbac.ts:1213

The direct permissions the subject holds (in the current guard & team), each with when the grant ends (expiresAt, null = permanent). Expired grants are not listed.

Returns ​

Promise<PermissionAssignment[]>


getDirectPermissions() ​

ts
getDirectPermissions(): Promise<Permission[]>;

Defined in: packages/core/src/rbac.ts:1252

Returns ​

Promise<Permission[]>


getPermissionNames() ​

ts
getPermissionNames(): Promise<string[]>;

Defined in: packages/core/src/rbac.ts:1265

Returns ​

Promise<string[]>


getPermissionsViaRoles() ​

ts
getPermissionsViaRoles(): Promise<Permission[]>;

Defined in: packages/core/src/rbac.ts:1256

Returns ​

Promise<Permission[]>


getRoleAssignments() ​

ts
getRoleAssignments(): Promise<RoleAssignment[]>;

Defined in: packages/core/src/rbac.ts:1107

The roles the subject holds (in the current guard & team), each with when the assignment ends (expiresAt, null = permanent). Expired assignments are not listed.

Returns ​

Promise<RoleAssignment[]>


getRoleNames() ​

ts
getRoleNames(): Promise<string[]>;

Defined in: packages/core/src/rbac.ts:1117

Returns ​

Promise<string[]>


getRoles() ​

ts
getRoles(): Promise<Role[]>;

Defined in: packages/core/src/rbac.ts:1113

Returns ​

Promise<Role[]>


givePermissionTo() ​

ts
givePermissionTo(...permissions): Promise<SubjectRbac<T, G>>;

Defined in: packages/core/src/rbac.ts:1154

Gives direct permissions. A last { expiresAt } argument makes the grants temporary (see ExpiryOptions); without it they are permanent, also for ones already held with an expiry.

Parameters ​

ParameterType
...permissionsExpiryArgs<Many<PermissionInput<PermissionsOf<GuardTypes<T, G>>>>>

Returns ​

Promise<SubjectRbac<T, G>>


hasAllDirectPermissions() ​

ts
hasAllDirectPermissions(...permissions): Promise<boolean>;

Defined in: packages/core/src/rbac.ts:1296

true if the subject has ALL the permissions directly (ignores roles). Never throws.

Parameters ​

ParameterType
...permissionsMany<PermissionInput<PermissionName<T, G>>>[]

Returns ​

Promise<boolean>


hasAllPermissions() ​

ts
hasAllPermissions(...permissions): Promise<boolean>;

Defined in: packages/core/src/rbac.ts:1349

true if the subject has ALL the permissions. Pure data check (no before / super-admin), never throws.

Parameters ​

ParameterType
...permissionsMany<PermissionInput<PermissionName<T, G>>>[]

Returns ​

Promise<boolean>


hasAllRoles() ​

ts
hasAllRoles(...roles): Promise<boolean>;

Defined in: packages/core/src/rbac.ts:1131

Parameters ​

ParameterType
...rolesMany<RoleInput<RoleName<T, G>>>[]

Returns ​

Promise<boolean>


hasAnyDirectPermission() ​

ts
hasAnyDirectPermission(...permissions): Promise<boolean>;

Defined in: packages/core/src/rbac.ts:1288

true if the subject has ANY of the permissions directly (ignores roles). Never throws.

Parameters ​

ParameterType
...permissionsMany<PermissionInput<PermissionName<T, G>>>[]

Returns ​

Promise<boolean>


hasAnyPermission() ​

ts
hasAnyPermission(...permissions): Promise<boolean>;

Defined in: packages/core/src/rbac.ts:1341

true if the subject has ANY of the permissions. Pure data check (no before / super-admin), never throws.

Parameters ​

ParameterType
...permissionsMany<PermissionInput<PermissionName<T, G>>>[]

Returns ​

Promise<boolean>


hasAnyRole() ​

ts
hasAnyRole(...roles): Promise<boolean>;

Defined in: packages/core/src/rbac.ts:1126

Parameters ​

ParameterType
...rolesMany<RoleInput<RoleName<T, G>>>[]

Returns ​

Promise<boolean>


hasDirectPermission() ​

ts
hasDirectPermission(permission): Promise<boolean>;

Defined in: packages/core/src/rbac.ts:1281

Checks a direct permission only (ignores roles).

Parameters ​

ParameterType
permissionPermissionInput<PermissionName<T, G>>

Returns ​

Promise<boolean>


hasExactRoles() ​

ts
hasExactRoles(...roles): Promise<boolean>;

Defined in: packages/core/src/rbac.ts:1137

true if the subject has ALL the given roles and no other role (in the current guard & team).

Parameters ​

ParameterType
...rolesMany<RoleInput<RoleName<T, G>>>[]

Returns ​

Promise<boolean>


hasPermissionTo() ​

ts
hasPermissionTo(permission): Promise<boolean>;

Defined in: packages/core/src/rbac.ts:1275

Checks a permission (direct or via roles). Pure data check: the before hook and superAdminRoles are NOT applied (like spatie, where only can() goes through the Gate). Throws PermissionDoesNotExist if the permission is unknown (when wildcard is off). Use can for a check that never throws.

Parameters ​

ParameterType
permissionPermissionInput<PermissionName<T, G>>

Returns ​

Promise<boolean>


hasRole() ​

ts
hasRole(roles): Promise<boolean>;

Defined in: packages/core/src/rbac.ts:1122

true if the subject has the role — or ANY of the roles when given an array.

Parameters ​

ParameterType
rolesMany<RoleInput<RoleName<T, G>>>

Returns ​

Promise<boolean>


removeRole() ​

ts
removeRole(...roles): Promise<SubjectRbac<T, G>>;

Defined in: packages/core/src/rbac.ts:1067

Parameters ​

ParameterType
...rolesMany<RoleInput<RolesOf<GuardTypes<T, G>>>>[]

Returns ​

Promise<SubjectRbac<T, G>>


revokeAll() ​

ts
revokeAll(options?): Promise<SubjectRbac<T, G>>;

Defined in: packages/core/src/rbac.ts:1233

Removes every role and direct permission of the subject in the current guard and team (syncRoles() plus syncPermissions() with nothing). allGuards and allTeams widen it; both together remove the subject from everything, like spatie does when a model is deleted. Emits role.detached / permission.detached per team and guard, for what was actually removed.

allTeams needs the optional adapter method getSubjectLinks() when teams are enabled, and throws ADAPTER_METHOD_MISSING without it.

Parameters ​

ParameterType
options?RevokeAllOptions

Returns ​

Promise<SubjectRbac<T, G>>


revokePermissionTo() ​

ts
revokePermissionTo(...permissions): Promise<SubjectRbac<T, G>>;

Defined in: packages/core/src/rbac.ts:1168

Parameters ​

ParameterType
...permissionsMany<PermissionInput<PermissionsOf<GuardTypes<T, G>>>>[]

Returns ​

Promise<SubjectRbac<T, G>>


snapshot() ​

ts
snapshot(options?): Promise<RbacSnapshot>;

Defined in: packages/core/src/rbac.ts:1360

Serializable { roles, permissions } — e.g. to hydrate a React/Vue client. With checkBefore, the before hook runs for those names and the ones it denies are listed in denied, so the UI hides them too.

Parameters ​

ParameterType
options?SnapshotOptions<T, G>

Returns ​

Promise<RbacSnapshot>


syncPermissions() ​

ts
syncPermissions(...permissions): Promise<SubjectRbac<T, G>>;

Defined in: packages/core/src/rbac.ts:1183

Replaces all direct permissions (in the current guard & team) with the given ones. { expiresAt } works as in syncRoles.

Parameters ​

ParameterType
...permissionsExpiryArgs<Many<PermissionInput<PermissionsOf<GuardTypes<T, G>>>>>

Returns ​

Promise<SubjectRbac<T, G>>


syncRoles() ​

ts
syncRoles(...roles): Promise<SubjectRbac<T, G>>;

Defined in: packages/core/src/rbac.ts:1082

Replaces all roles (in the current guard & team) with the given ones. With a last { expiresAt }, every listed role gets that expiry; without it, roles the subject keeps keep theirs and new ones are permanent.

Parameters ​

ParameterType
...rolesExpiryArgs<Many<RoleInput<RolesOf<GuardTypes<T, G>>>>>

Returns ​

Promise<SubjectRbac<T, G>>


teams() ​

ts
teams(options?): Promise<Id[]>;

Defined in: packages/core/src/rbac.ts:1246

Ids of the teams where the subject has a role or a direct permission in the current guard (every guard with allGuards). The global scope is not a team, so it is never listed. Ids come back as the adapter stores them (the SQL adapters return strings). Order is not specified.

Throws TEAMS_NOT_ENABLED without teams: true, and ADAPTER_METHOD_MISSING when the adapter has no getSubjectLinks().

Parameters ​

ParameterType
options?SubjectTeamsOptions

Returns ​

Promise<Id[]>


withScope() ​

ts
withScope<G2>(options): SubjectRbac<T, ScopedGuardName<T, G2>>;

Defined in: packages/core/src/rbac.ts:1040

The same subject in another guard and/or team; options left out keep the current ones (spatie passes the guard to each check: $user->hasRole('admin', 'api')).

ts
await rbac.for(user).withScope({ guard: 'api' }).hasRole('admin')

Type Parameters ​

Type ParameterDefault type
G2 extends stringG

Parameters ​

ParameterType
optionsScopeOptions<CheckGuard<T, G2>>

Returns ​

SubjectRbac<T, ScopedGuardName<T, G2>>

Released under the MIT License. Inspired by spatie/laravel-permission.