1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* HiPanel core package |
5
|
|
|
* |
6
|
|
|
* @link https://hipanel.com/ |
7
|
|
|
* @package hipanel-core |
8
|
|
|
* @license BSD-3-Clause |
9
|
|
|
* @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace hipanel\models; |
13
|
|
|
|
14
|
|
|
use Yii; |
15
|
|
|
use yii\base\Model; |
16
|
|
|
use yii\base\NotSupportedException; |
17
|
|
|
use yii\web\IdentityInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* User model. |
21
|
|
|
* |
22
|
|
|
* @property integer $id |
23
|
|
|
* @property string $username |
24
|
|
|
* @property string $password_hash |
25
|
|
|
* @property string $password_reset_token |
26
|
|
|
* @property string $email |
27
|
|
|
* @property string $auth_key |
28
|
|
|
* @property integer $role |
29
|
|
|
* @property integer $status |
30
|
|
|
* @property integer $created_at |
31
|
|
|
* @property integer $updated_at |
32
|
|
|
* @property string $password write-only password |
33
|
|
|
*/ |
34
|
|
|
class User extends Model implements IdentityInterface |
35
|
|
|
{ |
36
|
|
|
public $id; |
37
|
|
|
public $name; |
38
|
|
|
public $email; |
39
|
|
|
public $username; |
40
|
|
|
public $type; |
41
|
|
|
public $seller; |
42
|
|
|
public $seller_id; |
43
|
|
|
|
44
|
|
|
public $auth_key; |
45
|
|
|
public $password_hash; |
46
|
|
|
|
47
|
|
|
private static $_users = []; |
48
|
|
|
|
49
|
|
|
public function save() |
50
|
|
|
{ |
51
|
|
|
static::$_users[$this->id] = $this; |
|
|
|
|
52
|
|
|
Yii::$app->session->set('identity:' . $this->id, $this); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public static function findOne($id) |
56
|
|
|
{ |
57
|
|
|
if (isset(static::$_users[$id])) { |
|
|
|
|
58
|
|
|
return static::$_users[$id]; |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return Yii::$app->session->get('identity:' . $id); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
|
|
public function rules() |
68
|
|
|
{ |
69
|
|
|
return [ |
70
|
|
|
['status', 'default', 'value' => self::STATUS_ACTIVE], |
71
|
|
|
['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]], |
72
|
|
|
|
73
|
|
|
['role', 'default', 'value' => self::ROLE_USER], |
74
|
|
|
['role', 'in', 'range' => [self::ROLE_USER]], |
75
|
|
|
]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** {@inheritdoc} */ |
79
|
|
|
public static function findIdentity($id) |
80
|
|
|
{ |
81
|
|
|
return static::findOne($id); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** {@inheritdoc} */ |
85
|
|
|
public function getAccessToken() |
86
|
|
|
{ |
87
|
|
|
$client = Yii::$app->authClientCollection->getClient(); |
88
|
|
|
if ($client->getAccessToken() === null) { |
89
|
|
|
return null; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $client->getAccessToken()->getParam('access_token'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
|
|
public static function findIdentityByAccessToken($token, $type = null) |
99
|
|
|
{ |
100
|
|
|
throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Finds user by username. |
105
|
|
|
* |
106
|
|
|
* @param string $username |
107
|
|
|
* @return static|null |
108
|
|
|
*/ |
109
|
|
|
public static function findByUsername($username) |
110
|
|
|
{ |
111
|
|
|
return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Finds user by password reset token. |
116
|
|
|
* |
117
|
|
|
* @param string $token password reset token |
118
|
|
|
* @return static|null |
119
|
|
|
*/ |
120
|
|
|
public static function findByPasswordResetToken($token) |
121
|
|
|
{ |
122
|
|
|
if (!static::isPasswordResetTokenValid($token)) { |
123
|
|
|
return null; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return static::findOne([ |
127
|
|
|
'password_reset_token' => $token, |
128
|
|
|
'status' => self::STATUS_ACTIVE, |
129
|
|
|
]); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Finds out if password reset token is valid. |
134
|
|
|
* |
135
|
|
|
* @param string $token password reset token |
136
|
|
|
* @return boolean |
137
|
|
|
*/ |
138
|
|
|
public static function isPasswordResetTokenValid($token) |
139
|
|
|
{ |
140
|
|
|
if (empty($token)) { |
141
|
|
|
return false; |
142
|
|
|
} |
143
|
|
|
$expire = Yii::$app->params['user.passwordResetTokenExpire']; |
144
|
|
|
$parts = explode('_', $token); |
145
|
|
|
$timestamp = (int) end($parts); |
146
|
|
|
return $timestamp + $expire >= time(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* {@inheritdoc} |
151
|
|
|
*/ |
152
|
|
|
public function getId() |
153
|
|
|
{ |
154
|
|
|
return $this->id; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function is($key) |
158
|
|
|
{ |
159
|
|
|
return (int)$this->id === (int)$key || (string)$this->username === (string)$key; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function not($key) |
163
|
|
|
{ |
164
|
|
|
return (int)$this->id !== (int)$key && (string)$this->username !== (string)$key; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* {@inheritdoc} |
169
|
|
|
*/ |
170
|
|
|
public function getAuthKey() |
171
|
|
|
{ |
172
|
|
|
return 'DUMMY'; |
173
|
|
|
//return $this->auth_key; |
|
|
|
|
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* {@inheritdoc} |
178
|
|
|
*/ |
179
|
|
|
public function validateAuthKey($authKey) |
180
|
|
|
{ |
181
|
|
|
return $this->getAuthKey() === $authKey; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Validates password. |
186
|
|
|
* |
187
|
|
|
* @param string $password password to validate |
188
|
|
|
* @return boolean if password provided is valid for current user |
189
|
|
|
*/ |
190
|
|
|
public function validatePassword($password) |
191
|
|
|
{ |
192
|
|
|
return Yii::$app->security->validatePassword($password, $this->password_hash); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Generates password hash from password and sets it to the model. |
197
|
|
|
* |
198
|
|
|
* @param string $password |
199
|
|
|
*/ |
200
|
|
|
public function setPassword($password) |
201
|
|
|
{ |
202
|
|
|
$this->password_hash = Yii::$app->security->generatePasswordHash($password); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Generates "remember me" authentication key. |
207
|
|
|
*/ |
208
|
|
|
public function generateAuthKey() |
209
|
|
|
{ |
210
|
|
|
$this->auth_key = 'DUMMY'; |
211
|
|
|
//$this->auth_key = Yii::$app->security->generateRandomString(); |
|
|
|
|
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Generates new password reset token. |
216
|
|
|
*/ |
217
|
|
|
public function generatePasswordResetToken() |
218
|
|
|
{ |
219
|
|
|
$this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time(); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Removes password reset token. |
224
|
|
|
*/ |
225
|
|
|
public function removePasswordResetToken() |
226
|
|
|
{ |
227
|
|
|
$this->password_reset_token = null; |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: