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 Parameter | Default type |
|---|---|
T extends RbacTypeConfig | DefaultRbacTypes |
G extends string | DefaultGuardName<T> |
Constructors
Constructor
new SubjectRbac<T, G>(
rbac,
subject,
guardName,
teamId,
actor?
): SubjectRbac<T, G>;Defined in: packages/core/src/rbac.ts:1021
Parameters
| Parameter | Type |
|---|---|
rbac | Rbac<T> |
subject | Subject |
guardName | string |
teamId | TeamId |
actor? | RbacActor |
Returns
SubjectRbac<T, G>
Properties
| Property | Modifier | Type | Defined in |
|---|---|---|---|
guardName | readonly | string | packages/core/src/rbac.ts:1024 |
subject | readonly | Subject | packages/core/src/rbac.ts:1023 |
teamId | readonly | TeamId | packages/core/src/rbac.ts:1025 |
Methods
assignRole()
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
| Parameter | Type |
|---|---|
...roles | ExpiryArgs<Many<RoleInput<RolesOf<GuardTypes<T, G>>>>> |
Returns
Promise<SubjectRbac<T, G>>
can()
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
| Parameter | Type |
|---|---|
permission | PermissionInput<PermissionName<T, G>> |
Returns
Promise<boolean>
canAll()
canAll(...permissions): Promise<boolean>;Defined in: packages/core/src/rbac.ts:1333
Parameters
| Parameter | Type |
|---|---|
...permissions | Many<PermissionInput<PermissionName<T, G>>>[] |
Returns
Promise<boolean>
canAny()
canAny(...permissions): Promise<boolean>;Defined in: packages/core/src/rbac.ts:1326
Parameters
| Parameter | Type |
|---|---|
...permissions | Many<PermissionInput<PermissionName<T, G>>>[] |
Returns
Promise<boolean>
cannot()
cannot(permission): Promise<boolean>;Defined in: packages/core/src/rbac.ts:1322
Parameters
| Parameter | Type |
|---|---|
permission | PermissionInput<PermissionName<T, G>> |
Returns
Promise<boolean>
checkPermissionTo()
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
| Parameter | Type |
|---|---|
permission | PermissionInput<PermissionName<T, G>> |
Returns
Promise<boolean>
getAllPermissions()
getAllPermissions(): Promise<Permission[]>;Defined in: packages/core/src/rbac.ts:1260
Returns
Promise<Permission[]>
getDirectPermissionAssignments()
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()
getDirectPermissions(): Promise<Permission[]>;Defined in: packages/core/src/rbac.ts:1252
Returns
Promise<Permission[]>
getPermissionNames()
getPermissionNames(): Promise<string[]>;Defined in: packages/core/src/rbac.ts:1265
Returns
Promise<string[]>
getPermissionsViaRoles()
getPermissionsViaRoles(): Promise<Permission[]>;Defined in: packages/core/src/rbac.ts:1256
Returns
Promise<Permission[]>
getRoleAssignments()
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()
getRoleNames(): Promise<string[]>;Defined in: packages/core/src/rbac.ts:1117
Returns
Promise<string[]>
getRoles()
getRoles(): Promise<Role[]>;Defined in: packages/core/src/rbac.ts:1113
Returns
Promise<Role[]>
givePermissionTo()
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
| Parameter | Type |
|---|---|
...permissions | ExpiryArgs<Many<PermissionInput<PermissionsOf<GuardTypes<T, G>>>>> |
Returns
Promise<SubjectRbac<T, G>>
hasAllDirectPermissions()
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
| Parameter | Type |
|---|---|
...permissions | Many<PermissionInput<PermissionName<T, G>>>[] |
Returns
Promise<boolean>
hasAllPermissions()
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
| Parameter | Type |
|---|---|
...permissions | Many<PermissionInput<PermissionName<T, G>>>[] |
Returns
Promise<boolean>
hasAllRoles()
hasAllRoles(...roles): Promise<boolean>;Defined in: packages/core/src/rbac.ts:1131
Parameters
| Parameter | Type |
|---|---|
...roles | Many<RoleInput<RoleName<T, G>>>[] |
Returns
Promise<boolean>
hasAnyDirectPermission()
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
| Parameter | Type |
|---|---|
...permissions | Many<PermissionInput<PermissionName<T, G>>>[] |
Returns
Promise<boolean>
hasAnyPermission()
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
| Parameter | Type |
|---|---|
...permissions | Many<PermissionInput<PermissionName<T, G>>>[] |
Returns
Promise<boolean>
hasAnyRole()
hasAnyRole(...roles): Promise<boolean>;Defined in: packages/core/src/rbac.ts:1126
Parameters
| Parameter | Type |
|---|---|
...roles | Many<RoleInput<RoleName<T, G>>>[] |
Returns
Promise<boolean>
hasDirectPermission()
hasDirectPermission(permission): Promise<boolean>;Defined in: packages/core/src/rbac.ts:1281
Checks a direct permission only (ignores roles).
Parameters
| Parameter | Type |
|---|---|
permission | PermissionInput<PermissionName<T, G>> |
Returns
Promise<boolean>
hasExactRoles()
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
| Parameter | Type |
|---|---|
...roles | Many<RoleInput<RoleName<T, G>>>[] |
Returns
Promise<boolean>
hasPermissionTo()
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
| Parameter | Type |
|---|---|
permission | PermissionInput<PermissionName<T, G>> |
Returns
Promise<boolean>
hasRole()
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
| Parameter | Type |
|---|---|
roles | Many<RoleInput<RoleName<T, G>>> |
Returns
Promise<boolean>
removeRole()
removeRole(...roles): Promise<SubjectRbac<T, G>>;Defined in: packages/core/src/rbac.ts:1067
Parameters
| Parameter | Type |
|---|---|
...roles | Many<RoleInput<RolesOf<GuardTypes<T, G>>>>[] |
Returns
Promise<SubjectRbac<T, G>>
revokeAll()
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
| Parameter | Type |
|---|---|
options? | RevokeAllOptions |
Returns
Promise<SubjectRbac<T, G>>
revokePermissionTo()
revokePermissionTo(...permissions): Promise<SubjectRbac<T, G>>;Defined in: packages/core/src/rbac.ts:1168
Parameters
| Parameter | Type |
|---|---|
...permissions | Many<PermissionInput<PermissionsOf<GuardTypes<T, G>>>>[] |
Returns
Promise<SubjectRbac<T, G>>
snapshot()
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
| Parameter | Type |
|---|---|
options? | SnapshotOptions<T, G> |
Returns
Promise<RbacSnapshot>
syncPermissions()
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
| Parameter | Type |
|---|---|
...permissions | ExpiryArgs<Many<PermissionInput<PermissionsOf<GuardTypes<T, G>>>>> |
Returns
Promise<SubjectRbac<T, G>>
syncRoles()
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
| Parameter | Type |
|---|---|
...roles | ExpiryArgs<Many<RoleInput<RolesOf<GuardTypes<T, G>>>>> |
Returns
Promise<SubjectRbac<T, G>>
teams()
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
| Parameter | Type |
|---|---|
options? | SubjectTeamsOptions |
Returns
Promise<Id[]>
withScope()
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')).
await rbac.for(user).withScope({ guard: 'api' }).hasRole('admin')Type Parameters
| Type Parameter | Default type |
|---|---|
G2 extends string | G |
Parameters
| Parameter | Type |
|---|---|
options | ScopeOptions<CheckGuard<T, G2>> |
Returns
SubjectRbac<T, ScopedGuardName<T, G2>>
