1
- <?php
2
- namespace common \models ;
3
- use Yii ;
4
- use yii \base \NotSupportedException ;
5
- use yii \behaviors \TimestampBehavior ;
6
- use yii \db \ActiveRecord ;
7
- use yii \web \IdentityInterface ;
8
- /**
9
- * User model
10
- *
11
- * @property integer $id
12
- * @property string $username
13
- * @property string $password_hash
14
- * @property string $password_reset_token
15
- * @property string $email
16
- * @property string $auth_key
17
- * @property integer $status
18
- * @property integer $created_at
19
- * @property integer $updated_at
20
- * @property string $password write-only password
21
- */
22
- class User extends ActiveRecord implements IdentityInterface
23
- {
24
- const STATUS_DELETED = 0 ;
25
- const STATUS_ACTIVE = 10 ;
26
- /**
27
- * @inheritdoc
28
- */
29
- public static function tableName ()
30
- {
31
- return '{{%user}} ' ;
32
- }
33
- /**
34
- * @inheritdoc
35
- */
36
- public function behaviors ()
37
- {
38
- return [
39
- TimestampBehavior::className (),
40
- ];
41
- }
42
- /**
43
- * @inheritdoc
44
- */
45
- public function rules ()
46
- {
47
- return [
48
- ['status ' , 'default ' , 'value ' => self ::STATUS_ACTIVE ],
49
- ['status ' , 'in ' , 'range ' => [self ::STATUS_ACTIVE , self ::STATUS_DELETED ]],
50
- ];
51
- }
52
- /**
53
- * @inheritdoc
54
- */
55
- public static function findIdentity ($ id )
56
- {
57
- return static ::findOne (['id ' => $ id , 'status ' => self ::STATUS_ACTIVE ]);
58
- }
59
- /**
60
- * @inheritdoc
61
- */
62
- public static function findIdentityByAccessToken ($ token , $ type = null )
63
- {
64
- throw new NotSupportedException ( ' "findIdentityByAccessToken" is not implemented. ' );
65
- }
66
- /**
67
- * Finds user by username
68
- *
69
- * @param string $username
70
- * @return static|null
71
- */
72
- public static function findByUsername ($ username )
73
- {
74
- return static ::findOne (['username ' => $ username , 'status ' => self ::STATUS_ACTIVE ]);
75
- }
76
- /**
77
- * Finds user by password reset token
78
- *
79
- * @param string $token password reset token
80
- * @return static|null
81
- */
82
- public static function findByPasswordResetToken ($ token )
83
- {
84
- if (!static ::isPasswordResetTokenValid ($ token )) {
85
- return null ;
86
- }
87
- return static ::findOne ([
88
- 'password_reset_token ' => $ token ,
89
- 'status ' => self ::STATUS_ACTIVE ,
90
- ]);
91
- }
92
- /**
93
- * Finds out if password reset token is valid
94
- *
95
- * @param string $token password reset token
96
- * @return boolean
97
- */
98
- public static function isPasswordResetTokenValid ($ token )
99
- {
100
- if (empty ($ token )) {
101
- return false ;
102
- }
103
- $ expire = Yii::$ app ->params ['user.passwordResetTokenExpire ' ];
104
- $ parts = explode ('_ ' , $ token );
105
- $ timestamp = (int ) end ($ parts );
106
- return $ timestamp + $ expire >= time ();
107
- }
108
- /**
109
- * @inheritdoc
110
- */
111
- public function getId ()
112
- {
113
- return $ this ->getPrimaryKey ();
114
- }
115
- /**
116
- * @inheritdoc
117
- */
118
- public function getAuthKey ()
119
- {
120
- return $ this ->auth_key ;
121
- }
122
- /**
123
- * @inheritdoc
124
- */
125
- public function validateAuthKey ($ authKey )
126
- {
127
- return $ this ->getAuthKey () === $ authKey ;
128
- }
129
- /**
130
- * Validates password
131
- *
132
- * @param string $password password to validate
133
- * @return boolean if password provided is valid for current user
134
- */
135
- public function validatePassword ($ password )
136
- {
137
- return Yii::$ app ->security ->validatePassword ($ password , $ this ->password_hash );
138
- }
139
- /**
140
- * Generates password hash from password and sets it to the model
141
- *
142
- * @param string $password
143
- */
144
- public function setPassword ($ password )
145
- {
146
- $ this ->password_hash = Yii::$ app ->security ->generatePasswordHash ($ password );
147
- }
148
- /**
149
- * Generates "remember me" authentication key
150
- */
151
- public function generateAuthKey ()
152
- {
153
- $ this ->auth_key = Yii::$ app ->security ->generateRandomString ();
154
- }
155
- /**
156
- * Generates new password reset token
157
- */
158
- public function generatePasswordResetToken ()
159
- {
160
- $ this ->password_reset_token = Yii::$ app ->security ->generateRandomString () . '_ ' . time ();
161
- }
162
- /**
163
- * Removes password reset token
164
- */
165
- public function removePasswordResetToken ()
166
- {
167
- $ this ->password_reset_token = null ;
168
- }
1
+ <?php
2
+ namespace common \models ;
3
+ use Yii ;
4
+ use yii \base \NotSupportedException ;
5
+ use yii \behaviors \TimestampBehavior ;
6
+ use yii \db \ActiveRecord ;
7
+ use yii \web \IdentityInterface ;
8
+ /**
9
+ * User model
10
+ *
11
+ * @property integer $id
12
+ * @property string $username
13
+ * @property string $password_hash
14
+ * @property string $password_reset_token
15
+ * @property string $email
16
+ * @property string $auth_key
17
+ * @property integer $status
18
+ * @property integer $created_at
19
+ * @property integer $updated_at
20
+ * @property string $password write-only password
21
+ */
22
+ class User extends ActiveRecord implements IdentityInterface
23
+ {
24
+ const STATUS_DELETED = 0 ;
25
+ const STATUS_ACTIVE = 10 ;
26
+ /**
27
+ * @inheritdoc
28
+ */
29
+ public static function tableName ()
30
+ {
31
+ return '{{%user}} ' ;
32
+ }
33
+ /**
34
+ * @inheritdoc
35
+ */
36
+ public function behaviors ()
37
+ {
38
+ return [
39
+ TimestampBehavior::className (),
40
+ ];
41
+ }
42
+ /**
43
+ * @inheritdoc
44
+ */
45
+ public function rules ()
46
+ {
47
+ return [
48
+ ['status ' , 'default ' , 'value ' => self ::STATUS_ACTIVE ],
49
+ ['status ' , 'in ' , 'range ' => [self ::STATUS_ACTIVE , self ::STATUS_DELETED ]],
50
+ ];
51
+ }
52
+ /**
53
+ * @inheritdoc
54
+ */
55
+ public static function findIdentity ($ id )
56
+ {
57
+ return static ::findOne (['id ' => $ id , 'status ' => self ::STATUS_ACTIVE ]);
58
+ }
59
+ /**
60
+ * @inheritdoc
61
+ */
62
+ public static function findIdentityByAccessToken ($ token , $ type = null )
63
+ {
64
+ return static :: findOne ([ ' auth_key ' => $ token ] );
65
+ }
66
+ /**
67
+ * Finds user by username
68
+ *
69
+ * @param string $username
70
+ * @return static|null
71
+ */
72
+ public static function findByUsername ($ username )
73
+ {
74
+ return static ::findOne (['username ' => $ username , 'status ' => self ::STATUS_ACTIVE ]);
75
+ }
76
+ /**
77
+ * Finds user by password reset token
78
+ *
79
+ * @param string $token password reset token
80
+ * @return static|null
81
+ */
82
+ public static function findByPasswordResetToken ($ token )
83
+ {
84
+ if (!static ::isPasswordResetTokenValid ($ token )) {
85
+ return null ;
86
+ }
87
+ return static ::findOne ([
88
+ 'password_reset_token ' => $ token ,
89
+ 'status ' => self ::STATUS_ACTIVE ,
90
+ ]);
91
+ }
92
+ /**
93
+ * Finds out if password reset token is valid
94
+ *
95
+ * @param string $token password reset token
96
+ * @return boolean
97
+ */
98
+ public static function isPasswordResetTokenValid ($ token )
99
+ {
100
+ if (empty ($ token )) {
101
+ return false ;
102
+ }
103
+ $ expire = Yii::$ app ->params ['user.passwordResetTokenExpire ' ];
104
+ $ parts = explode ('_ ' , $ token );
105
+ $ timestamp = (int ) end ($ parts );
106
+ return $ timestamp + $ expire >= time ();
107
+ }
108
+ /**
109
+ * @inheritdoc
110
+ */
111
+ public function getId ()
112
+ {
113
+ return $ this ->getPrimaryKey ();
114
+ }
115
+ /**
116
+ * @inheritdoc
117
+ */
118
+ public function getAuthKey ()
119
+ {
120
+ return $ this ->auth_key ;
121
+ }
122
+ /**
123
+ * @inheritdoc
124
+ */
125
+ public function validateAuthKey ($ authKey )
126
+ {
127
+ return $ this ->getAuthKey () === $ authKey ;
128
+ }
129
+ /**
130
+ * Validates password
131
+ *
132
+ * @param string $password password to validate
133
+ * @return boolean if password provided is valid for current user
134
+ */
135
+ public function validatePassword ($ password )
136
+ {
137
+ return Yii::$ app ->security ->validatePassword ($ password , $ this ->password_hash );
138
+ }
139
+ /**
140
+ * Generates password hash from password and sets it to the model
141
+ *
142
+ * @param string $password
143
+ */
144
+ public function setPassword ($ password )
145
+ {
146
+ $ this ->password_hash = Yii::$ app ->security ->generatePasswordHash ($ password );
147
+ }
148
+ /**
149
+ * Generates "remember me" authentication key
150
+ */
151
+ public function generateAuthKey ()
152
+ {
153
+ $ this ->auth_key = Yii::$ app ->security ->generateRandomString ();
154
+ }
155
+ /**
156
+ * Generates new password reset token
157
+ */
158
+ public function generatePasswordResetToken ()
159
+ {
160
+ $ this ->password_reset_token = Yii::$ app ->security ->generateRandomString () . '_ ' . time ();
161
+ }
162
+ /**
163
+ * Removes password reset token
164
+ */
165
+ public function removePasswordResetToken ()
166
+ {
167
+ $ this ->password_reset_token = null ;
168
+ }
169
169
}
0 commit comments