|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace zacksleo\yii2\backend\models; |
|
4
|
|
|
|
|
5
|
|
|
use yii; |
|
6
|
|
|
use yii\base\NotSupportedException; |
|
7
|
|
|
use yii\db\ActiveRecord; |
|
8
|
|
|
use yii\web\IdentityInterface; |
|
9
|
|
|
use yii\web\UploadedFile; |
|
10
|
|
|
use yii\helpers\Url; |
|
11
|
|
|
use yii\behaviors\TimestampBehavior; |
|
12
|
|
|
use zacksleo\yii2\backend\models\queries\AdminQuery; |
|
13
|
|
|
use zacksleo\yii2\backend\Module; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* User model |
|
17
|
|
|
* |
|
18
|
|
|
* @property integer $id |
|
19
|
|
|
* @property string $username |
|
20
|
|
|
* @property string $email |
|
21
|
|
|
* @property integer $role |
|
22
|
|
|
* @property integer $status |
|
23
|
|
|
* @property string $created_at |
|
24
|
|
|
* @property string $updated_at |
|
25
|
|
|
* @property string $password_hash write-only password |
|
26
|
|
|
* @property string $avatar |
|
27
|
|
|
* @property string $auth_key |
|
28
|
|
|
* @property string $name |
|
29
|
|
|
* @property string $password_rest_token |
|
30
|
|
|
* @property string $email_confirmation_token |
|
31
|
|
|
* @property UploadedFile $imageFile |
|
32
|
|
|
*/ |
|
33
|
|
|
class Admin extends ActiveRecord implements IdentityInterface |
|
34
|
|
|
{ |
|
35
|
|
|
const STATUS_INACTIVE = 0; |
|
36
|
|
|
const STATUS_ACTIVE = 10; |
|
37
|
|
|
|
|
38
|
|
|
public $imageFile; |
|
39
|
|
|
|
|
40
|
|
|
public $isAdmin = true; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var string|null the current password value from form input |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $_password; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @inheritdoc |
|
49
|
32 |
|
*/ |
|
50
|
|
|
public static function tableName() |
|
51
|
32 |
|
{ |
|
52
|
|
|
return '{{%admin}}'; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return AdminQuery custom query class with user scopes |
|
57
|
30 |
|
*/ |
|
58
|
|
|
public static function find() |
|
59
|
30 |
|
{ |
|
60
|
|
|
return new AdminQuery(get_called_class()); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @inheritdoc |
|
65
|
14 |
|
*/ |
|
66
|
|
|
public function scenarios() |
|
67
|
14 |
|
{ |
|
68
|
14 |
|
return array_merge(parent::scenarios(), [ |
|
69
|
|
|
'reset' => ['username', 'email', 'password_hash'], |
|
70
|
|
|
]); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @inheritdoc |
|
75
|
14 |
|
*/ |
|
76
|
|
|
public function rules() |
|
77
|
|
|
{ |
|
78
|
14 |
|
return [ |
|
79
|
|
|
[['username', 'email', 'mobilephone', 'password_hash'], 'required', 'on' => 'reset'], |
|
80
|
|
|
['username', 'match', 'pattern' => '/^[a-z]\w*$/i', 'on' => 'reset'], |
|
81
|
|
|
['password_hash', 'match', 'pattern' => '/\d/', 'message' => '密码至少包含一位数字', 'on' => 'reset'], |
|
82
|
|
|
['password_hash', 'match', 'pattern' => '/[a-zA-Z]/', 'message' => '密码至少包含一个字母', 'on' => 'reset'], |
|
83
|
14 |
|
|
|
84
|
14 |
|
['status', 'default', 'value' => self::STATUS_ACTIVE], |
|
85
|
|
|
['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_INACTIVE]], |
|
86
|
|
|
|
|
87
|
|
|
['username', 'filter', 'filter' => 'trim'], |
|
88
|
|
|
['username', 'unique', 'on' => 'reset'], |
|
89
|
|
|
['username', 'string', 'min' => 2, 'max' => 255], |
|
90
|
|
|
['name', 'string', 'max' => '20'], |
|
91
|
|
|
|
|
92
|
|
|
['email', 'filter', 'filter' => 'trim'], |
|
93
|
|
|
['email', 'email'], |
|
94
|
|
|
['email', 'unique', 'on' => 'reset'], |
|
95
|
|
|
['avatar', 'safe'], |
|
96
|
|
|
[ |
|
97
|
|
|
['imageFile'], |
|
98
|
|
|
'file', |
|
99
|
|
|
'skipOnEmpty' => true, |
|
100
|
|
|
'extensions' => 'png, jpg', |
|
101
|
|
|
'maxFiles' => 1, |
|
102
|
|
|
'maxSize' => 300000 |
|
103
|
|
|
], |
|
104
|
|
|
]; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @inheritdoc |
|
109
|
2 |
|
*/ |
|
110
|
|
|
public function attributeLabels() |
|
111
|
|
|
{ |
|
112
|
2 |
|
return [ |
|
113
|
2 |
|
'avatar' => Module::t('backend', 'avatar'), |
|
114
|
2 |
|
'id' => Module::t('backend', 'id'), |
|
115
|
2 |
|
'username' => Module::t('backend', 'username'), |
|
116
|
2 |
|
'email' => Module::t('backend', 'email'), |
|
117
|
2 |
|
'is_email_verified' => Module::t('backend', 'is email verified'), |
|
118
|
2 |
|
'password_hash' => Module::t('backend', 'password'), |
|
119
|
2 |
|
'status' => Module::t('backend', 'status'), |
|
120
|
2 |
|
'created_at' => Module::t('backend', 'create time'), |
|
121
|
2 |
|
'updated_at' => Module::t('backend', 'update time'), |
|
122
|
2 |
|
'imageFile' => '上传头像', |
|
123
|
|
|
'name' => '姓名' |
|
124
|
|
|
]; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @inheritdoc |
|
129
|
33 |
|
*/ |
|
130
|
|
|
public function behaviors() |
|
131
|
|
|
{ |
|
132
|
|
|
return [ |
|
133
|
33 |
|
'timestamp' => [ |
|
134
|
|
|
'class' => TimestampBehavior::className(), |
|
135
|
|
|
], |
|
136
|
|
|
]; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @inheritdoc |
|
141
|
13 |
|
*/ |
|
142
|
|
|
public function beforeSave($insert) |
|
|
|
|
|
|
143
|
13 |
|
{ |
|
144
|
1 |
|
if ($this->isNewRecord) { |
|
145
|
|
|
$this->setPassword($this->password_hash); |
|
146
|
13 |
|
} |
|
147
|
|
|
if (Yii::$app->request->isPost && isset($_POST['Admin']['imageFile'])) { |
|
148
|
|
|
$this->imageFile = UploadedFile::getInstance($this, 'imageFile'); |
|
149
|
|
|
if (!empty($this->imageFile) && !$this->upload()) { |
|
150
|
|
|
$this->addError('avatar', $this->imageFile->getHasError()); |
|
|
|
|
|
|
151
|
|
|
return false; |
|
152
|
|
|
} |
|
153
|
13 |
|
} |
|
154
|
|
|
return parent::beforeSave($insert); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @inheritdoc |
|
159
|
5 |
|
*/ |
|
160
|
|
|
public static function findIdentity($id) |
|
161
|
5 |
|
{ |
|
162
|
|
|
return static::findOne($id); |
|
|
|
|
|
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @inheritdoc |
|
167
|
1 |
|
*/ |
|
168
|
|
|
public static function findIdentityByAccessToken($token, $type = null) |
|
169
|
1 |
|
{ |
|
170
|
|
|
throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.'); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @inheritdoc |
|
175
|
18 |
|
*/ |
|
176
|
|
|
public function getId() |
|
177
|
18 |
|
{ |
|
178
|
|
|
return $this->getPrimaryKey(); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* @inheritdoc |
|
183
|
1 |
|
*/ |
|
184
|
|
|
public function getAuthKey() |
|
185
|
1 |
|
{ |
|
186
|
|
|
return $this->auth_key; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* @inheritdoc |
|
191
|
1 |
|
*/ |
|
192
|
|
|
public function validateAuthKey($authKey) |
|
193
|
1 |
|
{ |
|
194
|
|
|
return $this->getAuthKey() === $authKey; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Validates password |
|
199
|
|
|
* |
|
200
|
|
|
* @param string $password password to validate |
|
201
|
|
|
* @return boolean if password provided is valid for current user |
|
202
|
7 |
|
*/ |
|
203
|
|
|
public function validatePassword($password) |
|
204
|
7 |
|
{ |
|
205
|
|
|
return Yii::$app->security->validatePassword($password, $this->password_hash); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Generates password hash from password and sets it to the model |
|
210
|
|
|
* |
|
211
|
|
|
* @param string $password |
|
212
|
6 |
|
*/ |
|
213
|
|
|
public function setPassword($password) |
|
214
|
6 |
|
{ |
|
215
|
6 |
|
$this->_password = $password; |
|
216
|
5 |
|
if (!empty($password)) { |
|
217
|
|
|
$this->password_hash = Yii::$app->security->generatePasswordHash($password); |
|
218
|
6 |
|
} |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* @return string|null the current password value, if set from form. Null otherwise. |
|
223
|
|
|
*/ |
|
224
|
|
|
public function getPassword() |
|
225
|
|
|
{ |
|
226
|
|
|
return $this->_password; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Generates "remember me" authentication key |
|
231
|
1 |
|
*/ |
|
232
|
|
|
public function generateAuthKey() |
|
233
|
1 |
|
{ |
|
234
|
1 |
|
$this->auth_key = Yii::$app->security->generateRandomString(); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Generates new email confirmation token |
|
239
|
|
|
* @param bool $save whether to save the record. Default is `false`. |
|
240
|
|
|
* @return bool|null whether the save was successful or null if $save was false. |
|
241
|
|
|
*/ |
|
242
|
|
|
public function generateEmailConfirmationToken($save = false) |
|
243
|
|
|
{ |
|
244
|
|
|
$this->email_confirmation_token = Yii::$app->security->generateRandomString() . '_' . time(); |
|
245
|
|
|
if ($save) { |
|
246
|
|
|
return $this->save(); |
|
247
|
|
|
} |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Generates new password reset token |
|
252
|
|
|
* @param bool $save whether to save the record. Default is `false`. |
|
253
|
|
|
* @return bool|null whether the save was successful or null if $save was false. |
|
254
|
5 |
|
*/ |
|
255
|
|
|
public function generatePasswordResetToken($save = false) |
|
256
|
5 |
|
{ |
|
257
|
5 |
|
$this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time(); |
|
|
|
|
|
|
258
|
5 |
|
if ($save) { |
|
259
|
|
|
return $this->save(); |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* Resets to a new password and deletes the password reset token. |
|
265
|
|
|
* @param string $password the new password for this user. |
|
266
|
|
|
* @return bool whether the record was updated successfully |
|
267
|
5 |
|
*/ |
|
268
|
|
|
public function resetPassword($password) |
|
269
|
5 |
|
{ |
|
270
|
5 |
|
$this->setPassword($password); |
|
271
|
5 |
|
$this->password_reset_token = null; |
|
|
|
|
|
|
272
|
|
|
return $this->save(); |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
public static function getStatusList() |
|
276
|
|
|
{ |
|
277
|
|
|
return [ |
|
278
|
2 |
|
self::STATUS_ACTIVE => '激活', |
|
279
|
|
|
self::STATUS_INACTIVE => '禁用', |
|
280
|
2 |
|
]; |
|
281
|
2 |
|
} |
|
282
|
2 |
|
|
|
283
|
2 |
|
/** |
|
284
|
1 |
|
* 上传头像 |
|
285
|
|
|
* @return bool |
|
286
|
2 |
|
*/ |
|
287
|
2 |
|
public function upload() |
|
288
|
2 |
|
{ |
|
289
|
2 |
|
if ($this->validate()) { |
|
290
|
|
|
$path = date('Ymd') . '/'; |
|
291
|
1 |
|
$fullpath = \Yii::getAlias('@app') . '/web/uploads/' . $path; |
|
292
|
|
|
if (!file_exists($fullpath)) { |
|
293
|
|
|
mkdir($fullpath); |
|
294
|
|
|
} |
|
295
|
|
|
$filename = uniqid() . '.' . $this->imageFile->extension; |
|
296
|
|
|
$this->imageFile->saveAs($fullpath . $filename); |
|
297
|
|
|
$this->avatar = $path . $filename; |
|
298
|
|
|
return true; |
|
299
|
1 |
|
} else { |
|
300
|
|
|
return false; |
|
301
|
1 |
|
} |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
1 |
|
/** |
|
305
|
|
|
* 获取头像地址 |
|
306
|
|
|
* @return string |
|
307
|
|
|
*/ |
|
308
|
|
|
public function getImageUrl() |
|
309
|
|
|
{ |
|
310
|
|
|
if (empty($this->avatar)) { |
|
311
|
|
|
return Url::to('/images/avatar.jpg'); |
|
312
|
|
|
} |
|
313
|
|
|
return Url::to('@web/uploads/' . $this->avatar, true); |
|
314
|
|
|
} |
|
315
|
|
|
} |
|
316
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: