@@ 365-409 (lines=45) @@ | ||
362 | * |
|
363 | * @return $this |
|
364 | */ |
|
365 | public function givePermissionTo(...$permissions) |
|
366 | { |
|
367 | $permissions = collect($permissions) |
|
368 | ->flatten() |
|
369 | ->map(function ($permission) { |
|
370 | if (empty($permission)) { |
|
371 | return false; |
|
372 | } |
|
373 | ||
374 | return $this->getStoredPermission($permission); |
|
375 | }) |
|
376 | ->filter(function ($permission) { |
|
377 | return $permission instanceof Permission; |
|
378 | }) |
|
379 | ->each(function ($permission) { |
|
380 | $this->ensureModelSharesGuard($permission); |
|
381 | }) |
|
382 | ->map->id |
|
383 | ->all(); |
|
384 | ||
385 | $model = $this->getModel(); |
|
386 | ||
387 | if ($model->exists) { |
|
388 | $this->permissions()->sync($permissions, false); |
|
389 | $model->load('permissions'); |
|
390 | } else { |
|
391 | $class = \get_class($model); |
|
392 | ||
393 | $class::saved( |
|
394 | function ($object) use ($permissions, $model) { |
|
395 | static $modelLastFiredOn; |
|
396 | if ($modelLastFiredOn !== null && $modelLastFiredOn === $model) { |
|
397 | return; |
|
398 | } |
|
399 | $object->permissions()->sync($permissions, false); |
|
400 | $object->load('permissions'); |
|
401 | $modelLastFiredOn = $object; |
|
402 | } |
|
403 | ); |
|
404 | } |
|
405 | ||
406 | $this->forgetCachedPermissions(); |
|
407 | ||
408 | return $this; |
|
409 | } |
|
410 | ||
411 | /** |
|
412 | * Remove all current permissions and set the given ones. |
@@ 99-142 (lines=44) @@ | ||
96 | * |
|
97 | * @return $this |
|
98 | */ |
|
99 | public function assignRole(...$roles) |
|
100 | { |
|
101 | $roles = collect($roles) |
|
102 | ->flatten() |
|
103 | ->map(function ($role) { |
|
104 | if (empty($role)) { |
|
105 | return false; |
|
106 | } |
|
107 | ||
108 | return $this->getStoredRole($role); |
|
109 | }) |
|
110 | ->filter(function ($role) { |
|
111 | return $role instanceof Role; |
|
112 | }) |
|
113 | ->each(function ($role) { |
|
114 | $this->ensureModelSharesGuard($role); |
|
115 | }) |
|
116 | ->map->id |
|
117 | ->all(); |
|
118 | ||
119 | $model = $this->getModel(); |
|
120 | ||
121 | if ($model->exists) { |
|
122 | $this->roles()->sync($roles, false); |
|
123 | $model->load('roles'); |
|
124 | } else { |
|
125 | $class = \get_class($model); |
|
126 | ||
127 | $class::saved( |
|
128 | function ($object) use ($roles, $model) { |
|
129 | static $modelLastFiredOn; |
|
130 | if ($modelLastFiredOn !== null && $modelLastFiredOn === $model) { |
|
131 | return; |
|
132 | } |
|
133 | $object->roles()->sync($roles, false); |
|
134 | $object->load('roles'); |
|
135 | $modelLastFiredOn = $object; |
|
136 | }); |
|
137 | } |
|
138 | ||
139 | $this->forgetCachedPermissions(); |
|
140 | ||
141 | return $this; |
|
142 | } |
|
143 | ||
144 | /** |
|
145 | * Revoke the given role from the model. |