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