|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Encore\Admin\Auth\Database; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
|
6
|
|
|
|
|
7
|
|
|
trait HasPermissions |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Get all permissions of user. |
|
11
|
|
|
* |
|
12
|
|
|
* @return mixed |
|
13
|
|
|
*/ |
|
14
|
|
|
public function allPermissions(): Collection |
|
15
|
|
|
{ |
|
16
|
|
|
return $this->roles()->with('permissions')->get()->pluck('permissions')->flatten()->merge($this->permissions); |
|
|
|
|
|
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Check if user has permission. |
|
21
|
|
|
* |
|
22
|
|
|
* @param $ability |
|
23
|
|
|
* @param array $arguments |
|
24
|
|
|
* |
|
25
|
|
|
* @return bool |
|
26
|
|
|
*/ |
|
27
|
|
|
public function can($ability, $arguments = []): bool |
|
|
|
|
|
|
28
|
|
|
{ |
|
29
|
|
|
if (empty($ability)) { |
|
30
|
|
|
return true; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
if ($this->isAdministrator()) { |
|
34
|
|
|
return true; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
if ($this->permissions->pluck('slug')->contains($ability)) { |
|
38
|
|
|
return true; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
return $this->roles->pluck('permissions')->flatten()->pluck('slug')->contains($ability); |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Check if user has no permission. |
|
46
|
|
|
* |
|
47
|
|
|
* @param iterable|string $abilities |
|
48
|
|
|
* @param array|mixed $arguments |
|
49
|
|
|
* @return bool |
|
50
|
|
|
*/ |
|
51
|
|
|
public function cant($abilities, $arguments = []): bool |
|
52
|
|
|
{ |
|
53
|
|
|
return !$this->can($abilities, $arguments); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Check if user has no permission. |
|
58
|
|
|
* |
|
59
|
|
|
* @param iterable|string $abilities |
|
60
|
|
|
* @param array|mixed $arguments |
|
61
|
|
|
* @return bool |
|
62
|
|
|
*/ |
|
63
|
|
|
public function cannot($abilities, $arguments = []): bool |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->cant($abilities, $arguments); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Check if user is administrator. |
|
70
|
|
|
* |
|
71
|
|
|
* @return bool |
|
72
|
|
|
*/ |
|
73
|
|
|
public function isAdministrator(): bool |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->isRole('administrator'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Check if user is $role. |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $role |
|
82
|
|
|
* |
|
83
|
|
|
* @return bool |
|
84
|
|
|
*/ |
|
85
|
|
|
public function isRole(string $role): bool |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->roles->pluck('slug')->contains($role); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Check if user in $roles. |
|
92
|
|
|
* |
|
93
|
|
|
* @param array $roles |
|
94
|
|
|
* |
|
95
|
|
|
* @return bool |
|
96
|
|
|
*/ |
|
97
|
|
|
public function inRoles(array $roles = []): bool |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->roles->pluck('slug')->intersect($roles)->isNotEmpty(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* If visible for roles. |
|
104
|
|
|
* |
|
105
|
|
|
* @param $roles |
|
106
|
|
|
* |
|
107
|
|
|
* @return bool |
|
108
|
|
|
*/ |
|
109
|
|
|
public function visible(array $roles = []): bool |
|
110
|
|
|
{ |
|
111
|
|
|
if (empty($roles)) { |
|
112
|
|
|
return true; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$roles = array_column($roles, 'slug'); |
|
116
|
|
|
|
|
117
|
|
|
return $this->inRoles($roles) || $this->isAdministrator(); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Detach models from the relationship. |
|
122
|
|
|
* |
|
123
|
|
|
* @return void |
|
124
|
|
|
*/ |
|
125
|
|
|
protected static function bootHasPermissions() |
|
126
|
|
|
{ |
|
127
|
|
|
static::deleting(function ($model) { |
|
128
|
|
|
$model->roles()->detach(); |
|
129
|
|
|
|
|
130
|
|
|
$model->permissions()->detach(); |
|
131
|
|
|
}); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: