1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Itstructure\LaRbac\Models; |
4
|
|
|
|
5
|
|
|
use Itstructure\LaRbac\Contracts\User as RbacUserContract; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Administrable |
9
|
|
|
* |
10
|
|
|
* @package Itstructure\LaRbac\Models |
11
|
|
|
*/ |
12
|
|
|
trait Administrable |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* New filled roles. |
16
|
|
|
* |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
private $_roles; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var int|null |
23
|
|
|
*/ |
24
|
|
|
private $_countAdministrativeRoles; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* User identifier. |
28
|
|
|
* |
29
|
|
|
* @return int |
30
|
|
|
*/ |
31
|
|
|
public function getIdAttribute(): int |
32
|
|
|
{ |
33
|
|
|
return $this->attributes['id']; |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* User name. |
38
|
|
|
* |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
|
|
public function getNameAttribute(): string |
42
|
|
|
{ |
43
|
|
|
return $this->attributes['name']; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Set new filled roles. |
48
|
|
|
* |
49
|
|
|
* @param $value |
50
|
|
|
* |
51
|
|
|
* @return void |
52
|
|
|
*/ |
53
|
|
|
public function setRolesAttribute($value): void |
54
|
|
|
{ |
55
|
|
|
$this->_roles = $value; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get user roles by relation. |
60
|
|
|
* |
61
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
62
|
|
|
*/ |
63
|
|
|
public function roles() |
64
|
|
|
{ |
65
|
|
|
return $this->belongsToMany(Role::class, 'user_role', 'user_id', 'role_id')->withTimestamps(); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Checks if User has access to $permissions. |
70
|
|
|
* |
71
|
|
|
* @param array $permissions |
72
|
|
|
* |
73
|
|
|
* @return bool |
74
|
|
|
*/ |
75
|
|
|
public function hasAccess(array $permissions) : bool |
76
|
|
|
{ |
77
|
|
|
// check if the permission is available in any role |
78
|
|
|
/* @var Role $role */ |
79
|
|
|
foreach ($this->roles as $role) { |
|
|
|
|
80
|
|
|
|
81
|
|
|
if($role->hasAccess($permissions)) { |
82
|
|
|
return true; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Checks if the user belongs to role. |
90
|
|
|
* |
91
|
|
|
* @param string $roleSlug |
92
|
|
|
* |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
|
|
public function inRole(string $roleSlug): bool |
96
|
|
|
{ |
97
|
|
|
return $this->roles()->where('slug', $roleSlug)->count() == 1; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Can assign role checking. |
102
|
|
|
* |
103
|
|
|
* @param RbacUserContract $member |
104
|
|
|
* @param Role $role |
105
|
|
|
* |
106
|
|
|
* @return bool |
107
|
|
|
*/ |
108
|
|
|
public function canAssignRole(RbacUserContract $member, Role $role): bool |
109
|
|
|
{ |
110
|
|
|
if ($this->countAdministrativeRoles() == 0) { |
111
|
|
|
return false; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if ($this->getIdAttribute() != $member->getIdAttribute()) { |
115
|
|
|
return true; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if ($this->countAdministrativeRoles() > 1) { |
119
|
|
|
return true; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if (!$role->hasAccess([Permission::ADMIN_PERMISSION])) { |
123
|
|
|
return true; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if ($this->inRole($role->slug)) { |
127
|
|
|
return false; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return true; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Synchronize user roles after save model. |
135
|
|
|
* |
136
|
|
|
* @param array $options |
137
|
|
|
* |
138
|
|
|
* @return bool |
139
|
|
|
*/ |
140
|
|
|
public function save(array $options = []) |
141
|
|
|
{ |
142
|
|
|
if (!parent::save($options)) { |
143
|
|
|
return false; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
if (null !== $this->_roles) { |
147
|
|
|
$this->roles()->sync($this->_roles); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return true; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return int |
155
|
|
|
*/ |
156
|
|
|
private function countAdministrativeRoles(): int |
157
|
|
|
{ |
158
|
|
|
if (null !== $this->_countAdministrativeRoles) { |
159
|
|
|
return $this->_countAdministrativeRoles; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$this->_countAdministrativeRoles = 0; |
163
|
|
|
|
164
|
|
|
/* @var Role $role */ |
165
|
|
|
foreach ($this->roles as $role) { |
|
|
|
|
166
|
|
|
|
167
|
|
|
if($role->hasAccess([Permission::ADMIN_PERMISSION])) { |
168
|
|
|
$this->_countAdministrativeRoles += 1; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return $this->_countAdministrativeRoles; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
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: