1313 *
1414 * {@inheritdoc}
1515 */
16- class User extends ActiveRecord
16+
17+ /**
18+ * User model
19+ *
20+ * @property integer $id
21+ * @property string $username
22+ * @property string $password_hash
23+ * @property string $password_reset_token
24+ * @property string $email
25+ * @property string $auth_key
26+ * @property string $access_token
27+ * @property string $refresh_token
28+ * @property integer $status
29+ * @property integer $created_at
30+ * @property integer $updated_at
31+ * @property string $created_by
32+ * @property string $updated_by
33+ * @property string $password write-only password
34+ */
35+ class User extends ActiveRecord implements IdentityInterface
1736{
18- public $ username ;
37+ public $ userna ;
1938 public $ password ;
2039 public $ rememberMe = true ;
2140
@@ -37,11 +56,13 @@ public function rules()
3756 {
3857 return [
3958 // username and password are both required
40- [['username ' , 'password ' ], 'required ' ],
59+ [['username ' , 'password_hash ' ], 'required ' ],
60+ [['username ' , 'password_hash ' ], 'safe ' ],
61+ [['username ' , 'password_hash ' ], 'string ' ],
4162 // rememberMe must be a boolean value
4263 ['rememberMe ' , 'boolean ' ],
4364 // password is validated by validatePassword()
44- ['password ' , 'validatePassword ' ],
65+ ['password_hash ' , 'validatePassword ' ],
4566 ];
4667 }
4768
@@ -54,22 +75,146 @@ public function getId()
5475 }
5576
5677 /**
57- * Validates the password.
58- * This method serves as the inline validation for password.
78+ * {@inheritdoc}
79+ */
80+ public static function findIdentity ($ id )
81+ {
82+ return static ::findOne (['id ' => $ id , 'status ' => self ::STATUS_ACTIVE ]);
83+ }
84+
85+ /**
86+ * {@inheritdoc}
87+ */
88+ public static function findIdentityByAccessToken ($ token , $ type = null )
89+ {
90+ return static ::findOne (['access_token ' => $ token , 'status ' => self ::STATUS_ACTIVE ]);
91+ }
92+
93+ /**
94+ * Generates new access token
95+ */
96+ public function generateAccessToken ()
97+ {
98+ $ this ->access_token = Yii::$ app ->security ->generateRandomString (32 ) . '_ ' . time ();
99+ }
100+
101+ /**
102+ * Validates password
103+ *
104+ * @param string $password password to validate
105+ * @return bool if password provided is valid for current user
106+ */
107+ public function validatePassword ($ password )
108+ {
109+ return Yii::$ app ->security ->validatePassword ($ password , $ this ->password_hash );
110+ }
111+
112+
113+ /**
114+ * {@inheritdoc}
115+ */
116+ public function validateAuthKey ($ authKey )
117+ {
118+ return $ this ->getAuthKey () === $ authKey ;
119+ }
120+
121+ /**
122+ * {@inheritdoc}
123+ */
124+ public function getAuthKey ()
125+ {
126+ return $ this ->auth_key ;
127+ }
128+
129+
130+ /**
131+ * Generates password hash from password and sets it to the model
132+ *
133+ * @param string $password
134+ */
135+ public function setPassword ($ password )
136+ {
137+ $ this ->password_hash = Yii::$ app ->security ->generatePasswordHash ($ password );
138+ }
139+
140+
141+ /**
142+ * Generates "remember me" authentication key
143+ */
144+ public function generateAuthKey ()
145+ {
146+ $ this ->auth_key = Yii::$ app ->security ->generateRandomString ();
147+ }
148+
149+ /**
150+ * Generates new password reset token
151+ */
152+ public function generatePasswordResetToken ()
153+ {
154+ $ this ->password_reset_token = Yii::$ app ->security ->generateRandomString () . '_ ' . time ();
155+ }
156+
157+ /**
158+ * Removes password reset token
159+ */
160+ public function removePasswordResetToken ()
161+ {
162+ $ this ->password_reset_token = null ;
163+ }
164+
165+ /**
166+ * Finds user by password reset token
167+ *
168+ * @param string $token password reset token
169+ * @return static|null
170+ */
171+ public static function findByPasswordResetToken ($ token )
172+ {
173+ if (!static ::isPasswordResetTokenValid ($ token )) {
174+ return null ;
175+ }
176+
177+ return static ::findOne ([
178+ 'password_reset_token ' => $ token ,
179+ 'status ' => self ::STATUS_ACTIVE ,
180+ ]);
181+ }
182+
183+ /**
184+ * Finds out if password reset token is valid
59185 *
60- * @param string $attribute the attribute currently being validated
61- * @param array $params the additional name-value pairs given in the rule
186+ * @param string $token password reset token
187+ * @return bool
62188 */
63- public function validatePassword ( $ attribute , $ params )
189+ public static function isPasswordResetTokenValid ( $ token )
64190 {
65- if (!$ this ->hasErrors ()) {
66- $ user = $ this ->getUser ();
67- if (!$ user || !$ user ->validatePassword ($ this ->password )) {
68- $ this ->addError ($ attribute , 'Incorrect username or password. ' );
69- }
191+ if (empty ($ token )) {
192+ return false ;
70193 }
194+
195+ $ timestamp = (int )substr ($ token , strrpos ($ token , '_ ' ) + 1 );
196+ $ expire = Yii::$ app ->params ['user.passwordResetTokenExpire ' ];
197+ return $ timestamp + $ expire >= time ();
198+ }
199+
200+ /**
201+ * Generates new refresh token
202+ */
203+ public function generateRefreshToken ()
204+ {
205+ $ this ->refresh_token = Yii::$ app ->security ->generateRandomString (32 ) . '_ ' . time ();
206+
207+ }
208+
209+ /**
210+ * Removes access token
211+ */
212+ public function removeRefreshToken ()
213+ {
214+ $ this ->refresh_token = null ;
71215 }
72216
217+
73218 /**
74219 * Finds user by [[username]]
75220 *
@@ -102,10 +247,43 @@ public static function findByUsername($username)
102247 */
103248 public function login ()
104249 {
105- if ($ this ->validate ()) {
106- return Yii::$ app ->user ->login ($ this ->getUser (), $ this ->rememberMe ? 3600 * 24 * 30 : 0 );
250+ $ login = Yii::$ app ->user ->login ($ this ->getUser (), $ this ->rememberMe ? 3600 * 24 * 30 : 0 );
251+ if ($ login == true ){
252+ return $ this ->_user ;
107253 }
254+ else
255+ return $ login ;
108256
257+ /*
258+ if ($this->validate()) {
259+ return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
260+ }
109261 return false;
262+ */
263+ }
264+
265+ public function register ($ data ){
266+
267+ $ user = new User ();
268+ $ user ->id = null ;
269+ $ user ->email = $ data ->username ;
270+ $ user ->username = $ data ->username ;
271+ $ user ->setPassword ($ data ->password );
272+ $ user ->generateAuthKey ();
273+ $ user ->generatePasswordResetToken ();
274+ $ user ->generateAccessToken ();
275+ $ user ->generateRefreshToken ();
276+ $ user ->created_at = time () /1000 ;
277+ $ user ->updated_at = time () / 1000 ;
278+
279+ $ user ->isNewRecord = true ;
280+ if (!$ user ->save ()) {
281+ return $ user ->getErrors ();
282+ }
283+ else {
284+ return $ user ;
285+
286+ }
110287 }
288+
111289}
0 commit comments