I allow count limit on assign role
This commit is contained in:
parent
2d35fab83d
commit
84d81863ed
|
|
@ -55,7 +55,7 @@ trait HasRoles
|
||||||
*
|
*
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function assignRole(string|Role $role)
|
public function assignRole(string|Role $role, int $max_times = 1)
|
||||||
{
|
{
|
||||||
if (is_string($role) && !is_numeric($role)) {
|
if (is_string($role) && !is_numeric($role)) {
|
||||||
$role = config('roles.models.role', \Blax\Roles\Models\Role::class)::firstOrCreate([
|
$role = config('roles.models.role', \Blax\Roles\Models\Role::class)::firstOrCreate([
|
||||||
|
|
@ -66,6 +66,13 @@ trait HasRoles
|
||||||
$role = config('roles.models.role', \Blax\Roles\Models\Role::class)::find($role);
|
$role = config('roles.models.role', \Blax\Roles\Models\Role::class)::find($role);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($max_times >= 0) {
|
||||||
|
$currentCount = $this->roles()->wherePivot('role_id', $role->id)->count();
|
||||||
|
if ($currentCount >= $max_times) {
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ($role instanceof Role) {
|
if ($role instanceof Role) {
|
||||||
$this->roles()->attach($role);
|
$this->roles()->attach($role);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue