1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace modules\users\models; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\helpers\ArrayHelper; |
7
|
|
|
use yii\helpers\Html; |
8
|
|
|
use modules\users\traits\ModuleTrait; |
9
|
|
|
use modules\users\Module; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class User |
13
|
|
|
* @package modules\users\models |
14
|
|
|
* |
15
|
|
|
* @property array statusesArray Array statuses |
16
|
|
|
* @property string userFullName Full user name |
17
|
|
|
*/ |
18
|
|
|
class User extends BaseUser |
19
|
|
|
{ |
20
|
|
|
use ModuleTrait; |
21
|
|
|
|
22
|
|
|
const LENGTH_STRING_PASSWORD_MIN = 6; |
23
|
|
|
const LENGTH_STRING_PASSWORD_MAX = 16; |
24
|
|
|
|
25
|
|
|
const SCENARIO_ADMIN_CREATE = 'adminCreate'; |
26
|
|
|
const SCENARIO_ADMIN_UPDATE = 'adminUpdate'; |
27
|
|
|
const SCENARIO_ADMIN_PASSWORD_UPDATE = 'adminPasswordUpdate'; |
28
|
|
|
const SCENARIO_PROFILE_UPDATE = 'profileUpdate'; |
29
|
|
|
const SCENARIO_PASSWORD_UPDATE = 'passwordUpdate'; |
30
|
|
|
const SCENARIO_PROFILE_DELETE = 'profileDelete'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
public $currentPassword; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
public $newPassword; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
public $newPasswordRepeat; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
|
|
public function rules() |
51
|
|
|
{ |
52
|
|
|
return ArrayHelper::merge(parent::rules(), [ |
53
|
|
|
['status', 'integer'], |
54
|
|
|
['status', 'default', 'value' => self::STATUS_WAIT], |
55
|
|
|
['status', 'in', 'range' => array_keys(self::getStatusesArray())], |
56
|
|
|
|
57
|
|
|
[['newPassword', 'newPasswordRepeat'], 'required', 'on' => [self::SCENARIO_ADMIN_CREATE, self::SCENARIO_PASSWORD_UPDATE, self::SCENARIO_ADMIN_PASSWORD_UPDATE]], |
58
|
|
|
['newPassword', 'string', 'min' => self::LENGTH_STRING_PASSWORD_MIN], |
59
|
|
|
['newPasswordRepeat', 'compare', 'compareAttribute' => 'newPassword'], |
60
|
|
|
['currentPassword', 'validateCurrentPassword', 'skipOnEmpty' => false, 'skipOnError' => false], |
61
|
|
|
]); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $attribute |
66
|
|
|
*/ |
67
|
|
|
public function validateCurrentPassword($attribute) |
68
|
|
|
{ |
69
|
|
|
if (!empty($this->newPassword) && !empty($this->newPasswordRepeat) && !$this->hasErrors()) { |
70
|
|
|
if ($attribute) { |
71
|
|
|
if (!$this->validatePassword($this->$attribute)) |
72
|
|
|
$this->addError($attribute, Module::t('module', 'Incorrect current password.')); |
73
|
|
|
} else { |
74
|
|
|
$this->addError($attribute, Module::t('module', 'Enter your current password.')); |
75
|
|
|
} |
76
|
|
|
} else { |
77
|
|
|
$this->addError($attribute, Module::t('module', 'Not all fields are filled in correctly.')); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
|
public function scenarios() |
85
|
|
|
{ |
86
|
|
|
$scenarios = parent::scenarios(); |
87
|
|
|
$scenarios[self::SCENARIO_ADMIN_CREATE] = ['avatar', 'username', 'email', 'status', 'newPassword', 'newPasswordRepeat', 'registration_type', 'first_name', 'last_name']; |
88
|
|
|
$scenarios[self::SCENARIO_ADMIN_UPDATE] = ['username', 'email', 'status', 'first_name', 'last_name']; |
89
|
|
|
$scenarios[self::SCENARIO_ADMIN_PASSWORD_UPDATE] = ['newPassword', 'newPasswordRepeat']; |
90
|
|
|
$scenarios[self::SCENARIO_PASSWORD_UPDATE] = ['currentPassword', 'newPassword', 'newPasswordRepeat']; |
91
|
|
|
$scenarios[self::SCENARIO_PROFILE_UPDATE] = ['email', 'first_name', 'last_name']; |
92
|
|
|
$scenarios[self::SCENARIO_PROFILE_DELETE] = ['status']; |
93
|
|
|
$scenarios['default'] = ['username', 'email', 'password_hash', 'status', 'auth_key', 'email_confirm_token']; |
94
|
|
|
return $scenarios; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
|
|
public function attributeLabels() |
101
|
|
|
{ |
102
|
|
|
return ArrayHelper::merge(parent::attributeLabels(), [ |
103
|
|
|
'userRoleName' => Module::t('module', 'Role'), |
104
|
|
|
'currentPassword' => Module::t('module', 'Current Password'), |
105
|
|
|
'newPassword' => Module::t('module', 'New Password'), |
106
|
|
|
'newPasswordRepeat' => Module::t('module', 'Repeat Password'), |
107
|
|
|
]); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Actions before saving |
112
|
|
|
* |
113
|
|
|
* @param bool $insert |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
|
|
public function beforeSave($insert) |
117
|
|
|
{ |
118
|
|
|
if (parent::beforeSave($insert)) { |
119
|
|
|
if (!empty($this->newPassword)) { |
120
|
|
|
$this->setPassword($this->newPassword); |
121
|
|
|
Yii::$app->session->setFlash('success', Module::t('module', 'Password changed successfully.')); |
122
|
|
|
} |
123
|
|
|
return true; |
124
|
|
|
} |
125
|
|
|
return false; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return array |
130
|
|
|
*/ |
131
|
|
|
public static function getStatusesArray() |
132
|
|
|
{ |
133
|
|
|
return [ |
134
|
|
|
self::STATUS_BLOCKED => Module::t('module', 'Blocked'), |
135
|
|
|
self::STATUS_ACTIVE => Module::t('module', 'Active'), |
136
|
|
|
self::STATUS_WAIT => Module::t('module', 'Wait'), |
137
|
|
|
self::STATUS_DELETED => Module::t('module', 'Deleted'), |
138
|
|
|
]; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Set Status |
143
|
|
|
*/ |
144
|
|
|
public function setStatus() |
145
|
|
|
{ |
146
|
|
|
if ($this->status == self::STATUS_ACTIVE) { |
147
|
|
|
$this->status = self::STATUS_BLOCKED; |
148
|
|
|
} else if ($this->status == self::STATUS_BLOCKED) { |
149
|
|
|
$this->status = self::STATUS_ACTIVE; |
150
|
|
|
} else if ($this->status == self::STATUS_WAIT) { |
151
|
|
|
$this->status = self::STATUS_ACTIVE; |
152
|
|
|
} else if ($this->status == self::STATUS_DELETED) { |
153
|
|
|
$this->status = self::STATUS_WAIT; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
|
|
public function getUserFullName() |
161
|
|
|
{ |
162
|
|
|
$fullName = ''; |
163
|
|
|
if (!Yii::$app->user->isGuest) { |
164
|
|
|
if ($fullName = $this->getFirstNameLastName()) { |
165
|
|
|
return Html::encode($fullName); |
166
|
|
|
} else if ($fullName = $this->getFirstName()) { |
167
|
|
|
return Html::encode($fullName); |
168
|
|
|
} else if ($fullName = $this->getLastName()) { |
169
|
|
|
return Html::encode($fullName); |
170
|
|
|
} else { |
171
|
|
|
$fullName = $this->username; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
return Html::encode($fullName); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return bool|string |
179
|
|
|
*/ |
180
|
|
|
protected function getFirstNameLastName() |
181
|
|
|
{ |
182
|
|
|
if (!empty($this->first_name) && !empty($this->last_name)) { |
183
|
|
|
return $this->first_name . ' ' . $this->last_name; |
184
|
|
|
} |
185
|
|
|
return false; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @return bool|string |
190
|
|
|
*/ |
191
|
|
|
protected function getFirstName() |
192
|
|
|
{ |
193
|
|
|
if (!empty($this->first_name)) { |
194
|
|
|
return $this->first_name; |
195
|
|
|
} |
196
|
|
|
return false; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @return bool|string |
201
|
|
|
*/ |
202
|
|
|
protected function getLastName() |
203
|
|
|
{ |
204
|
|
|
if (!empty($this->last_name)) { |
205
|
|
|
return $this->last_name; |
206
|
|
|
} |
207
|
|
|
return false; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param integer|string $id |
212
|
|
|
* @return bool |
213
|
|
|
*/ |
214
|
|
|
public function isSuperAdmin($id = '') |
215
|
|
|
{ |
216
|
|
|
$id = $id ? $id : $this->id; |
217
|
|
|
$authManager = Yii::$app->authManager; |
218
|
|
|
$roles = $authManager->getRolesByUser($id); |
219
|
|
|
foreach ($roles as $role) { |
220
|
|
|
if ($role->name == \modules\rbac\models\Role::ROLE_SUPER_ADMIN) |
221
|
|
|
return true; |
222
|
|
|
} |
223
|
|
|
return false; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @return bool |
228
|
|
|
*/ |
229
|
|
|
public function isDeleted() |
230
|
|
|
{ |
231
|
|
|
return $this->status === self::STATUS_DELETED; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @return mixed |
236
|
|
|
*/ |
237
|
|
|
public function getStatusName() |
238
|
|
|
{ |
239
|
|
|
return ArrayHelper::getValue(self::getStatusesArray(), $this->status); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Return <span class="label label-success">Active</span> |
244
|
|
|
* @return string |
245
|
|
|
*/ |
246
|
|
|
public function getStatusLabelName() |
247
|
|
|
{ |
248
|
|
|
$name = ArrayHelper::getValue(self::getLabelsArray(), $this->status); |
249
|
|
|
return Html::tag('span', $this->getStatusName(), ['class' => 'label label-' . $name]); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @return array |
254
|
|
|
*/ |
255
|
|
|
public static function getLabelsArray() |
256
|
|
|
{ |
257
|
|
|
return [ |
258
|
|
|
self::STATUS_BLOCKED => 'default', |
259
|
|
|
self::STATUS_ACTIVE => 'success', |
260
|
|
|
self::STATUS_WAIT => 'warning', |
261
|
|
|
self::STATUS_DELETED => 'danger', |
262
|
|
|
]; |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|