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