aboutsummaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2022-02-25 02:07:52 +0800
committerGitHub <noreply@github.com>2022-02-24 19:07:52 +0100
commit3685cc7660e968f9674087dfa213be5a78247f0d (patch)
tree31f0a9a3c456949fbacfb4e690bdb1b0112f093b /models
parent9d9ccdbe434992d21b418eefae0ecc32a5f22b14 (diff)
downloadgitea-3685cc7660e968f9674087dfa213be5a78247f0d.tar.gz
gitea-3685cc7660e968f9674087dfa213be5a78247f0d.zip
Fix ldap user sync missed email in email_address table (#18786) (#18876)
* Fix ldap user sync missed email in email_address table (#18786)
Diffstat (limited to 'models')
-rw-r--r--models/user/user.go31
1 files changed, 26 insertions, 5 deletions
diff --git a/models/user/user.go b/models/user/user.go
index 1508b24209..efc9fb2967 100644
--- a/models/user/user.go
+++ b/models/user/user.go
@@ -827,8 +827,9 @@ func validateUser(u *User) error {
return ValidateEmail(u.Email)
}
-func updateUser(ctx context.Context, u *User, changePrimaryEmail bool) error {
- if err := validateUser(u); err != nil {
+func updateUser(ctx context.Context, u *User, changePrimaryEmail bool, cols ...string) error {
+ err := validateUser(u)
+ if err != nil {
return err
}
@@ -860,15 +861,35 @@ func updateUser(ctx context.Context, u *User, changePrimaryEmail bool) error {
}); err != nil {
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
+ }
+
+ if !primaryEmailExist {
+ if _, err = e.Insert(&EmailAddress{
+ Email: u.Email,
+ UID: u.ID,
+ IsActivated: true,
+ IsPrimary: true,
+ }); err != nil {
+ return err
+ }
+ }
}
- _, err := e.ID(u.ID).AllCols().Update(u)
+ if len(cols) == 0 {
+ _, err = e.ID(u.ID).AllCols().Update(u)
+ } else {
+ _, err = e.ID(u.ID).Cols(cols...).Update(u)
+ }
return err
}
// UpdateUser updates user's information.
-func UpdateUser(u *User, emailChanged bool) error {
- return updateUser(db.DefaultContext, u, emailChanged)
+func UpdateUser(u *User, emailChanged bool, cols ...string) error {
+ return updateUser(db.DefaultContext, u, emailChanged, cols...)
}
// UpdateUserCols update user according special columns