1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Itstructure\RbacModule\models; |
4
|
|
|
|
5
|
|
|
use yii\rbac\{ManagerInterface, Role as BaseRole}; |
6
|
|
|
use yii\base\{Model, InvalidConfigException}; |
7
|
|
|
use Itstructure\RbacModule\interfaces\{ModelInterface, RbacIdentityInterface}; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class for validation user(profile) roles. |
11
|
|
|
* |
12
|
|
|
* @property string $userUame |
13
|
|
|
* @property BaseRole[] $roles |
14
|
|
|
* @property RbacIdentityInterface $profileModel |
15
|
|
|
* @property ManagerInterface $authManager |
16
|
|
|
* |
17
|
|
|
* @package Itstructure\UsersModule\models |
18
|
|
|
*/ |
19
|
|
|
class ProfileValidate extends Model implements ModelInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Current profile (user) model. |
23
|
|
|
* |
24
|
|
|
* @var RbacIdentityInterface |
25
|
|
|
*/ |
26
|
|
|
private $profileModel; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Auth manager. |
30
|
|
|
* |
31
|
|
|
* @var ManagerInterface |
32
|
|
|
*/ |
33
|
|
|
private $authManager; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Initialize. |
37
|
|
|
*/ |
38
|
|
|
public function init() |
39
|
|
|
{ |
40
|
|
|
if (null === $this->authManager){ |
41
|
|
|
throw new InvalidConfigException('The authManager is not defined.'); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritdoc |
47
|
|
|
*/ |
48
|
|
|
public function rules() |
49
|
|
|
{ |
50
|
|
|
return [ |
51
|
|
|
[ |
52
|
|
|
'roles', |
53
|
|
|
'required', |
54
|
|
|
], |
55
|
|
|
[ |
56
|
|
|
'roles', |
57
|
|
|
'validateRoles', |
58
|
|
|
], |
59
|
|
|
]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* List if attributes. |
64
|
|
|
* |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
|
|
public function attributes() |
68
|
|
|
{ |
69
|
|
|
return [ |
70
|
|
|
'roles', |
71
|
|
|
]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* List if attribute labels. |
76
|
|
|
* |
77
|
|
|
* @inheritdoc |
78
|
|
|
*/ |
79
|
|
|
public function attributeLabels() |
80
|
|
|
{ |
81
|
|
|
return [ |
82
|
|
|
'roles' => 'Roles', |
83
|
|
|
]; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get user name. |
88
|
|
|
* |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
public function getUserName(): string |
92
|
|
|
{ |
93
|
|
|
return $this->profileModel->getUserName(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Set new roles values. |
98
|
|
|
* |
99
|
|
|
* @param mixed $roles |
100
|
|
|
*/ |
101
|
|
|
public function setRoles($roles) |
102
|
|
|
{ |
103
|
|
|
$this->roles = $roles; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* List of profile assigned roles. |
108
|
|
|
* |
109
|
|
|
* @return string[] |
110
|
|
|
*/ |
111
|
|
|
public function getRoles() |
112
|
|
|
{ |
113
|
|
|
return array_keys($this->profileModel->getRoles()); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Set profile (user) model. |
118
|
|
|
* |
119
|
|
|
* @param RbacIdentityInterface $model. |
|
|
|
|
120
|
|
|
* |
121
|
|
|
* @throws InvalidConfigException |
122
|
|
|
* |
123
|
|
|
* @return void |
124
|
|
|
*/ |
125
|
|
|
public function setProfileModel(RbacIdentityInterface $model): void |
126
|
|
|
{ |
127
|
|
|
$this->profileModel = $model; |
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Returns profile (user) model. |
132
|
|
|
* |
133
|
|
|
* @return RbacIdentityInterface |
134
|
|
|
*/ |
135
|
|
|
public function getProfileModel(): RbacIdentityInterface |
136
|
|
|
{ |
137
|
|
|
return $this->profileModel; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Set auth manager. |
142
|
|
|
* |
143
|
|
|
* @param ManagerInterface $authManager |
144
|
|
|
*/ |
145
|
|
|
public function setAuthManager(ManagerInterface $authManager) |
146
|
|
|
{ |
147
|
|
|
$this->authManager = $authManager; |
|
|
|
|
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Get auth manager. |
152
|
|
|
* |
153
|
|
|
* @return ManagerInterface |
154
|
|
|
*/ |
155
|
|
|
public function getAuthManager(): ManagerInterface |
156
|
|
|
{ |
157
|
|
|
return $this->authManager; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Validate roles data format. |
162
|
|
|
* |
163
|
|
|
* @param $attribute |
164
|
|
|
* |
165
|
|
|
* @return bool |
166
|
|
|
*/ |
167
|
|
|
public function validateRoles($attribute): bool |
168
|
|
|
{ |
169
|
|
|
if (!is_array($this->roles)){ |
170
|
|
|
$this->addError($attribute, 'Incorrect roles data format.'); |
171
|
|
|
return false; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return true; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Save user data. |
179
|
|
|
* |
180
|
|
|
* @return bool |
181
|
|
|
*/ |
182
|
|
|
public function save(): bool |
183
|
|
|
{ |
184
|
|
|
if (!$this->validate()){ |
185
|
|
|
return false; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
$this->assignRoles(); |
189
|
|
|
|
190
|
|
|
return true; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Returns current model id. |
195
|
|
|
* |
196
|
|
|
* @return int |
197
|
|
|
*/ |
198
|
|
|
public function getId(): int |
199
|
|
|
{ |
200
|
|
|
return $this->profileModel->getId(); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Assign roles. |
205
|
|
|
* |
206
|
|
|
* @return void |
207
|
|
|
*/ |
208
|
|
|
private function assignRoles(): void |
209
|
|
|
{ |
210
|
|
|
if (!$this->profileModel->getIsNewRecord()){ |
211
|
|
|
$this->authManager->revokeAll( |
212
|
|
|
$this->profileModel->getId() |
213
|
|
|
); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
foreach ($this->roles as $role){ |
217
|
|
|
$roleObject = $this->authManager->getRole($role); |
218
|
|
|
|
219
|
|
|
if (null === $roleObject){ |
220
|
|
|
continue; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
$this->authManager->assign($roleObject, $this->profileModel->getId()); |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.