|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Napp\Core\Acl\Role; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Collection; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Trait HasRole. |
|
10
|
|
|
*/ |
|
11
|
|
|
trait HasRole |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
|
15
|
|
|
*/ |
|
16
|
94 |
|
public function roles(): BelongsToMany |
|
17
|
|
|
{ |
|
18
|
94 |
|
return $this->belongsToMany(config('acl.models.role'), config('acl.table_names.users_roles')); |
|
|
|
|
|
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @throws \Exception |
|
23
|
|
|
* |
|
24
|
|
|
* @return bool |
|
25
|
|
|
*/ |
|
26
|
2 |
|
public function delete() |
|
27
|
|
|
{ |
|
28
|
2 |
|
$this->roles()->detach(); |
|
29
|
|
|
|
|
30
|
2 |
|
return parent::delete(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @return bool |
|
35
|
|
|
*/ |
|
36
|
2 |
|
public function getDeleteableAttribute(): bool |
|
37
|
|
|
{ |
|
38
|
2 |
|
$user = auth()->user(); |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
2 |
|
return $user->getAuthIdentifier() != $this->attributes['id'] |
|
|
|
|
|
|
41
|
2 |
|
&& $user->roles[0]->access_level <= $this->roles[0]->access_level; |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @return Collection |
|
46
|
|
|
*/ |
|
47
|
56 |
|
public function getRoles() |
|
48
|
|
|
{ |
|
49
|
56 |
|
return $this->roles; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return array |
|
54
|
|
|
*/ |
|
55
|
46 |
|
public function getRoleIds(): array |
|
56
|
|
|
{ |
|
57
|
46 |
|
$ids = []; |
|
58
|
|
|
|
|
59
|
46 |
|
foreach ($this->getRoles() as $role) { |
|
60
|
46 |
|
$ids[] = $role->id; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
46 |
|
return $ids; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Returns an array of merged permissions for each group the user is in. |
|
68
|
|
|
* |
|
69
|
|
|
* @return array |
|
70
|
|
|
*/ |
|
71
|
34 |
|
public function getMergedPermissions(): array |
|
72
|
|
|
{ |
|
73
|
34 |
|
$permissions = []; |
|
74
|
34 |
|
foreach ($this->getRoles() as $role) { |
|
75
|
34 |
|
$permissions = \array_merge($permissions, $role->getPermissions() ?? []); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
// merge with users own permissions |
|
79
|
34 |
|
return \array_merge($permissions, $this->getPermissions()); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return array |
|
84
|
|
|
*/ |
|
85
|
34 |
|
public function getPermissions(): array |
|
86
|
|
|
{ |
|
87
|
34 |
|
return []; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @return bool |
|
92
|
|
|
*/ |
|
93
|
44 |
|
public function isSuperUser(): bool |
|
94
|
|
|
{ |
|
95
|
44 |
|
return \in_array(1, $this->getRoleIds(), true); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @return bool |
|
100
|
|
|
*/ |
|
101
|
46 |
|
public function isAdmin(): bool |
|
102
|
|
|
{ |
|
103
|
46 |
|
return \in_array(2, $this->getRoleIds(), true); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @return int Lowest value of the users groups access levels |
|
108
|
|
|
*/ |
|
109
|
2 |
|
public function getAccessLevels(): int |
|
110
|
|
|
{ |
|
111
|
2 |
|
$accessLevel = PHP_INT_MAX; |
|
112
|
|
|
|
|
113
|
2 |
|
foreach ($this->getRoles() as $role) { |
|
114
|
2 |
|
$role->access_level_parent = (null === $role->access_level_parent) ? PHP_INT_MAX : $role->access_level_parent; |
|
115
|
2 |
|
$accessLevel = $this->getLowestAccessLevel($role->access_level, $role->access_level_parent, $accessLevel); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
2 |
|
return $accessLevel; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param int $level |
|
123
|
|
|
* @param int $parent |
|
124
|
|
|
* @param int $current |
|
125
|
|
|
* |
|
126
|
|
|
* @return int |
|
127
|
|
|
*/ |
|
128
|
2 |
|
protected function getLowestAccessLevel($level, $parent, $current): int |
|
129
|
|
|
{ |
|
130
|
2 |
|
return min([$level, $parent, $current]); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.