HasRoles
trait HasRoles
Traits
Build Permission relations from one immutable partition and team snapshot.
Methods
Build a partition-aware belongs-to-many relation.
Build a partition-aware morph-to-many relation.
Build the related query using the relation's captured partition.
Ensure a partition-bearing relation parent belongs to the captured partition.
Apply the captured partition to pivot reads and writes.
Boot permission cleanup and queued assignment handling.
Delete the model and its permission assignments atomically.
Determine whether deleting the model removes permission assignments.
Delete one kind of assignment for a hard-deleted subject.
Delete assignment pivots owned by a Role or Permission record.
Resolve and validate a permission record's deletion partition.
Build the direct permission assignment relation for a captured context.
Get the immutable context captured by a permission assignment relation.
Forget an assignment relation loaded for an earlier context.
Get cached direct permission assignments for this model.
Get direct permissions for a model-returning public API.
Scope the model query to certain permissions only.
Add an effective permission predicate for the given permission ids.
Add a permission-effect predicate for direct and role-granted permissions.
Add a direct permission-effect predicate.
Add a role permission-effect predicate.
Scope the model query to only those without certain permissions, whether indirectly by role or by direct permission.
No description
Find a permission.
Determine if the model may perform the given permission.
Validates a wildcard permission against all permissions of a user.
An alias to hasPermissionTo(), but avoids throwing an exception.
Determine if the model has any of the given permissions.
Determine if the model has all of the given permissions.
Determine if the model has, via roles, the given permission.
Determine if the model has the given permission.
Return all the permissions the model has via roles.
Return all the permissions the model has, both directly and via roles.
Grant the given permission(s) to the model.
Deny the given permission(s) for the model.
Build a collision-safe assignment ID identity.
Index assignment IDs by their collision-safe identities.
Normalize an ID read directly from an assignment pivot.
Read current assignment pivots through their captured relation constraints.
Insert permission assignments into an empty assignment set.
Queue permission assignments until the model is saved.
Remove permission assignments queued for a captured context.
Flush all assignments queued before the model was saved.
Attach collapsed queued permission assignment batches.
Ensure queued permission assignments have their required team context.
Clear queued permission assignments after their transaction commits.
Invalidate the captured contexts of committed permission assignments.
Collapse queued permission assignments to their final edge state.
Dispatch the permission attached event when enabled and listened for.
Determine whether the permission attached event has listeners.
Remove all current permissions and set the given ones.
Remove all current permissions and set allowed and denied permissions.
Revoke the given permission(s).
Dispatch the permission detached event when enabled and listened for.
Determine whether the permission detached event has listeners.
Determine if the model has an explicit denied direct permission.
Determine if the model has an explicit denied permission via roles.
Return permissions granted through the model's roles with role-permission pivot data.
Load permissions granted through the model's roles with role-permission pivot data.
Resolve a permission for matching without throwing.
Build lookup keys for denied permissions.
Clone a permission with the matching role-permission pivot.
Normalize a permission assignment effect.
Determine if a stored permission matches an input permission.
Resolve the guard to use when matching stored permissions.
No description
Check if the model has All of the requested Direct permissions.
Check if the model has Any of the requested Direct permissions.
Boot role cleanup.
Get the role model class.
A model may have multiple roles.
Build the role assignment relation for a captured context.
Get cached role assignments for this model.
Scope the model query to certain roles only.
Scope the model query to only those without certain roles.
A model may be part of multiple teams.
Scope the model query to certain teams only.
Scope the model query to those without certain teams.
Queue role assignments until the model is saved.
Replace role assignments queued for a captured context.
Remove role assignments queued for a captured context.
Dispatch the role attached event when enabled and listened for.
Determine whether the role attached event has listeners.
Revoke the given role from the model.
Dispatch the role detached event when enabled and listened for.
Determine whether the role detached event has listeners.
Remove all current roles and set the given ones.
Determine if the model has (one of) the given role(s).
Determine if the model has any of the given role(s).
Determine if the model has all of the given role(s).
Determine if the model has exactly all of the given role(s).
Return allowed permissions directly assigned to the model.
Get the role names.
Get a stored role instance.
Convert a pipe-delimited role string to an array.
Details
in
BuildsPermissionRelations at line 35
protected BelongsToMany
permissionBelongsToMany(string $related, string $table, string $foreignPivotKey, string $relatedPivotKey, string $relationName, PermissionRelationContext|null $context = null)
Build a partition-aware belongs-to-many relation.
in
BuildsPermissionRelations at line 72
protected MorphToMany
permissionMorphToMany(string $related, string $table, string $foreignPivotKey, string $relatedPivotKey, string $relationName, bool $inverse = false, bool $teamScoped = false, PermissionRelationContext|null $context = null)
Build a partition-aware morph-to-many relation.
in
BuildsPermissionRelations at line 129
protected Builder
permissionRelationQuery(Model $related, PermissionPartition|null $partition)
Build the related query using the relation's captured partition.
in
BuildsPermissionRelations at line 144
protected void
ensurePermissionRelationParentMatches(PermissionRegistrar $registrar, PermissionPartition|null $partition)
Ensure a partition-bearing relation parent belongs to the captured partition.
in
BuildsPermissionRelations at line 178
protected BelongsToMany
applyPermissionPartitionToRelation(BelongsToMany $relation, PermissionPartition|null $partition)
Apply the captured partition to pivot reads and writes.
in
HasPermissions at line 55
protected PermissionRegistrar
permissionRegistrar()
Get the permission registrar.
in
HasPermissions at line 63
protected Dispatcher
eventDispatcher()
Get the event dispatcher.
in
HasPermissions at line 71
static void
bootHasPermissions()
Boot permission cleanup and queued assignment handling.
in
HasPermissions at line 173
int|bool|null
delete()
Delete the model and its permission assignments atomically.
in
HasPermissions at line 187
static protected bool
shouldDeletePermissionAssignments(Model $model)
Determine whether deleting the model removes permission assignments.
in
HasPermissions at line 195
static protected mixed
requireDeletionModelKey(Model $model)
Get the model key required for permission assignment cleanup.
in
HasPermissions at line 211
static protected array|null
deleteSubjectAssignments(string $morphType, mixed $modelKey, string $table)
Delete one kind of assignment for a hard-deleted subject.
in
HasPermissions at line 293
static protected void
deletePermissionRecordAssignments(mixed $modelKey, string $modelAssignmentsTable, string $pivotKey, PermissionPartition|null $partition)
Delete assignment pivots owned by a Role or Permission record.
in
HasPermissions at line 320
static protected PermissionPartition|null
permissionRecordDeletionPartition(Model $model, PermissionRegistrar $registrar)
Resolve and validate a permission record's deletion partition.
in
HasPermissions at line 350
string
getPermissionClass()
Get the permission model class.
in
HasPermissions at line 362
string
getWildcardClass()
Get the wildcard permission class.
in
HasPermissions at line 384
BelongsToMany
permissions()
A model may have multiple direct permissions.
in
HasPermissions at line 392
protected BelongsToMany
permissionAssignmentRelation(PermissionRelationContext|null $context = null)
Build the direct permission assignment relation for a captured context.
in
HasPermissions at line 412
protected PermissionRelationContext
permissionRelationContext(BelongsToMany $relation)
Get the immutable context captured by a permission assignment relation.
in
HasPermissions at line 421
protected void
forgetStalePermissionRelation(PermissionRegistrar $registrar, string $relation)
Forget an assignment relation loaded for an earlier context.
in
HasPermissions at line 432
protected Collection
getCachedDirectPermissions()
Get cached direct permission assignments for this model.
in
HasPermissions at line 534
protected Collection
directPermissionsForModelResult()
Get direct permissions for a model-returning public API.
in
HasPermissions at line 552
protected Collection
allowedDirectPermissions()
Return allowed direct permissions.
in
HasPermissions at line 566
Builder
scopePermission(Builder $query, array|Collection|int|Permission|string|UnitEnum $permissions, bool $without = false)
Scope the model query to certain permissions only.
in
HasPermissions at line 588
protected Builder
whereEffectivePermission(Builder $query, array $permissionIds)
Add an effective permission predicate for the given permission ids.
in
HasPermissions at line 613
protected Builder
wherePermissionEffect(Builder $query, int|string $permissionId, bool $denied)
Add a permission-effect predicate for direct and role-granted permissions.
in
HasPermissions at line 633
protected Builder
whereDirectPermissionEffect(Builder $query, int|string $permissionId, bool $denied)
Add a direct permission-effect predicate.
in
HasPermissions at line 648
protected Builder
whereRolePermissionEffect(Builder $query, int|string $permissionId, bool $denied)
Add a role permission-effect predicate.
in
HasPermissions at line 665
Builder
scopeWithoutPermission(Builder $query, array|Collection|int|Permission|string|UnitEnum $permissions)
Scope the model query to only those without certain permissions, whether indirectly by role or by direct permission.
in
HasPermissions at line 675
protected array
convertToPermissionModels(array|Collection|int|Permission|string|UnitEnum $permissions)
No description
in
HasPermissions at line 708
Permission
filterPermission(int|Permission|string|UnitEnum $permission, string|null $guardName = null)
Find a permission.
in
HasPermissions at line 745
bool
hasPermissionTo(int|Permission|string|UnitEnum $permission, string|null $guardName = null)
Determine if the model may perform the given permission.
in
HasPermissions at line 777
protected bool
hasWildcardPermission(int|Permission|string|UnitEnum $permission, string|null $guardName = null)
Validates a wildcard permission against all permissions of a user.
in
HasPermissions at line 808
bool
checkPermissionTo(int|Permission|string|UnitEnum $permission, string|null $guardName = null)
An alias to hasPermissionTo(), but avoids throwing an exception.
in
HasPermissions at line 822
bool
hasAnyPermission(array|Collection|int|Permission|string|UnitEnum ...$permissions)
Determine if the model has any of the given permissions.
in
HasPermissions at line 840
bool
hasAllPermissions(array|Collection|int|Permission|string|UnitEnum ...$permissions)
Determine if the model has all of the given permissions.
in
HasPermissions at line 856
protected bool
hasPermissionViaRole(Permission $permission)
Determine if the model has, via roles, the given permission.
in
HasPermissions at line 879
bool
hasDirectPermission(int|Permission|string|UnitEnum $permission)
Determine if the model has the given permission.
in
HasPermissions at line 893
Collection
getPermissionsViaRoles()
Return all the permissions the model has via roles.
in
HasPermissions at line 908
Collection
getAllPermissions()
Return all the permissions the model has, both directly and via roles.
in
HasPermissions at line 975
HasPermissions
givePermissionTo(array|Collection|int|Permission|string|UnitEnum ...$permissions)
Grant the given permission(s) to the model.
in
HasPermissions at line 985
HasPermissions
denyPermissionTo(array|Collection|int|Permission|string|UnitEnum ...$permissions)
Deny the given permission(s) for the model.
in
HasPermissions at line 1083
protected string
assignmentIdIdentity(int|string $id)
Build a collision-safe assignment ID identity.
in
HasPermissions at line 1094
protected array
indexAssignmentIds(array $ids)
Index assignment IDs by their collision-safe identities.
in
HasPermissions at line 1108
protected int|string
normalizeRelatedPivotId(BelongsToMany $relation, mixed $id)
Normalize an ID read directly from an assignment pivot.
in
HasPermissions at line 1121
protected Collection
readCurrentAssignmentPivots(BelongsToMany $relation, array $columns, array|null $ids = null)
Read current assignment pivots through their captured relation constraints.
in
HasPermissions at line 1288
protected void
attachPermissionAssignments(array $permissions, array $pivot, PermissionRelationContext|null $context, string $pivotClass)
Insert permission assignments into an empty assignment set.
in
HasPermissions at line 1315
protected void
queuePermissionAssignments(array $permissions, array $pivot, PermissionRelationContext $context, string $pivotClass)
Queue permission assignments until the model is saved.
in
HasPermissions at line 1364
protected void
removeQueuedPermissionAssignments(array $permissions, PermissionRelationContext $context)
Remove permission assignments queued for a captured context.
at line 546
protected void
flushQueuedPermissionAssignments()
Flush all assignments queued before the model was saved.
in
HasPermissions at line 1424
protected void
attachQueuedPermissionAssignmentBatches(array $assignments)
Attach collapsed queued permission assignment batches.
in
HasPermissions at line 1441
protected void
ensureQueuedPermissionAssignmentTeamsSelected(array $assignments, PermissionRegistrar $registrar)
Ensure queued permission assignments have their required team context.
in
HasPermissions at line 1453
protected void
clearQueuedPermissionAssignments()
Clear queued permission assignments after their transaction commits.
in
HasPermissions at line 1463
protected void
invalidateQueuedPermissionAssignmentContexts(array $assignments)
Invalidate the captured contexts of committed permission assignments.
in
HasPermissions at line 1490
protected array
collapseQueuedPermissionAssignments()
Collapse queued permission assignments to their final edge state.
in
HasPermissions at line 1535
protected void
dispatchPermissionAttachedEvent(array $permissions)
Dispatch the permission attached event when enabled and listened for.
in
HasPermissions at line 1547
protected bool
permissionAttachedEventIsListenedFor()
Determine whether the permission attached event has listeners.
in
HasPermissions at line 1556
void
forgetWildcardPermissionIndex()
Forget the wildcard permission index.
in
HasPermissions at line 1568
HasPermissions
syncPermissions(array|Collection|int|Permission|string|UnitEnum ...$permissions)
Remove all current permissions and set the given ones.
in
HasPermissions at line 1643
array
syncPermissionEffects(array|Collection $allowed = [], array|Collection $denied = [])
Remove all current permissions and set allowed and denied permissions.
For unsaved models, assignments are queued until the model is saved and the returned change set is empty because no database rows are changed yet.
in
HasPermissions at line 1728
HasPermissions
revokePermissionTo(Permission|Permission[]|string|string[]|UnitEnum $permission)
Revoke the given permission(s).
in
HasPermissions at line 1777
protected void
dispatchPermissionDetachedEvent(mixed $permission)
Dispatch the permission detached event when enabled and listened for.
in
HasPermissions at line 1789
protected bool
permissionDetachedEventIsListenedFor()
Determine whether the permission detached event has listeners.
in
HasPermissions at line 1800
bool
hasDeniedPermission(int|Permission|string|UnitEnum $permission, string|null $guardName = null)
Determine if the model has an explicit denied direct permission.
in
HasPermissions at line 1816
bool
hasDeniedPermissionViaRoles(int|Permission|string|UnitEnum $permission, string|null $guardName = null)
Determine if the model has an explicit denied permission via roles.
in
HasPermissions at line 1853
protected Collection
getPermissionsViaRolesWithPivots()
Return permissions granted through the model's roles with role-permission pivot data.
in
HasPermissions at line 1872
protected Collection
loadPermissionsViaRolesWithPivots()
Load permissions granted through the model's roles with role-permission pivot data.
in
HasPermissions at line 1905
protected Model|null
permissionForMatch(mixed $permission, string $guardName)
Resolve a permission for matching without throwing.
in
HasPermissions at line 1937
protected array
deniedPermissionKeys(Collection ...$permissionCollections)
Build lookup keys for denied permissions.
in
HasPermissions at line 1955
protected string
permissionComparisonKey(Model $permission)
Build a permission comparison key.
in
HasPermissions at line 1963
protected Model
permissionWithRolePivot(Model $permission, Model $role, PermissionRegistrar $registrar, PermissionPartition|null $partition)
Clone a permission with the matching role-permission pivot.
in
HasPermissions at line 1995
protected bool
pivotIsDenied(Model $model)
Determine if a hydrated pivot marks the permission as denied.
in
HasPermissions at line 2010
protected bool
permissionEffectIsDenied(mixed $value)
Normalize a permission assignment effect.
in
HasPermissions at line 2019
protected Collection
relationCollection(Model $model, string $relation)
Get a hydrated relation collection.
in
HasPermissions at line 2040
protected bool
storedPermissionMatches(Model $storedPermission, mixed $permission, string|null $guardName = null)
Determine if a stored permission matches an input permission.
in
HasPermissions at line 2063
protected string
guardNameForPermissionMatch(mixed $permission, string|null $guardName = null)
Resolve the guard to use when matching stored permissions.
in
HasPermissions at line 2090
Collection
getPermissionNames()
Get the permission names.
in
HasPermissions at line 2099
protected Collection|Permission)
getStoredPermission(array|Collection|int|Permission|string|UnitEnum $permissions, PermissionPartition|null $partition = null)
No description
in
HasPermissions at line 2187
protected void
ensureModelSharesGuard(Permission|Role $roleOrPermission)
No description
in
HasPermissions at line 2197
protected Collection
getGuardNames()
Get the guard names for the model.
in
HasPermissions at line 2205
protected string
getDefaultGuardName()
Get the default guard name for the model.
in
HasPermissions at line 2213
void
forgetCachedPermissions()
Forget the cached permissions.
in
HasPermissions at line 2223
bool
hasAllDirectPermissions(array|Collection|int|Permission|string|UnitEnum ...$permissions)
Check if the model has All of the requested Direct permissions.
in
HasPermissions at line 2241
bool
hasAnyDirectPermission(array|Collection|int|Permission|string|UnitEnum ...$permissions)
Check if the model has Any of the requested Direct permissions.
at line 42
static void
bootHasRoles()
Boot role cleanup.
at line 118
string
getRoleClass()
Get the role model class.
at line 130
BelongsToMany
roles()
A model may have multiple roles.
at line 138
protected BelongsToMany
roleAssignmentRelation(PermissionRelationContext|null $context = null)
Build the role assignment relation for a captured context.
at line 171
protected Collection
getCachedRoles()
Get cached role assignments for this model.
at line 205
Builder
scopeRole(Builder $query, array|Collection|int|Role|string|UnitEnum $roles, string|null $guard = null, bool $without = false)
Scope the model query to certain roles only.
at line 253
Builder
scopeWithoutRole(Builder $query, array|Collection|int|Role|string|UnitEnum $roles, string|null $guard = null)
Scope the model query to only those without certain roles.
at line 265
BelongsToMany
teams()
A model may be part of multiple teams.
When the teams feature is disabled this returns an empty BelongsToMany so tooling that introspects model relations (e.g. ide-helper:models) does not break. Querying it is a no-op and produces no rows.
at line 301
Builder
scopeTeam(Builder $query, array|Collection|int|Model|string $teams, bool $without = false)
Scope the model query to certain teams only.
at line 342
Builder
scopeWithoutTeam(Builder $query, array|Collection|int|Model|string $teams)
Scope the model query to those without certain teams.
at line 381
HasRoles
assignRole(array|Collection|int|Role|string|UnitEnum ...$roles)
Assign the given role to the model.
at line 454
protected void
queueRoleAssignments(array $roles, array $pivot, PermissionRelationContext $context, string $pivotClass)
Queue role assignments until the model is saved.
at line 484
protected void
replaceQueuedRoleAssignments(array $roles, array $pivot, PermissionRelationContext $context, string $pivotClass)
Replace role assignments queued for a captured context.
at line 512
protected void
removeQueuedRoleAssignments(array $roles, PermissionRelationContext $context, string $pivotClass)
Remove role assignments queued for a captured context.
at line 612
protected void
dispatchRoleAttachedEvent(array $roles)
Dispatch the role attached event when enabled and listened for.
at line 624
protected bool
roleAttachedEventIsListenedFor()
Determine whether the role attached event has listeners.
at line 636
HasRoles
removeRole(array|Collection|int|Role|string|UnitEnum ...$role)
Revoke the given role from the model.
at line 690
protected void
dispatchRoleDetachedEvent(array $roles)
Dispatch the role detached event when enabled and listened for.
at line 702
protected bool
roleDetachedEventIsListenedFor()
Determine whether the role detached event has listeners.
at line 714
HasRoles
syncRoles(array|Collection|int|Role|string|UnitEnum ...$roles)
Remove all current roles and set the given ones.
at line 792
bool
hasRole(array|Collection|int|Role|string|UnitEnum $roles, string|null $guard = null)
Determine if the model has (one of) the given role(s).
at line 857
bool
hasAnyRole(array|Collection|int|Role|string|UnitEnum ...$roles)
Determine if the model has any of the given role(s).
Alias to hasRole() but without Guard controls
at line 867
bool
hasAllRoles(array|Collection|Role|string|UnitEnum $roles, string|null $guard = null)
Determine if the model has all of the given role(s).
at line 908
bool
hasExactRoles(array|Collection|Role|string|UnitEnum $roles, string|null $guard = null)
Determine if the model has exactly all of the given role(s).
at line 941
Collection
getDirectPermissions()
Return allowed permissions directly assigned to the model.
at line 951
Collection
getRoleNames()
Get the role names.
at line 962
protected Role
getStoredRole(int|Role|string|UnitEnum $role, PermissionPartition|null $partition = null)
Get a stored role instance.
at line 1066
protected array
convertPipeToArray(string $pipeString)
Convert a pipe-delimited role string to an array.