|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yajra\Acl\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
|
6
|
|
|
use Yajra\Acl\Models\Permission; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @mixin \Illuminate\Database\Eloquent\Model |
|
10
|
|
|
* @property Permission[]|\Illuminate\Database\Eloquent\Collection $permissions |
|
11
|
|
|
*/ |
|
12
|
|
|
trait HasRoleAndPermission |
|
13
|
|
|
{ |
|
14
|
|
|
use HasRole { |
|
15
|
|
|
HasRole::getPermissions as getRolePermissions; |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Grants the given permission to the user. |
|
20
|
|
|
* |
|
21
|
|
|
* @param \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model|array $id |
|
22
|
|
|
* @param array $attributes |
|
23
|
|
|
* @param bool $touch |
|
24
|
|
|
* @return void |
|
25
|
|
|
*/ |
|
26
|
|
|
public function grantPermission($id, array $attributes = [], $touch = true) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->permissions()->attach($id, $attributes, $touch); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Get related permissions. |
|
33
|
|
|
* |
|
34
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
|
35
|
|
|
*/ |
|
36
|
|
|
public function permissions(): BelongsToMany |
|
37
|
|
|
{ |
|
38
|
|
|
return $this->belongsToMany(config('acl.permission', Permission::class))->withTimestamps(); |
|
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Revokes the given permission from the user. |
|
43
|
|
|
* |
|
44
|
|
|
* @param mixed $ids |
|
45
|
|
|
* @param bool $touch |
|
46
|
|
|
* @return int |
|
47
|
|
|
*/ |
|
48
|
|
|
public function revokePermission($ids = null, $touch = true): int |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->permissions()->detach($ids, $touch); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Syncs the given permission(s) with the user. |
|
55
|
|
|
* |
|
56
|
|
|
* @param \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model|array $ids |
|
57
|
|
|
* @param bool $detaching |
|
58
|
|
|
* @return array |
|
59
|
|
|
*/ |
|
60
|
|
|
public function syncPermissions($ids, $detaching = true): array |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->permissions()->sync($ids, $detaching); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Revokes all permissions from the user. |
|
67
|
|
|
* |
|
68
|
|
|
* @return int |
|
69
|
|
|
*/ |
|
70
|
|
|
public function revokeAllPermissions(): int |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->permissions()->detach(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Get all user permissions slug. |
|
77
|
|
|
* |
|
78
|
|
|
* @return array|null |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getPermissions(): array |
|
81
|
|
|
{ |
|
82
|
|
|
$rolePermissions = $this->getRolePermissions(); |
|
|
|
|
|
|
83
|
|
|
$userPermissions = $this->permissions->pluck('slug')->toArray(); |
|
84
|
|
|
|
|
85
|
|
|
return collect($userPermissions)->merge($rolePermissions)->unique()->toArray(); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: