|
1
|
1
|
<?php |
|
2
|
|
-namespace common\models; |
|
3
|
|
- |
|
4
|
|
-use Yii; |
|
5
|
|
-use yii\base\NotSupportedException; |
|
6
|
|
-use yii\behaviors\TimestampBehavior; |
|
7
|
|
-use yii\db\ActiveRecord; |
|
8
|
|
-use yii\web\IdentityInterface; |
|
9
|
|
- |
|
10
|
|
-/** |
|
11
|
|
- * User model |
|
12
|
|
- * |
|
13
|
|
- * @property integer $id |
|
14
|
|
- * @property string $username |
|
15
|
|
- * @property string $password_hash |
|
16
|
|
- * @property string $password_reset_token |
|
17
|
|
- * @property string $email |
|
18
|
|
- * @property string $auth_key |
|
19
|
|
- * @property integer $status |
|
20
|
|
- * @property integer $created_at |
|
21
|
|
- * @property integer $updated_at |
|
22
|
|
- * @property string $password write-only password |
|
23
|
|
- */ |
|
24
|
|
-class User extends ActiveRecord implements IdentityInterface |
|
25
|
|
-{ |
|
26
|
|
- const STATUS_DELETED = 0; |
|
27
|
|
- const STATUS_ACTIVE = 10; |
|
28
|
|
- |
|
29
|
|
- |
|
30
|
|
- /** |
|
31
|
|
- * @inheritdoc |
|
32
|
|
- */ |
|
33
|
|
- public static function tableName() |
|
34
|
|
- { |
|
35
|
|
- return '{{%user}}'; |
|
36
|
|
- } |
|
37
|
|
- |
|
38
|
|
- /** |
|
39
|
|
- * @inheritdoc |
|
40
|
|
- */ |
|
41
|
|
- public function behaviors() |
|
42
|
|
- { |
|
43
|
|
- return [ |
|
44
|
|
- TimestampBehavior::className(), |
|
45
|
|
- ]; |
|
46
|
|
- } |
|
47
|
|
- |
|
48
|
|
- /** |
|
49
|
|
- * @inheritdoc |
|
50
|
|
- */ |
|
51
|
|
- public function rules() |
|
52
|
|
- { |
|
53
|
|
- return [ |
|
54
|
|
- ['status', 'default', 'value' => self::STATUS_ACTIVE], |
|
55
|
|
- ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]], |
|
56
|
|
- ]; |
|
57
|
|
- } |
|
58
|
|
- |
|
59
|
|
- /** |
|
60
|
|
- * @inheritdoc |
|
61
|
|
- */ |
|
62
|
|
- public static function findIdentity($id) |
|
63
|
|
- { |
|
64
|
|
- return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]); |
|
65
|
|
- } |
|
66
|
|
- |
|
67
|
|
- /** |
|
68
|
|
- * @inheritdoc |
|
69
|
|
- */ |
|
70
|
|
- public static function findIdentityByAccessToken($token, $type = null) |
|
71
|
|
- { |
|
72
|
|
- throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.'); |
|
73
|
|
- } |
|
74
|
|
- |
|
75
|
|
- /** |
|
76
|
|
- * Finds user by username |
|
|
2
|
+ namespace common\models; |
|
|
3
|
+ |
|
|
4
|
+ use frontend\models\UserData; |
|
|
5
|
+ use frontend\models\UserPassport; |
|
|
6
|
+ use Yii; |
|
|
7
|
+ use yii\base\NotSupportedException; |
|
|
8
|
+ use yii\behaviors\TimestampBehavior; |
|
|
9
|
+ use yii\db\ActiveRecord; |
|
|
10
|
+ use yii\web\IdentityInterface; |
|
|
11
|
+ |
|
|
12
|
+ /** |
|
|
13
|
+ * User model |
|
77
|
14
|
* |
|
78
|
|
- * @param string $username |
|
79
|
|
- * @return static|null |
|
80
|
|
- */ |
|
81
|
|
- public static function findByUsername($username) |
|
82
|
|
- { |
|
83
|
|
- return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]); |
|
84
|
|
- } |
|
85
|
|
- |
|
86
|
|
- /** |
|
87
|
|
- * Finds user by password reset token |
|
88
|
|
- * |
|
89
|
|
- * @param string $token password reset token |
|
90
|
|
- * @return static|null |
|
91
|
|
- */ |
|
92
|
|
- public static function findByPasswordResetToken($token) |
|
93
|
|
- { |
|
94
|
|
- if (!static::isPasswordResetTokenValid($token)) { |
|
95
|
|
- return null; |
|
96
|
|
- } |
|
97
|
|
- |
|
98
|
|
- return static::findOne([ |
|
99
|
|
- 'password_reset_token' => $token, |
|
100
|
|
- 'status' => self::STATUS_ACTIVE, |
|
101
|
|
- ]); |
|
102
|
|
- } |
|
103
|
|
- |
|
104
|
|
- /** |
|
105
|
|
- * Finds out if password reset token is valid |
|
106
|
|
- * |
|
107
|
|
- * @param string $token password reset token |
|
108
|
|
- * @return bool |
|
109
|
|
- */ |
|
110
|
|
- public static function isPasswordResetTokenValid($token) |
|
111
|
|
- { |
|
112
|
|
- if (empty($token)) { |
|
113
|
|
- return false; |
|
|
15
|
+ * @property integer $id |
|
|
16
|
+ * @property string $username |
|
|
17
|
+ * @property string $password_hash |
|
|
18
|
+ * @property string $password_reset_token |
|
|
19
|
+ * @property string $email |
|
|
20
|
+ * @property string $auth_key |
|
|
21
|
+ * @property integer $status |
|
|
22
|
+ * @property integer $created_at |
|
|
23
|
+ * @property integer $updated_at |
|
|
24
|
+ * @property string $password write-only password |
|
|
25
|
+ * @property UserData $userData |
|
|
26
|
+ * @property UserPassport $userPassport |
|
|
27
|
+ */ |
|
|
28
|
+ class User extends ActiveRecord implements IdentityInterface |
|
|
29
|
+ { |
|
|
30
|
+ const STATUS_DELETED = 0; |
|
|
31
|
+ const STATUS_ACTIVE = 10; |
|
|
32
|
+ |
|
|
33
|
+ /** |
|
|
34
|
+ * @inheritdoc |
|
|
35
|
+ */ |
|
|
36
|
+ public static function tableName() |
|
|
37
|
+ { |
|
|
38
|
+ return '{{%user}}'; |
|
|
39
|
+ } |
|
|
40
|
+ |
|
|
41
|
+ /** |
|
|
42
|
+ * @inheritdoc |
|
|
43
|
+ */ |
|
|
44
|
+ public function behaviors() |
|
|
45
|
+ { |
|
|
46
|
+ return [ |
|
|
47
|
+ TimestampBehavior::className(), |
|
|
48
|
+ ]; |
|
|
49
|
+ } |
|
|
50
|
+ |
|
|
51
|
+ /** |
|
|
52
|
+ * @inheritdoc |
|
|
53
|
+ */ |
|
|
54
|
+ public function rules() |
|
|
55
|
+ { |
|
|
56
|
+ return [ |
|
|
57
|
+ [ |
|
|
58
|
+ 'status', |
|
|
59
|
+ 'default', |
|
|
60
|
+ 'value' => self::STATUS_ACTIVE, |
|
|
61
|
+ ], |
|
|
62
|
+ [ |
|
|
63
|
+ 'status', |
|
|
64
|
+ 'in', |
|
|
65
|
+ 'range' => [ |
|
|
66
|
+ self::STATUS_ACTIVE, |
|
|
67
|
+ self::STATUS_DELETED, |
|
|
68
|
+ ], |
|
|
69
|
+ ], |
|
|
70
|
+ ]; |
|
|
71
|
+ } |
|
|
72
|
+ |
|
|
73
|
+ /** |
|
|
74
|
+ * @inheritdoc |
|
|
75
|
+ */ |
|
|
76
|
+ public static function findIdentity($id) |
|
|
77
|
+ { |
|
|
78
|
+ return static::findOne( |
|
|
79
|
+ [ |
|
|
80
|
+ 'id' => $id, |
|
|
81
|
+ 'status' => self::STATUS_ACTIVE, |
|
|
82
|
+ ] |
|
|
83
|
+ ); |
|
|
84
|
+ } |
|
|
85
|
+ |
|
|
86
|
+ /** |
|
|
87
|
+ * @inheritdoc |
|
|
88
|
+ */ |
|
|
89
|
+ public static function findIdentityByAccessToken($token, $type = null) |
|
|
90
|
+ { |
|
|
91
|
+ throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.'); |
|
|
92
|
+ } |
|
|
93
|
+ |
|
|
94
|
+ /** |
|
|
95
|
+ * Finds user by username |
|
|
96
|
+ * |
|
|
97
|
+ * @param string $username |
|
|
98
|
+ * |
|
|
99
|
+ * @return static|null |
|
|
100
|
+ */ |
|
|
101
|
+ public static function findByUsername($username) |
|
|
102
|
+ { |
|
|
103
|
+ return static::findOne( |
|
|
104
|
+ [ |
|
|
105
|
+ 'username' => $username, |
|
|
106
|
+ 'status' => self::STATUS_ACTIVE, |
|
|
107
|
+ ] |
|
|
108
|
+ ); |
|
|
109
|
+ } |
|
|
110
|
+ |
|
|
111
|
+ /** |
|
|
112
|
+ * Finds user by password reset token |
|
|
113
|
+ * |
|
|
114
|
+ * @param string $token password reset token |
|
|
115
|
+ * |
|
|
116
|
+ * @return static|null |
|
|
117
|
+ */ |
|
|
118
|
+ public static function findByPasswordResetToken($token) |
|
|
119
|
+ { |
|
|
120
|
+ if (!static::isPasswordResetTokenValid($token)) { |
|
|
121
|
+ return null; |
|
|
122
|
+ } |
|
|
123
|
+ |
|
|
124
|
+ return static::findOne( |
|
|
125
|
+ [ |
|
|
126
|
+ 'password_reset_token' => $token, |
|
|
127
|
+ 'status' => self::STATUS_ACTIVE, |
|
|
128
|
+ ] |
|
|
129
|
+ ); |
|
|
130
|
+ } |
|
|
131
|
+ |
|
|
132
|
+ /** |
|
|
133
|
+ * Finds out if password reset token is valid |
|
|
134
|
+ * |
|
|
135
|
+ * @param string $token password reset token |
|
|
136
|
+ * |
|
|
137
|
+ * @return bool |
|
|
138
|
+ */ |
|
|
139
|
+ public static function isPasswordResetTokenValid($token) |
|
|
140
|
+ { |
|
|
141
|
+ if (empty( $token )) { |
|
|
142
|
+ return false; |
|
|
143
|
+ } |
|
|
144
|
+ |
|
|
145
|
+ $timestamp = (int) substr($token, strrpos($token, '_') + 1); |
|
|
146
|
+ $expire = Yii::$app->params[ 'user.passwordResetTokenExpire' ]; |
|
|
147
|
+ return $timestamp + $expire >= time(); |
|
|
148
|
+ } |
|
|
149
|
+ |
|
|
150
|
+ /** |
|
|
151
|
+ * @inheritdoc |
|
|
152
|
+ */ |
|
|
153
|
+ public function getId() |
|
|
154
|
+ { |
|
|
155
|
+ return $this->getPrimaryKey(); |
|
|
156
|
+ } |
|
|
157
|
+ |
|
|
158
|
+ /** |
|
|
159
|
+ * @inheritdoc |
|
|
160
|
+ */ |
|
|
161
|
+ public function getAuthKey() |
|
|
162
|
+ { |
|
|
163
|
+ return $this->auth_key; |
|
|
164
|
+ } |
|
|
165
|
+ |
|
|
166
|
+ /** |
|
|
167
|
+ * @inheritdoc |
|
|
168
|
+ */ |
|
|
169
|
+ public function validateAuthKey($authKey) |
|
|
170
|
+ { |
|
|
171
|
+ return $this->getAuthKey() === $authKey; |
|
|
172
|
+ } |
|
|
173
|
+ |
|
|
174
|
+ /** |
|
|
175
|
+ * Validates password |
|
|
176
|
+ * |
|
|
177
|
+ * @param string $password password to validate |
|
|
178
|
+ * |
|
|
179
|
+ * @return bool if password provided is valid for current user |
|
|
180
|
+ */ |
|
|
181
|
+ public function validatePassword($password) |
|
|
182
|
+ { |
|
|
183
|
+ return Yii::$app->security->validatePassword($password, $this->password_hash); |
|
|
184
|
+ } |
|
|
185
|
+ |
|
|
186
|
+ /** |
|
|
187
|
+ * Generates password hash from password and sets it to the model |
|
|
188
|
+ * |
|
|
189
|
+ * @param string $password |
|
|
190
|
+ */ |
|
|
191
|
+ public function setPassword($password) |
|
|
192
|
+ { |
|
|
193
|
+ $this->password_hash = Yii::$app->security->generatePasswordHash($password); |
|
|
194
|
+ } |
|
|
195
|
+ |
|
|
196
|
+ /** |
|
|
197
|
+ * Generates "remember me" authentication key |
|
|
198
|
+ */ |
|
|
199
|
+ public function generateAuthKey() |
|
|
200
|
+ { |
|
|
201
|
+ $this->auth_key = Yii::$app->security->generateRandomString(); |
|
|
202
|
+ } |
|
|
203
|
+ |
|
|
204
|
+ /** |
|
|
205
|
+ * Generates new password reset token |
|
|
206
|
+ */ |
|
|
207
|
+ public function generatePasswordResetToken() |
|
|
208
|
+ { |
|
|
209
|
+ $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time(); |
|
|
210
|
+ } |
|
|
211
|
+ |
|
|
212
|
+ /** |
|
|
213
|
+ * Removes password reset token |
|
|
214
|
+ */ |
|
|
215
|
+ public function removePasswordResetToken() |
|
|
216
|
+ { |
|
|
217
|
+ $this->password_reset_token = null; |
|
|
218
|
+ } |
|
|
219
|
+ |
|
|
220
|
+ public function getUserData() |
|
|
221
|
+ { |
|
|
222
|
+ return $this->hasOne(UserData::className(), [ 'user_id' => 'id' ]); |
|
|
223
|
+ } |
|
|
224
|
+ |
|
|
225
|
+ public function getUserPassport() |
|
|
226
|
+ { |
|
|
227
|
+ return $this->hasOne(UserPassport::className(), [ 'user_id' => 'id' ]); |
|
114
|
228
|
} |
|
115
|
|
- |
|
116
|
|
- $timestamp = (int) substr($token, strrpos($token, '_') + 1); |
|
117
|
|
- $expire = Yii::$app->params['user.passwordResetTokenExpire']; |
|
118
|
|
- return $timestamp + $expire >= time(); |
|
119
|
|
- } |
|
120
|
|
- |
|
121
|
|
- /** |
|
122
|
|
- * @inheritdoc |
|
123
|
|
- */ |
|
124
|
|
- public function getId() |
|
125
|
|
- { |
|
126
|
|
- return $this->getPrimaryKey(); |
|
127
|
|
- } |
|
128
|
|
- |
|
129
|
|
- /** |
|
130
|
|
- * @inheritdoc |
|
131
|
|
- */ |
|
132
|
|
- public function getAuthKey() |
|
133
|
|
- { |
|
134
|
|
- return $this->auth_key; |
|
135
|
|
- } |
|
136
|
|
- |
|
137
|
|
- /** |
|
138
|
|
- * @inheritdoc |
|
139
|
|
- */ |
|
140
|
|
- public function validateAuthKey($authKey) |
|
141
|
|
- { |
|
142
|
|
- return $this->getAuthKey() === $authKey; |
|
143
|
|
- } |
|
144
|
|
- |
|
145
|
|
- /** |
|
146
|
|
- * Validates password |
|
147
|
|
- * |
|
148
|
|
- * @param string $password password to validate |
|
149
|
|
- * @return bool if password provided is valid for current user |
|
150
|
|
- */ |
|
151
|
|
- public function validatePassword($password) |
|
152
|
|
- { |
|
153
|
|
- return Yii::$app->security->validatePassword($password, $this->password_hash); |
|
154
|
|
- } |
|
155
|
|
- |
|
156
|
|
- /** |
|
157
|
|
- * Generates password hash from password and sets it to the model |
|
158
|
|
- * |
|
159
|
|
- * @param string $password |
|
160
|
|
- */ |
|
161
|
|
- public function setPassword($password) |
|
162
|
|
- { |
|
163
|
|
- $this->password_hash = Yii::$app->security->generatePasswordHash($password); |
|
164
|
|
- } |
|
165
|
|
- |
|
166
|
|
- /** |
|
167
|
|
- * Generates "remember me" authentication key |
|
168
|
|
- */ |
|
169
|
|
- public function generateAuthKey() |
|
170
|
|
- { |
|
171
|
|
- $this->auth_key = Yii::$app->security->generateRandomString(); |
|
172
|
|
- } |
|
173
|
|
- |
|
174
|
|
- /** |
|
175
|
|
- * Generates new password reset token |
|
176
|
|
- */ |
|
177
|
|
- public function generatePasswordResetToken() |
|
178
|
|
- { |
|
179
|
|
- $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time(); |
|
180
|
|
- } |
|
181
|
|
- |
|
182
|
|
- /** |
|
183
|
|
- * Removes password reset token |
|
184
|
|
- */ |
|
185
|
|
- public function removePasswordResetToken() |
|
186
|
|
- { |
|
187
|
|
- $this->password_reset_token = null; |
|
188
|
229
|
} |
|
189
|
|
-} |
...
|
...
|
|