| @@ 288-332 (lines=45) @@ | ||
| 285 | * |
|
| 286 | * @return $this |
|
| 287 | */ |
|
| 288 | public function givePermissionTo(...$permissions) |
|
| 289 | { |
|
| 290 | $permissions = collect($permissions) |
|
| 291 | ->flatten() |
|
| 292 | ->map(function ($permission) { |
|
| 293 | if (empty($permission)) { |
|
| 294 | return false; |
|
| 295 | } |
|
| 296 | ||
| 297 | return $this->getStoredPermission($permission); |
|
| 298 | }) |
|
| 299 | ->filter(function ($permission) { |
|
| 300 | return $permission instanceof Permission; |
|
| 301 | }) |
|
| 302 | ->each(function ($permission) { |
|
| 303 | $this->ensureModelSharesGuard($permission); |
|
| 304 | }) |
|
| 305 | ->map->id |
|
| 306 | ->all(); |
|
| 307 | ||
| 308 | $model = $this->getModel(); |
|
| 309 | ||
| 310 | if ($model->exists) { |
|
| 311 | $this->permissions()->sync($permissions, false); |
|
| 312 | $model->load('permissions'); |
|
| 313 | } else { |
|
| 314 | $class = \get_class($model); |
|
| 315 | ||
| 316 | $class::saved( |
|
| 317 | function ($object) use ($permissions, $model) { |
|
| 318 | static $modelLastFiredOn; |
|
| 319 | if ($modelLastFiredOn !== null && $modelLastFiredOn === $model) { |
|
| 320 | return; |
|
| 321 | } |
|
| 322 | $object->permissions()->sync($permissions, false); |
|
| 323 | $object->load('permissions'); |
|
| 324 | $modelLastFiredOn = $object; |
|
| 325 | } |
|
| 326 | ); |
|
| 327 | } |
|
| 328 | ||
| 329 | $this->forgetCachedPermissions(); |
|
| 330 | ||
| 331 | return $this; |
|
| 332 | } |
|
| 333 | ||
| 334 | /** |
|
| 335 | * Remove all current permissions and set the given ones. |
|
| @@ 140-183 (lines=44) @@ | ||
| 137 | * |
|
| 138 | * @return $this |
|
| 139 | */ |
|
| 140 | public function assignRole(...$roles) |
|
| 141 | { |
|
| 142 | $roles = collect($roles) |
|
| 143 | ->flatten() |
|
| 144 | ->map(function ($role) { |
|
| 145 | if (empty($role)) { |
|
| 146 | return false; |
|
| 147 | } |
|
| 148 | ||
| 149 | return $this->getStoredRole($role); |
|
| 150 | }) |
|
| 151 | ->filter(function ($role) { |
|
| 152 | return $role instanceof Role; |
|
| 153 | }) |
|
| 154 | ->each(function ($role) { |
|
| 155 | $this->ensureModelSharesGuard($role); |
|
| 156 | }) |
|
| 157 | ->map->id |
|
| 158 | ->all(); |
|
| 159 | ||
| 160 | $model = $this->getModel(); |
|
| 161 | ||
| 162 | if ($model->exists) { |
|
| 163 | $this->roles()->sync($roles, false); |
|
| 164 | $model->load('roles'); |
|
| 165 | } else { |
|
| 166 | $class = \get_class($model); |
|
| 167 | ||
| 168 | $class::saved( |
|
| 169 | function ($object) use ($roles, $model) { |
|
| 170 | static $modelLastFiredOn; |
|
| 171 | if ($modelLastFiredOn !== null && $modelLastFiredOn === $model) { |
|
| 172 | return; |
|
| 173 | } |
|
| 174 | $object->roles()->sync($roles, false); |
|
| 175 | $object->load('roles'); |
|
| 176 | $modelLastFiredOn = $object; |
|
| 177 | }); |
|
| 178 | } |
|
| 179 | ||
| 180 | $this->forgetCachedPermissions(); |
|
| 181 | ||
| 182 | return $this; |
|
| 183 | } |
|
| 184 | ||
| 185 | /** |
|
| 186 | * Revoke the given role from the model. |
|