@@ -74,14 +74,15 @@ func GetEmailAddressByID(uid, id int64) (*EmailAddress, error) {
74
74
return email , nil
75
75
}
76
76
77
- func isEmailActive (e Engine , email string , userID , emailID int64 ) (bool , error ) {
77
+ // isEmailActive check if email is activated with a different emailID
78
+ func isEmailActive (e Engine , email string , excludeEmailID int64 ) (bool , error ) {
78
79
if len (email ) == 0 {
79
80
return true , nil
80
81
}
81
82
82
83
// Can't filter by boolean field unless it's explicit
83
84
cond := builder .NewCond ()
84
- cond = cond .And (builder.Eq {"lower_email" : strings .ToLower (email )}, builder.Neq {"id" : emailID })
85
+ cond = cond .And (builder.Eq {"lower_email" : strings .ToLower (email )}, builder.Neq {"id" : excludeEmailID })
85
86
if setting .Service .RegisterEmailConfirm {
86
87
// Inactive (unvalidated) addresses don't count as active if email validation is required
87
88
cond = cond .And (builder.Eq {"is_activated" : true })
@@ -90,7 +91,7 @@ func isEmailActive(e Engine, email string, userID, emailID int64) (bool, error)
90
91
var em EmailAddress
91
92
if has , err := e .Where (cond ).Get (& em ); has || err != nil {
92
93
if has {
93
- log .Info ("isEmailActive('%s',%d,%d ) found duplicate in email ID %d" , email , userID , emailID , em .ID )
94
+ log .Info ("isEmailActive(%q, %d ) found duplicate in email ID %d" , email , excludeEmailID , em .ID )
94
95
}
95
96
return has , err
96
97
}
@@ -366,8 +367,8 @@ func SearchEmails(opts *SearchEmailOptions) ([]*SearchEmailResult, int64, error)
366
367
}
367
368
368
369
// ActivateUserEmail will change the activated state of an email address,
369
- // either primary (in the user table) or secondary (in the email_address table)
370
- func ActivateUserEmail (userID int64 , email string , primary , activate bool ) (err error ) {
370
+ // either primary or secondary (all in the email_address table)
371
+ func ActivateUserEmail (userID int64 , email string , activate bool ) (err error ) {
371
372
sess := x .NewSession ()
372
373
defer sess .Close ()
373
374
if err = sess .Begin (); err != nil {
@@ -387,34 +388,33 @@ func ActivateUserEmail(userID int64, email string, primary, activate bool) (err
387
388
return nil
388
389
}
389
390
if activate {
390
- if used , err := isEmailActive (sess , email , 0 , addr .ID ); err != nil {
391
- return fmt .Errorf ("isEmailActive(): %v" , err )
391
+ if used , err := isEmailActive (sess , email , addr .ID ); err != nil {
392
+ return fmt .Errorf ("unable to check isEmailActive() for %s : %v" , email , err )
392
393
} else if used {
393
394
return ErrEmailAlreadyUsed {Email : email }
394
395
}
395
396
}
396
397
if err = addr .updateActivation (sess , activate ); err != nil {
397
- return fmt .Errorf ("updateActivation(): %v" , err )
398
+ return fmt .Errorf ("unable to updateActivation() for %d:%s: %w" , addr . ID , addr . Email , err )
398
399
}
399
400
400
- if primary {
401
- // Activate/deactivate a user's primary email address
401
+ // Activate/deactivate a user's primary email address and account
402
+ if addr . IsPrimary {
402
403
user := User {ID : userID , Email : email }
403
404
if has , err := sess .Get (& user ); err != nil {
404
405
return err
405
406
} else if ! has {
406
- return fmt .Errorf ("no such user: %d (%s) " , userID , email )
407
+ return fmt .Errorf ("no user with ID : %d and Email: %s " , userID , email )
407
408
}
408
- if user .IsActive == activate {
409
- // Already in the desired state; no action
410
- return nil
411
- }
412
- user .IsActive = activate
413
- if user .Rands , err = GetUserSalt (); err != nil {
414
- return fmt .Errorf ("generate salt: %v" , err )
415
- }
416
- if err = updateUserCols (sess , & user , "is_active" , "rands" ); err != nil {
417
- return fmt .Errorf ("updateUserCols(): %v" , err )
409
+ // The user's activation state should be synchronized with the primary email
410
+ if user .IsActive != activate {
411
+ user .IsActive = activate
412
+ if user .Rands , err = GetUserSalt (); err != nil {
413
+ return fmt .Errorf ("unable to generate salt: %v" , err )
414
+ }
415
+ if err = updateUserCols (sess , & user , "is_active" , "rands" ); err != nil {
416
+ return fmt .Errorf ("unable to updateUserCols() for user ID: %d: %v" , userID , err )
417
+ }
418
418
}
419
419
}
420
420
0 commit comments