andrey-helldar /
laravel-roles
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Helldar\Roles\Models; |
||||
| 4 | |||||
| 5 | use Helldar\Roles\Constants\Tables; |
||||
| 6 | use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
||||
| 7 | |||||
| 8 | /** |
||||
| 9 | * @property \Helldar\Roles\Models\Role[]|\Illuminate\Database\Eloquent\Collection $roles |
||||
| 10 | * @mixin \Illuminate\Database\Eloquent\Model |
||||
| 11 | */ |
||||
| 12 | class Permission extends BaseModel |
||||
| 13 | { |
||||
| 14 | protected $table = Tables::PERMISSIONS; |
||||
| 15 | |||||
| 16 | 6 | public function roles(): BelongsToMany |
|||
| 17 | { |
||||
| 18 | 6 | return $this->belongsToMany(Role::class, Tables::ROLE_PERMISSION); |
|||
| 19 | } |
||||
| 20 | |||||
| 21 | /** |
||||
| 22 | * @param \Helldar\Roles\Models\Role|string $role |
||||
| 23 | * |
||||
| 24 | * @throws \Throwable |
||||
| 25 | */ |
||||
| 26 | public function assignRole($role): void |
||||
| 27 | { |
||||
| 28 | $role = $this->findRole($role); |
||||
| 29 | |||||
| 30 | $this->roles()->attach($role->id); |
||||
| 31 | } |
||||
| 32 | |||||
| 33 | /** |
||||
| 34 | * @param \Helldar\Roles\Models\Role[]|string[] $roles |
||||
| 35 | * |
||||
| 36 | * @throws \Throwable |
||||
| 37 | */ |
||||
| 38 | public function assignRoles(...$roles): void |
||||
| 39 | { |
||||
| 40 | foreach ($roles as $role) { |
||||
| 41 | $this->assignRole($role); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 42 | } |
||||
| 43 | } |
||||
| 44 | |||||
| 45 | /** |
||||
| 46 | * @param \Helldar\Roles\Models\Role|string $role |
||||
| 47 | * |
||||
| 48 | * @throws \Throwable |
||||
| 49 | */ |
||||
| 50 | public function revokeRole($role): void |
||||
| 51 | { |
||||
| 52 | $role = $this->findRole($role); |
||||
| 53 | |||||
| 54 | $this->roles()->detach($role->id); |
||||
| 55 | } |
||||
| 56 | |||||
| 57 | /** |
||||
| 58 | * @param \Helldar\Roles\Models\Role[]|string[] $roles |
||||
| 59 | * |
||||
| 60 | * @throws \Throwable |
||||
| 61 | */ |
||||
| 62 | public function revokeRoles(...$roles): void |
||||
| 63 | { |
||||
| 64 | foreach ($roles as $role) { |
||||
| 65 | $this->revokeRole($role); |
||||
|
0 ignored issues
–
show
$role of type Helldar\Roles\Models\Role[]|string[] is incompatible with the type Helldar\Roles\Models\Role|string expected by parameter $role of Helldar\Roles\Models\Permission::revokeRole().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 66 | } |
||||
| 67 | } |
||||
| 68 | |||||
| 69 | /** |
||||
| 70 | * @param int[] $roles_ids |
||||
| 71 | * |
||||
| 72 | * @return array |
||||
| 73 | */ |
||||
| 74 | public function syncRoles(array $roles_ids): array |
||||
| 75 | { |
||||
| 76 | return $this->roles()->sync($roles_ids); |
||||
| 77 | } |
||||
| 78 | } |
||||
| 79 |