|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\models; |
|
4
|
|
|
|
|
5
|
|
|
use OAuth2\Storage\UserCredentialsInterface; |
|
6
|
|
|
use roaresearch\yii2\oauth2server\{ |
|
7
|
|
|
RevokeAccessTokenInterface, |
|
8
|
|
|
RevokeAccessTokenTrait |
|
|
|
|
|
|
9
|
|
|
}; |
|
10
|
|
|
use Yii; |
|
11
|
|
|
use yii\web\IdentityInterface; |
|
12
|
|
|
|
|
13
|
|
|
class User extends \yii\db\ActiveRecord implements |
|
14
|
|
|
UserCredentialsInterface, |
|
15
|
|
|
IdentityInterface, |
|
16
|
|
|
RevokeAccessTokenInterface |
|
17
|
|
|
{ |
|
18
|
|
|
use RevokeAccessTokenTrait; |
|
19
|
|
|
|
|
20
|
|
|
public static function tableName() |
|
21
|
|
|
{ |
|
22
|
|
|
return '{{%user}}'; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @inheritdoc |
|
27
|
|
|
*/ |
|
28
|
|
|
public static function findIdentity($id) |
|
29
|
|
|
{ |
|
30
|
|
|
return static::findOne(['id' => $id]); |
|
|
|
|
|
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Finds user by username |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $username |
|
37
|
|
|
* @return static|null |
|
38
|
|
|
*/ |
|
39
|
|
|
public static function findByUsername($username) |
|
40
|
|
|
{ |
|
41
|
|
|
return static::findOne(['username' => $username]); |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Finds user by password reset token |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $token password reset token |
|
48
|
|
|
* @return static|null |
|
49
|
|
|
*/ |
|
50
|
|
|
public static function findByPasswordResetToken($token) |
|
51
|
|
|
{ |
|
52
|
|
|
if (!static::isPasswordResetTokenValid($token)) { |
|
53
|
|
|
return null; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return static::findOne([ |
|
|
|
|
|
|
57
|
|
|
'password_reset_token' => $token, |
|
58
|
|
|
'status' => self::STATUS_ACTIVE, |
|
59
|
|
|
]); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Finds out if password reset token is valid |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $token password reset token |
|
66
|
|
|
* @return bool |
|
67
|
|
|
*/ |
|
68
|
|
|
public static function isPasswordResetTokenValid($token) |
|
69
|
|
|
{ |
|
70
|
|
|
if (empty($token)) { |
|
71
|
|
|
return false; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$timestamp = (int)substr($token, strrpos($token, '_') + 1); |
|
75
|
|
|
$expire = Yii::$app->params['user.passwordResetTokenExpire']; |
|
76
|
|
|
|
|
77
|
|
|
return $timestamp + $expire >= time(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @inheritdoc |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getId() |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->getPrimaryKey(); |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @inheritdoc |
|
90
|
|
|
*/ |
|
91
|
|
|
public function getAuthKey() |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->auth_key; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @inheritdoc |
|
98
|
|
|
*/ |
|
99
|
|
|
public function validateAuthKey($authKey) |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->getAuthKey() === $authKey; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Validates password |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $password password to validate |
|
108
|
|
|
* @return bool if password provided is valid for current user |
|
109
|
|
|
*/ |
|
110
|
|
|
public function validatePassword($password) |
|
111
|
|
|
{ |
|
112
|
|
|
return Yii::$app->security->validatePassword($password, $this->password_hash); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Generates password hash from password and sets it to the model |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $password |
|
119
|
|
|
*/ |
|
120
|
|
|
public function setPassword($password) |
|
121
|
|
|
{ |
|
122
|
|
|
$this->password_hash = Yii::$app->security->generatePasswordHash($password); |
|
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Generates "remember me" authentication key |
|
127
|
|
|
*/ |
|
128
|
|
|
public function generateAuthKey() |
|
129
|
|
|
{ |
|
130
|
|
|
$this->auth_key = Yii::$app->security->generateRandomString(); |
|
|
|
|
|
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Generates new password reset token |
|
135
|
|
|
*/ |
|
136
|
|
|
public function generatePasswordResetToken() |
|
137
|
|
|
{ |
|
138
|
|
|
$this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time(); |
|
|
|
|
|
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Removes password reset token |
|
143
|
|
|
*/ |
|
144
|
|
|
public function removePasswordResetToken() |
|
145
|
|
|
{ |
|
146
|
|
|
$this->password_reset_token = null; |
|
|
|
|
|
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @inheritdoc |
|
151
|
|
|
*/ |
|
152
|
|
|
public function checkUserCredentials($username, $password) |
|
153
|
|
|
{ |
|
154
|
|
|
$user = static::findByUsername($username); |
|
155
|
|
|
if (empty($user)) { |
|
156
|
|
|
return false; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return $user->validatePassword($password); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @inheritdoc |
|
164
|
|
|
*/ |
|
165
|
|
|
public function getUserDetails($username = null) |
|
166
|
|
|
{ |
|
167
|
|
|
$user = $username |
|
168
|
|
|
? static::findByUsername($username) |
|
169
|
|
|
: $this; |
|
170
|
|
|
|
|
171
|
|
|
return ['user_id' => $user->id]; |
|
|
|
|
|
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @inheritdoc |
|
176
|
|
|
*/ |
|
177
|
|
|
public function rules() |
|
178
|
|
|
{ |
|
179
|
|
|
return [ |
|
180
|
|
|
[['username'], 'string'], |
|
181
|
|
|
]; |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths