andrey-helldar /
laravel-roles
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Helldar\Roles\Models; |
||||
| 4 | |||||
| 5 | use Helldar\Roles\Constants\Tables; |
||||
| 6 | use Helldar\Roles\Facades\Database\Search; |
||||
| 7 | use Illuminate\Database\Eloquent\Model; |
||||
| 8 | use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
||||
| 9 | |||||
| 10 | /** |
||||
| 11 | * @property \Helldar\Roles\Models\Permission[]|\Illuminate\Database\Eloquent\Collection $permissions |
||||
| 12 | * @property bool $is_root |
||||
| 13 | */ |
||||
| 14 | class Role extends BaseModel |
||||
| 15 | { |
||||
| 16 | protected $table = Tables::ROLES; |
||||
| 17 | |||||
| 18 | protected $fillable = ['slug', 'title', 'is_root']; |
||||
| 19 | |||||
| 20 | protected $casts = [ |
||||
| 21 | 'is_root' => 'boolean', |
||||
| 22 | ]; |
||||
| 23 | |||||
| 24 | 138 | public function permissions(): BelongsToMany |
|||
| 25 | { |
||||
| 26 | 138 | return $this->belongsToMany(Permission::class, Tables::ROLE_PERMISSION); |
|||
| 27 | } |
||||
| 28 | |||||
| 29 | public function createPermission(string $slug): Model |
||||
| 30 | { |
||||
| 31 | return $this->permissions()->create(compact('slug')); |
||||
| 32 | } |
||||
| 33 | |||||
| 34 | /** |
||||
| 35 | * @param \Helldar\Roles\Models\Permission|string $permission |
||||
| 36 | * |
||||
| 37 | * @throws \Throwable |
||||
| 38 | */ |
||||
| 39 | public function assignPermission($permission): void |
||||
| 40 | { |
||||
| 41 | $permission = $this->findPermission($permission); |
||||
| 42 | |||||
| 43 | $this->permissions()->attach($permission->id); |
||||
| 44 | } |
||||
| 45 | |||||
| 46 | /** |
||||
| 47 | * @param \Helldar\Roles\Models\Permission[]|string[] $permissions |
||||
| 48 | * |
||||
| 49 | * @throws \Throwable |
||||
| 50 | */ |
||||
| 51 | public function assignPermissions(...$permissions): void |
||||
| 52 | { |
||||
| 53 | foreach ($permissions as $permission) { |
||||
| 54 | $this->assignPermission($permission); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 55 | } |
||||
| 56 | } |
||||
| 57 | |||||
| 58 | /** |
||||
| 59 | * @param \Helldar\Roles\Models\Permission|string $permission |
||||
| 60 | * |
||||
| 61 | * @throws \Throwable |
||||
| 62 | */ |
||||
| 63 | public function revokePermission($permission): void |
||||
| 64 | { |
||||
| 65 | $permission = $this->findPermission($permission); |
||||
| 66 | |||||
| 67 | $this->permissions()->detach($permission->id); |
||||
| 68 | } |
||||
| 69 | |||||
| 70 | /** |
||||
| 71 | * @param \Helldar\Roles\Models\Permission[]|string[] $permissions |
||||
| 72 | * |
||||
| 73 | * @throws \Throwable |
||||
| 74 | */ |
||||
| 75 | public function revokePermissions(...$permissions): void |
||||
| 76 | { |
||||
| 77 | foreach ($permissions as $permission) { |
||||
| 78 | $this->revokePermission($permission); |
||||
|
0 ignored issues
–
show
$permission of type Helldar\Roles\Models\Permission[]|string[] is incompatible with the type Helldar\Roles\Models\Permission|string expected by parameter $permission of Helldar\Roles\Models\Role::revokePermission().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 79 | } |
||||
| 80 | } |
||||
| 81 | |||||
| 82 | /** |
||||
| 83 | * @param int[] $permissions_ids |
||||
| 84 | */ |
||||
| 85 | 138 | public function syncPermissions(array $permissions_ids): void |
|||
| 86 | { |
||||
| 87 | 138 | $this->permissions()->sync($permissions_ids); |
|||
| 88 | 138 | } |
|||
| 89 | |||||
| 90 | /** |
||||
| 91 | * @param \Helldar\Roles\Models\Permission|string $permission |
||||
| 92 | * |
||||
| 93 | * @return bool |
||||
| 94 | */ |
||||
| 95 | public function hasPermission($permission): bool |
||||
| 96 | { |
||||
| 97 | return Search::by($this->permissions(), $permission)->exists(); |
||||
|
0 ignored issues
–
show
|
|||||
| 98 | } |
||||
| 99 | } |
||||
| 100 |