forked from mdmsoft/yii2-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUser.php
115 lines (97 loc) · 2.33 KB
/
User.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
namespace mdm\admin\models;
use yii\db\ActiveRecord;
use yii\helpers\Security;
use yii\web\IdentityInterface;
/**
* This is the model class for table "tbl_user".
*
* @property integer $id
* @property string $username
* @property string $password
* @property string $password_hash
* @property string $email
*/
class User extends ActiveRecord implements IdentityInterface
{
public $password;
public $retypePassword;
/**
* @inheritdoc
*/
public static function tableName()
{
return 'tbl_user';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['username', 'email'], 'filter', 'filter' => 'trim'],
[['username', 'email'], 'required'],
[['username','password'], 'string', 'min' => 6, 'max' => 32],
['email', 'filter', 'filter' => 'trim'],
['email', 'required'],
['email', 'email'],
['email', 'unique', 'message' => 'This email address has already been taken.', 'on' => 'signup'],
['email', 'exist', 'message' => 'There is no user with such email.', 'on' => 'requestPasswordResetToken'],
['password', 'required'],
['password', 'string', 'min' => 6],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'username' => 'Username',
'password' => 'Password',
'retypePassword' => 'Retype Password',
'email' => 'Email',
];
}
public function beforeSave($insert)
{
if(parent::beforeSave($insert)){
if (($this->isNewRecord || $this->getScenario() === 'resetPassword') && !empty($this->password)) {
$this->password_hash = Security::generatePasswordHash($this->password);
}
return true;
}
return false;
}
public function getRoles()
{
$roles = \Yii::$app->authManager->getItems($this->id);
return implode(', ', array_keys($roles));
}
// Inherited from IdentityInterface
public static function findIdentity($id)
{
return self::find($id);
}
public static function findByUsername($username)
{
return self::find(['username' => $username]);
}
public function getId()
{
return $this->id;
}
public function getAuthKey()
{
return md5('AUTHKEY#' . $this->password_hash);
}
public function validateAuthKey($authKey)
{
return md5('AUTHKEY#' . $this->password_hash) === $authKey;
}
public function validatePassword($password)
{
return Security::validatePassword($password, $this->password_hash);
}
}