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 $permission |
23
|
|
|
* |
24
|
|
|
* @return bool |
25
|
|
|
*/ |
26
|
|
|
public function can(string $permission) : bool |
27
|
|
|
{ |
28
|
|
|
if ($this->isAdministrator()) { |
29
|
|
|
return true; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
if ($this->permissions->pluck('slug')->contains($permission)) { |
33
|
|
|
return true; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return $this->roles->pluck('permissions')->flatten()->pluck('slug')->contains($permission); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Check if user has no permission. |
41
|
|
|
* |
42
|
|
|
* @param $permission |
43
|
|
|
* |
44
|
|
|
* @return bool |
45
|
|
|
*/ |
46
|
|
|
public function cannot(string $permission) : bool |
47
|
|
|
{ |
48
|
|
|
return !$this->can($permission); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Check if user is administrator. |
53
|
|
|
* |
54
|
|
|
* @return mixed |
55
|
|
|
*/ |
56
|
|
|
public function isAdministrator() : bool |
57
|
|
|
{ |
58
|
|
|
return $this->isRole('administrator'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Check if user is $role. |
63
|
|
|
* |
64
|
|
|
* @param string $role |
65
|
|
|
* |
66
|
|
|
* @return mixed |
67
|
|
|
*/ |
68
|
|
|
public function isRole(string $role) : bool |
69
|
|
|
{ |
70
|
|
|
return $this->roles->pluck('slug')->contains($role); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Check if user in $roles. |
75
|
|
|
* |
76
|
|
|
* @param array $roles |
77
|
|
|
* |
78
|
|
|
* @return mixed |
79
|
|
|
*/ |
80
|
|
|
public function inRoles(array $roles = []) : bool |
81
|
|
|
{ |
82
|
|
|
return $this->roles->pluck('slug')->intersect($roles)->isNotEmpty(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* If visible for roles. |
87
|
|
|
* |
88
|
|
|
* @param $roles |
89
|
|
|
* |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
|
|
public function visible(array $roles = []) : bool |
93
|
|
|
{ |
94
|
|
|
if (empty($roles)) { |
95
|
|
|
return true; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$roles = array_column($roles, 'slug'); |
99
|
|
|
|
100
|
|
|
return $this->inRoles($roles) || $this->isAdministrator(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Detach models from the relationship. |
105
|
|
|
* |
106
|
|
|
* @return void |
107
|
|
|
*/ |
108
|
|
|
protected static function boot() |
109
|
|
|
{ |
110
|
|
|
parent::boot(); |
111
|
|
|
|
112
|
|
|
static::deleting(function ($model) { |
113
|
|
|
$model->roles()->detach(); |
114
|
|
|
|
115
|
|
|
$model->permissions()->detach(); |
116
|
|
|
}); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
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: