1717 * @property string $role_id
1818 * @property integer $status
1919 * @property string $email
20- * @property string $new_email
2120 * @property string $username
2221 * @property string $password
2322 * @property string $auth_key
@@ -136,7 +135,6 @@ public function attributeLabels()
136135 'role_id ' => Yii::t ('user ' , 'Role ID ' ),
137136 'status ' => Yii::t ('user ' , 'Status ' ),
138137 'email ' => Yii::t ('user ' , 'Email ' ),
139- 'new_email ' => Yii::t ('user ' , 'New Email ' ),
140138 'username ' => Yii::t ('user ' , 'Username ' ),
141139 'password ' => Yii::t ('user ' , 'Password ' ),
142140 'auth_key ' => Yii::t ('user ' , 'Auth Key ' ),
@@ -334,30 +332,31 @@ public function setRegisterAttributes($roleId, $userIp, $status = null)
334332 }
335333
336334 /**
337- * Check and prepare for email change
338- * @return bool True if user set a `new_email`
335+ * Check for email change
336+ * @return string| bool
339337 */
340- public function checkAndPrepEmailChange ()
338+ public function checkEmailChange ()
341339 {
342- // check if user is removing email address (only if Module::$requireEmail = false)
343- if (trim ( $ this ->email ) === "" ) {
340+ // check if user didn't change email
341+ if ($ this ->email == $ this -> getOldAttribute ( " email " ) ) {
344342 return false ;
345343 }
346344
347- // check for change in email
348- if ($ this ->email != $ this ->getOldAttribute ("email " )) {
349-
350- // change status
351- $ this ->status = static ::STATUS_UNCONFIRMED_EMAIL ;
352-
353- // set `new_email` attribute and restore old one
354- $ this ->new_email = $ this ->email ;
355- $ this ->email = $ this ->getOldAttribute ("email " );
345+ // check if we need to confirm email change
346+ if (!Yii::$ app ->getModule ("user " )->emailChangeConfirmation ) {
347+ return false ;
348+ }
356349
357- return true ;
350+ // check if user is removing email address (only valid if Module::$requireEmail = false)
351+ if (!$ this ->email ) {
352+ return false ;
358353 }
359354
360- return false ;
355+ // update status and email before returning new email
356+ $ newEmail = $ this ->email ;
357+ $ this ->status = static ::STATUS_UNCONFIRMED_EMAIL ;
358+ $ this ->email = $ this ->getOldAttribute ("email " );
359+ return $ newEmail ;
361360 }
362361
363362 /**
@@ -371,42 +370,29 @@ public function updateLoginMeta()
371370 return $ this ->save (false , ["logged_in_ip " , "logged_in_at " ]);
372371 }
373372
374- /**
375- * Clear new_email field
376- * @param bool $save
377- * @return static
378- */
379- public function clearNewEmail ($ save = true )
380- {
381- $ this ->new_email = null ;
382- if ($ save ) {
383- $ this ->save (false , ["new_email " ]);
384- }
385- return $ this ;
386- }
387-
388373 /**
389374 * Confirm user email
375+ * @param string $newEmail
390376 * @return bool
391377 */
392- public function confirm ()
378+ public function confirm ($ newEmail )
393379 {
394380 // update status
395381 $ this ->status = static ::STATUS_ACTIVE ;
396382
397- // check new_email if set and check if another user already has it
383+ // process $newEmail from a userToken
384+ // check if another user already has that email
398385 $ success = true ;
399- if ($ this -> new_email ) {
400- $ checkUser = static ::findOne (["email " => $ this -> new_email ]);
386+ if ($ newEmail ) {
387+ $ checkUser = static ::findOne (["email " => $ newEmail ]);
401388 if ($ checkUser ) {
402389 $ success = false ;
403390 } else {
404- $ this ->email = $ this -> new_email ;
391+ $ this ->email = $ newEmail ;
405392 }
406- $ this ->new_email = null ;
407393 }
408394
409- $ this ->save (false , ["email " , "new_email " , " status " ]);
395+ $ this ->save (false , ["email " , "status " ]);
410396 return $ success ;
411397 }
412398
@@ -479,7 +465,7 @@ public function sendEmailConfirmation($userToken)
479465 // send email
480466 $ user = $ this ;
481467 $ profile = $ user ->profile ;
482- $ email = $ user -> new_email ?: $ user ->email ;
468+ $ email = $ userToken -> data ?: $ user ->email ;
483469 $ subject = Yii::$ app ->id . " - " . Yii::t ("user " , "Email Confirmation " );
484470 $ message = $ mailer ->compose ('confirmEmail ' , compact ("subject " , "user " , "profile " , "userToken " ))
485471 ->setTo ($ email )
0 commit comments