added give and revoke helper methods

This commit is contained in:
2023-03-13 10:38:50 +10:00
parent c18b740f46
commit 0c2ac5d0a5

View File

@@ -103,6 +103,42 @@ class User extends Authenticatable implements Auditable
return ($this->permissions()->where('permission', $permission)->first() !== null); return ($this->permissions()->where('permission', $permission)->first() !== null);
} }
/**
* Give permissions to the user
*
* @param string|array $permissions The permission(s) to give.
* @return Collection
*/
public function givePermission($permissions)
{
if (!is_array($permissions)) {
$permissions = [$permissions];
}
$permissions = collect($permissions)->map(function ($permission) {
return ['permission' => $permission];
});
return $this->permissions()->firstOrCreateMany($permissions->toArray());
}
/**
* Revoke permissions from the user
*
* @param string|array $permissions The permission(s) to revoke.
* @return int
*/
public function revokePermission($permissions)
{
if (!is_array($permissions)) {
$permissions = [$permissions];
}
return $this->permissions()
->whereIn('permission', $permissions)
->delete();
}
/** /**
* Get the list of files of the user * Get the list of files of the user
* *