User Permissions
User Permissions is an extended version of HasRole
that allows us to directly add custom permissions to a User
. To implement this setup, you need to extend Yajra\Acl\Traits\HasRoleAndPermission
trait on your User
model.
class User extends Authenticatable{ use HasRoleAndPermission;}
-
getPermissions()
-
grantPermission($ids, array $attributes = [], $touch = true)
-
grantPermissionBySlug($slug)
-
grantPermissionByResource($resource)
-
revokePermission($ids = null, $touch = true)($ids = null, $touch = true)
-
revokePermissionBySlug($slug)
-
revokeAllPermissions()
-
syncPermissions($ids, $detaching = true)
getPermissions()
Retrieves an array of assigned permission slugs for the user.
$user = User::find(1); return $user->getPermissions();
grantPermission($ids, array $attributes = [], $touch = true)
Grant the given permission to the user.
$user = User::find(1);$user->grantPermission(1); $permissions = Permission::all();$user->grantPermission($permissions);
grantPermissionBySlug($slug)
Grant the given permission slug to the user.
$user = User::find(1);$user->grantPermissionBySlug('create-post'); $permissions = ['create-post', 'view-post'];$user->grantPermissionBySlug($permissions);
grantPermissionByResource($resource)
Grant the given permission resource to the user.
$user = User::find(1);$user->grantPermissionByResource('Posts'); $resources = ['Users', 'Posts'];$user->grantPermissionByResource($resources);
revokePermission($ids = null, $touch = true)
Revokes the given permission from the user.
$user = User::find(1); $user->revokePermission(1);$user->revokePermission([1, 2]);
revokePermissionBySlug($slug)
Revokes the given permission by slug from the user.
$user = User::find(1); $user->revokePermissionBySlug('create-posts');$user->revokePermissionBySlug(['create-posts', 'update-posts']);
##revokeAllPermissions() Revokes all permissions from the user.
$user = User::find(1); $user->revokeAllPermissions();
syncPermissions($ids, $detaching = true)
Syncs the given permissions with the user. This will revoke any permissions not supplied.
$user = User::find(1); $user->syncPermissions(1);$user->syncPermissions([1, 2, 3]);