|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\models; |
|
4
|
|
|
|
|
5
|
|
|
use OAuth2\Storage\UserCredentialsInterface; |
|
6
|
|
|
use roaresearch\yii2\oauth2server\models\OauthAccessTokens as AccessToken; |
|
7
|
|
|
use Yii; |
|
8
|
|
|
use yii\web\IdentityInterface; |
|
9
|
|
|
|
|
10
|
|
|
class User extends \yii\db\ActiveRecord implements UserCredentialsInterface, IdentityInterface |
|
11
|
|
|
{ |
|
12
|
|
|
public static function tableName() |
|
13
|
|
|
{ |
|
14
|
|
|
return '{{%user}}'; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @inheritdoc |
|
19
|
|
|
*/ |
|
20
|
|
|
public static function findIdentity($id) |
|
21
|
|
|
{ |
|
22
|
|
|
return static::findOne(['id' => $id]); |
|
|
|
|
|
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @inheritdoc |
|
27
|
|
|
*/ |
|
28
|
|
|
public static function findIdentityByAccessToken($token, $type = null) |
|
29
|
|
|
{ |
|
30
|
|
|
return static::find()->joinWith('accessTokens', false) |
|
|
|
|
|
|
31
|
|
|
->andWhere(['access_token' => $token]) |
|
32
|
|
|
->one(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Finds user by username |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $username |
|
39
|
|
|
* @return static|null |
|
40
|
|
|
*/ |
|
41
|
|
|
public static function findByUsername($username) |
|
42
|
|
|
{ |
|
43
|
|
|
return static::findOne(['username' => $username]); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Finds user by password reset token |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $token password reset token |
|
50
|
|
|
* @return static|null |
|
51
|
|
|
*/ |
|
52
|
|
|
public static function findByPasswordResetToken($token) |
|
53
|
|
|
{ |
|
54
|
|
|
if (!static::isPasswordResetTokenValid($token)) { |
|
55
|
|
|
return null; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return static::findOne([ |
|
59
|
|
|
'password_reset_token' => $token, |
|
60
|
|
|
'status' => self::STATUS_ACTIVE, |
|
|
|
|
|
|
61
|
|
|
]); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Finds out if password reset token is valid |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $token password reset token |
|
68
|
|
|
* @return bool |
|
69
|
|
|
*/ |
|
70
|
|
|
public static function isPasswordResetTokenValid($token) |
|
71
|
|
|
{ |
|
72
|
|
|
if (empty($token)) { |
|
73
|
|
|
return false; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$timestamp = (int)substr($token, strrpos($token, '_') + 1); |
|
77
|
|
|
$expire = Yii::$app->params['user.passwordResetTokenExpire']; |
|
78
|
|
|
|
|
79
|
|
|
return $timestamp + $expire >= time(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @inheritdoc |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getId() |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->getPrimaryKey(); |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @inheritdoc |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getAuthKey() |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->auth_key; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @inheritdoc |
|
100
|
|
|
*/ |
|
101
|
|
|
public function validateAuthKey($authKey) |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->getAuthKey() === $authKey; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Validates password |
|
108
|
|
|
* |
|
109
|
|
|
* @param string $password password to validate |
|
110
|
|
|
* @return bool if password provided is valid for current user |
|
111
|
|
|
*/ |
|
112
|
|
|
public function validatePassword($password) |
|
113
|
|
|
{ |
|
114
|
|
|
return Yii::$app->security->validatePassword($password, $this->password_hash); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Generates password hash from password and sets it to the model |
|
119
|
|
|
* |
|
120
|
|
|
* @param string $password |
|
121
|
|
|
*/ |
|
122
|
|
|
public function setPassword($password) |
|
123
|
|
|
{ |
|
124
|
|
|
$this->password_hash = Yii::$app->security->generatePasswordHash($password); |
|
|
|
|
|
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Generates "remember me" authentication key |
|
129
|
|
|
*/ |
|
130
|
|
|
public function generateAuthKey() |
|
131
|
|
|
{ |
|
132
|
|
|
$this->auth_key = Yii::$app->security->generateRandomString(); |
|
|
|
|
|
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Generates new password reset token |
|
137
|
|
|
*/ |
|
138
|
|
|
public function generatePasswordResetToken() |
|
139
|
|
|
{ |
|
140
|
|
|
$this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time(); |
|
|
|
|
|
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Removes password reset token |
|
145
|
|
|
*/ |
|
146
|
|
|
public function removePasswordResetToken() |
|
147
|
|
|
{ |
|
148
|
|
|
$this->password_reset_token = null; |
|
|
|
|
|
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @inheritdoc |
|
153
|
|
|
*/ |
|
154
|
|
|
public function checkUserCredentials($username, $password) |
|
155
|
|
|
{ |
|
156
|
|
|
$user = static::findByUsername($username); |
|
157
|
|
|
if (empty($user)) { |
|
158
|
|
|
return false; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
return $user->validatePassword($password); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @inheritdoc |
|
166
|
|
|
*/ |
|
167
|
|
|
public function getUserDetails($username = null) |
|
168
|
|
|
{ |
|
169
|
|
|
$user = $username |
|
170
|
|
|
? static::findByUsername($username) |
|
171
|
|
|
: $this; |
|
172
|
|
|
|
|
173
|
|
|
return ['user_id' => $user->id]; |
|
|
|
|
|
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @return \yii\db\ActiveQuery |
|
178
|
|
|
*/ |
|
179
|
|
|
public function getAccessTokens() |
|
180
|
|
|
{ |
|
181
|
|
|
return $this->hasMany(AccessToken::class, ['user_id' => 'id']) |
|
182
|
|
|
->andOnCondition(['client_id' => 'testclient']); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
|
|
186
|
|
|
public function rules() |
|
187
|
|
|
{ |
|
188
|
|
|
return [ |
|
189
|
|
|
[['username'], 'string'], |
|
190
|
|
|
]; |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: