Fix ldap user sync missed email in email_address table (#18786)

* Fix ldap user sync missed email in email_address table

* Fix test
This commit is contained in:
Lunny Xiao 2022-02-24 14:13:59 +08:00 committed by GitHub
parent cb41f5cae1
commit f1b1472632
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 6 deletions

View File

@ -827,8 +827,9 @@ func validateUser(u *User) error {
return ValidateEmail(u.Email) return ValidateEmail(u.Email)
} }
func updateUser(ctx context.Context, u *User, changePrimaryEmail bool) error { func updateUser(ctx context.Context, u *User, changePrimaryEmail bool, cols ...string) error {
if err := validateUser(u); err != nil { err := validateUser(u)
if err != nil {
return err return err
} }
@ -860,15 +861,34 @@ func updateUser(ctx context.Context, u *User, changePrimaryEmail bool) error {
}); err != nil { }); err != nil {
return err return err
} }
} else { // check if primary email in email_address table
primaryEmailExist, err := e.Where("uid=? AND is_primary=?", u.ID, true).Exist(&EmailAddress{})
if err != nil {
return err
} }
_, err := e.ID(u.ID).AllCols().Update(u) if !primaryEmailExist {
_, err = e.Insert(&EmailAddress{
Email: u.Email,
UID: u.ID,
IsActivated: true,
IsPrimary: true,
})
return err
}
}
if len(cols) == 0 {
_, err = e.ID(u.ID).AllCols().Update(u)
} else {
_, err = e.ID(u.ID).Cols(cols...).Update(u)
}
return err return err
} }
// UpdateUser updates user's information. // UpdateUser updates user's information.
func UpdateUser(u *User, emailChanged bool) error { func UpdateUser(u *User, emailChanged bool, cols ...string) error {
return updateUser(db.DefaultContext, u, emailChanged) return updateUser(db.DefaultContext, u, emailChanged, cols...)
} }
// UpdateUserCols update user according special columns // UpdateUserCols update user according special columns

View File

@ -146,6 +146,7 @@ func (source *Source) Sync(ctx context.Context, updateExisting bool) error {
log.Trace("SyncExternalUsers[%s]: Updating user %s", source.authSource.Name, usr.Name) log.Trace("SyncExternalUsers[%s]: Updating user %s", source.authSource.Name, usr.Name)
usr.FullName = fullName usr.FullName = fullName
emailChanged := usr.Email != su.Mail
usr.Email = su.Mail usr.Email = su.Mail
// Change existing admin flag only if AdminFilter option is set // Change existing admin flag only if AdminFilter option is set
if len(source.AdminFilter) > 0 { if len(source.AdminFilter) > 0 {
@ -157,7 +158,7 @@ func (source *Source) Sync(ctx context.Context, updateExisting bool) error {
} }
usr.IsActive = true usr.IsActive = true
err = user_model.UpdateUserCols(db.DefaultContext, usr, "full_name", "email", "is_admin", "is_restricted", "is_active") err = user_model.UpdateUser(usr, emailChanged, "full_name", "email", "is_admin", "is_restricted", "is_active")
if err != nil { if err != nil {
log.Error("SyncExternalUsers[%s]: Error updating user %s: %v", source.authSource.Name, usr.Name, err) log.Error("SyncExternalUsers[%s]: Error updating user %s: %v", source.authSource.Name, usr.Name, err)
} }