When we call $user->hasDirectPermission('delete articles') it returns true,īut false for $user->hasDirectPermission('edit articles'). The permission of 'delete articles' is the user's direct permission because it is assigned directly to them. Now the user can edit articles and additionally delete articles. In the above example, a role is given permission to edit articles and this role is assigned to a user. get collection $role -> permissions // return only the permission names: $role -> permissions -> pluck ( 'name' ) // count the number of permissions assigned to a role count ($role -> permissions) // or $role -> permissions -> count () #Assigning Direct Permissions To A UserĪdditionally, individual permissions can be assigned to the user too.įor instance: $role = Role :: findByName ( 'writer' ) $role -> givePermissionTo ( 'edit articles' ) $user -> assignRole ( 'writer' ) $user -> givePermissionTo ( 'delete articles' ) This collection can respond to usual Eloquent Collection operations, such as count, sort, etc. The permissions property on any given role returns a collection with all the related permission objects. NOTE: Permissions are inherited from roles automatically. String or a Spatie\Permission\Models\Permission object. The givePermissionTo and revokePermissionTo functions can accept a Or revoke & add new permissions in one go: $role -> syncPermissions () You can determine if a role has a certain permission: $role -> hasPermissionTo ( 'edit articles' ) Ī permission can be revoked from a role: $role -> revokePermissionTo ( 'edit articles' ) #Assigning Permissions to RolesĪ permission can be given to a role: $role -> givePermissionTo ( 'edit articles' ) String, a \Spatie\Permission\Models\Role object or an \Illuminate\Support\Collection object. The assignRole, hasRole, hasAnyRole, hasAllRoles, hasExactRoles and removeRole functions can accept a You can also determine if a user has exactly all of a given list of roles: $user -> hasExactRoles ( Role :: all ()) You can also determine if a user has all of a given list of roles: $user -> hasAllRoles ( Role :: all ()) You can also determine if a user has any of a given list of roles: $user -> hasAnyRole () // or $user -> hasAnyRole ( 'writer', 'reader' ) You can determine if a user has a certain role: $user -> hasRole ( 'writer' ) // or at least one role from an array of roles: $user -> hasRole () Roles can also be synced: // All current roles will be removed from the user and replaced by the array given $user -> syncRoles () #Checking Roles A role can be assigned to any user: $user -> assignRole ( 'writer' ) // You can also assign multiple roles at once $user -> assignRole ( 'writer', 'admin' ) // or as an array $user -> assignRole () Ī role can be removed from a user: $user -> removeRole ( 'writer' )
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |