Browse Source

Only update needed columns when update user (#2296)

* only update needed columns when update user

* fix missing update_unix column
Lunny Xiao 3 years ago
parent
commit
f960e19c59
6 changed files with 45 additions and 17 deletions
  1. 1 1
      cmd/admin.go
  2. 29 4
      models/user.go
  3. 1 1
      routers/api/v1/org/org.go
  4. 10 8
      routers/user/auth.go
  5. 2 1
      routers/user/auth_openid.go
  6. 2 2
      routers/user/setting.go

+ 1 - 1
cmd/admin.go

@@ -103,7 +103,7 @@ func runChangePassword(c *cli.Context) error {
103 103
 		return fmt.Errorf("%v", err)
104 104
 	}
105 105
 	user.EncodePasswd()
106
-	if err := models.UpdateUser(user); err != nil {
106
+	if err := models.UpdateUserCols(user, "passwd", "salt"); err != nil {
107 107
 		return fmt.Errorf("%v", err)
108 108
 	}
109 109
 

+ 29 - 4
models/user.go

@@ -157,7 +157,7 @@ func (u *User) SetLastLogin() {
157 157
 // UpdateDiffViewStyle updates the users diff view style
158 158
 func (u *User) UpdateDiffViewStyle(style string) error {
159 159
 	u.DiffViewStyle = style
160
-	return UpdateUser(u)
160
+	return UpdateUserCols(u, "diff_view_style")
161 161
 }
162 162
 
163 163
 // AfterSet is invoked from XORM after setting the value of a field of this object.
@@ -860,7 +860,9 @@ func updateUser(e Engine, u *User) error {
860 860
 		if len(u.AvatarEmail) == 0 {
861 861
 			u.AvatarEmail = u.Email
862 862
 		}
863
-		u.Avatar = base.HashEmail(u.AvatarEmail)
863
+		if len(u.AvatarEmail) > 0 {
864
+			u.Avatar = base.HashEmail(u.AvatarEmail)
865
+		}
864 866
 	}
865 867
 
866 868
 	u.LowerName = strings.ToLower(u.Name)
@@ -877,6 +879,29 @@ func UpdateUser(u *User) error {
877 879
 	return updateUser(x, u)
878 880
 }
879 881
 
882
+// UpdateUserCols update user according special columns
883
+func UpdateUserCols(u *User, cols ...string) error {
884
+	// Organization does not need email
885
+	u.Email = strings.ToLower(u.Email)
886
+	if !u.IsOrganization() {
887
+		if len(u.AvatarEmail) == 0 {
888
+			u.AvatarEmail = u.Email
889
+		}
890
+		if len(u.AvatarEmail) > 0 {
891
+			u.Avatar = base.HashEmail(u.AvatarEmail)
892
+		}
893
+	}
894
+
895
+	u.LowerName = strings.ToLower(u.Name)
896
+	u.Location = base.TruncateString(u.Location, 255)
897
+	u.Website = base.TruncateString(u.Website, 255)
898
+	u.Description = base.TruncateString(u.Description, 255)
899
+
900
+	cols = append(cols, "updated_unix")
901
+	_, err := x.Id(u.ID).Cols(cols...).Update(u)
902
+	return err
903
+}
904
+
880 905
 // UpdateUserSetting updates user's settings.
881 906
 func UpdateUserSetting(u *User) error {
882 907
 	if !u.IsOrganization() {
@@ -1418,7 +1443,7 @@ func SyncExternalUsers() {
1418 1443
 						}
1419 1444
 						usr.IsActive = true
1420 1445
 
1421
-						err = UpdateUser(usr)
1446
+						err = UpdateUserCols(usr, "full_name", "email", "is_admin", "is_active")
1422 1447
 						if err != nil {
1423 1448
 							log.Error(4, "SyncExternalUsers[%s]: Error updating user %s: %v", s.Name, usr.Name, err)
1424 1449
 						}
@@ -1440,7 +1465,7 @@ func SyncExternalUsers() {
1440 1465
 						log.Trace("SyncExternalUsers[%s]: Deactivating user %s", s.Name, usr.Name)
1441 1466
 
1442 1467
 						usr.IsActive = false
1443
-						err = UpdateUser(&usr)
1468
+						err = UpdateUserCols(&usr, "is_active")
1444 1469
 						if err != nil {
1445 1470
 							log.Error(4, "SyncExternalUsers[%s]: Error deactivating user %s: %v", s.Name, usr.Name, err)
1446 1471
 						}

+ 1 - 1
routers/api/v1/org/org.go

@@ -56,7 +56,7 @@ func Edit(ctx *context.APIContext, form api.EditOrgOption) {
56 56
 	org.Description = form.Description
57 57
 	org.Website = form.Website
58 58
 	org.Location = form.Location
59
-	if err := models.UpdateUser(org); err != nil {
59
+	if err := models.UpdateUserCols(org, "full_name", "description", "website", "location"); err != nil {
60 60
 		ctx.Error(500, "UpdateUser", err)
61 61
 		return
62 62
 	}

+ 10 - 8
routers/user/auth.go

@@ -340,8 +340,8 @@ func handleSignInFull(ctx *context.Context, u *models.User, remember bool, obeyR
340 340
 
341 341
 	// Register last login
342 342
 	u.SetLastLogin()
343
-	if err := models.UpdateUser(u); err != nil {
344
-		ctx.Handle(500, "UpdateUser", err)
343
+	if err := models.UpdateUserCols(u, "last_login_unix"); err != nil {
344
+		ctx.Handle(500, "UpdateUserCols", err)
345 345
 		return
346 346
 	}
347 347
 
@@ -430,8 +430,8 @@ func handleOAuth2SignIn(u *models.User, gothUser goth.User, ctx *context.Context
430 430
 
431 431
 			// Register last login
432 432
 			u.SetLastLogin()
433
-			if err := models.UpdateUser(u); err != nil {
434
-				ctx.Handle(500, "UpdateUser", err)
433
+			if err := models.UpdateUserCols(u, "last_login_unix"); err != nil {
434
+				ctx.Handle(500, "UpdateUserCols", err)
435 435
 				return
436 436
 			}
437 437
 
@@ -666,7 +666,8 @@ func LinkAccountPostRegister(ctx *context.Context, cpt *captcha.Captcha, form au
666 666
 	if models.CountUsers() == 1 {
667 667
 		u.IsAdmin = true
668 668
 		u.IsActive = true
669
-		if err := models.UpdateUser(u); err != nil {
669
+		u.SetLastLogin()
670
+		if err := models.UpdateUserCols(u, "is_admin", "is_active", "last_login_unix"); err != nil {
670 671
 			ctx.Handle(500, "UpdateUser", err)
671 672
 			return
672 673
 		}
@@ -781,7 +782,8 @@ func SignUpPost(ctx *context.Context, cpt *captcha.Captcha, form auth.RegisterFo
781 782
 	if models.CountUsers() == 1 {
782 783
 		u.IsAdmin = true
783 784
 		u.IsActive = true
784
-		if err := models.UpdateUser(u); err != nil {
785
+		u.SetLastLogin()
786
+		if err := models.UpdateUserCols(u, "is_admin", "is_active", "last_login_unix"); err != nil {
785 787
 			ctx.Handle(500, "UpdateUser", err)
786 788
 			return
787 789
 		}
@@ -840,7 +842,7 @@ func Activate(ctx *context.Context) {
840 842
 			ctx.Handle(500, "UpdateUser", err)
841 843
 			return
842 844
 		}
843
-		if err := models.UpdateUser(user); err != nil {
845
+		if err := models.UpdateUserCols(user, "is_active", "rands"); err != nil {
844 846
 			if models.IsErrUserNotExist(err) {
845 847
 				ctx.Error(404)
846 848
 			} else {
@@ -991,7 +993,7 @@ func ResetPasswdPost(ctx *context.Context) {
991 993
 			return
992 994
 		}
993 995
 		u.EncodePasswd()
994
-		if err := models.UpdateUser(u); err != nil {
996
+		if err := models.UpdateUserCols(u, "passwd", "rands", "salt"); err != nil {
995 997
 			ctx.Handle(500, "UpdateUser", err)
996 998
 			return
997 999
 		}

+ 2 - 1
routers/user/auth_openid.go

@@ -404,7 +404,8 @@ func RegisterOpenIDPost(ctx *context.Context, cpt *captcha.Captcha, form auth.Si
404 404
 	if models.CountUsers() == 1 {
405 405
 		u.IsAdmin = true
406 406
 		u.IsActive = true
407
-		if err := models.UpdateUser(u); err != nil {
407
+		u.SetLastLogin()
408
+		if err := models.UpdateUserCols(u, "is_admin", "is_active", "last_login_unix"); err != nil {
408 409
 			ctx.Handle(500, "UpdateUser", err)
409 410
 			return
410 411
 		}

+ 2 - 2
routers/user/setting.go

@@ -156,7 +156,7 @@ func UpdateAvatarSetting(ctx *context.Context, form auth.AvatarForm, ctxUser *mo
156 156
 		}
157 157
 	}
158 158
 
159
-	if err := models.UpdateUser(ctxUser); err != nil {
159
+	if err := models.UpdateUserCols(ctxUser, "avatar", "avatar_email", "use_custom_avatar"); err != nil {
160 160
 		return fmt.Errorf("UpdateUser: %v", err)
161 161
 	}
162 162
 
@@ -221,7 +221,7 @@ func SettingsPasswordPost(ctx *context.Context, form auth.ChangePasswordForm) {
221 221
 			return
222 222
 		}
223 223
 		ctx.User.EncodePasswd()
224
-		if err := models.UpdateUser(ctx.User); err != nil {
224
+		if err := models.UpdateUserCols(ctx.User, "salt", "passwd"); err != nil {
225 225
 			ctx.Handle(500, "UpdateUser", err)
226 226
 			return
227 227
 		}