summaryrefslogtreecommitdiffstats
path: root/models/user
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2022-02-24 14:13:59 +0800
committerGitHub <noreply@github.com>2022-02-24 14:13:59 +0800
commitf1b1472632e36c3bb5df73f184a6efeef47f5d01 (patch)
treeeace871525841fc31c07ec6fd3b194650a174359 /models/user
parentcb41f5cae16c5c11cad717c0d19333c2aa4e3b55 (diff)
downloadgitea-f1b1472632e36c3bb5df73f184a6efeef47f5d01.tar.gz
gitea-f1b1472632e36c3bb5df73f184a6efeef47f5d01.zip
Fix ldap user sync missed email in email_address table (#18786)
* Fix ldap user sync missed email in email_address table * Fix test
Diffstat (limited to 'models/user')
-rw-r--r--models/user/user.go30
1 files changed, 25 insertions, 5 deletions
diff --git a/models/user/user.go b/models/user/user.go
index b61ffd7072..d0f1d9d747 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,34 @@ 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 {
+ _, err = e.Insert(&EmailAddress{
+ Email: u.Email,
+ UID: u.ID,
+ IsActivated: true,
+ IsPrimary: true,
+ })
+ 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