diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-11-26 09:56:16 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-25 20:56:16 -0500 |
commit | 371ebce2524f013986401af84732fb27bdbc4f96 (patch) | |
tree | 028d1ee76c82e8254d625cd9ba2784b372745a6a /routers/api | |
parent | f23851f6906acafd5509fb0dcef9866a9aee88ce (diff) | |
download | gitea-371ebce2524f013986401af84732fb27bdbc4f96.tar.gz gitea-371ebce2524f013986401af84732fb27bdbc4f96.zip |
Fix database inconsistent when admin change user email (#17549)
Diffstat (limited to 'routers/api')
-rw-r--r-- | routers/api/v1/admin/user.go | 16 | ||||
-rw-r--r-- | routers/api/v1/user/settings.go | 2 |
2 files changed, 14 insertions, 4 deletions
diff --git a/routers/api/v1/admin/user.go b/routers/api/v1/admin/user.go index e50abb5937..b93c628072 100644 --- a/routers/api/v1/admin/user.go +++ b/routers/api/v1/admin/user.go @@ -9,6 +9,7 @@ import ( "errors" "fmt" "net/http" + "strings" "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" @@ -203,12 +204,21 @@ func EditUser(ctx *context.APIContext) { if form.FullName != nil { u.FullName = *form.FullName } + var emailChanged bool if form.Email != nil { - u.Email = *form.Email - if len(u.Email) == 0 { + email := strings.TrimSpace(*form.Email) + if len(email) == 0 { ctx.Error(http.StatusUnprocessableEntity, "", fmt.Errorf("email is not allowed to be empty string")) return } + + if err := user_model.ValidateEmail(email); err != nil { + ctx.InternalServerError(err) + return + } + + emailChanged = !strings.EqualFold(u.Email, email) + u.Email = email } if form.Website != nil { u.Website = *form.Website @@ -247,7 +257,7 @@ func EditUser(ctx *context.APIContext) { u.IsRestricted = *form.Restricted } - if err := user_model.UpdateUser(u); err != nil { + if err := user_model.UpdateUser(u, emailChanged); err != nil { if user_model.IsErrEmailAlreadyUsed(err) || user_model.IsErrEmailInvalid(err) { ctx.Error(http.StatusUnprocessableEntity, "", err) } else { diff --git a/routers/api/v1/user/settings.go b/routers/api/v1/user/settings.go index 40bee56681..5f4d76ed72 100644 --- a/routers/api/v1/user/settings.go +++ b/routers/api/v1/user/settings.go @@ -74,7 +74,7 @@ func UpdateUserSettings(ctx *context.APIContext) { ctx.User.KeepActivityPrivate = *form.HideActivity } - if err := user_model.UpdateUser(ctx.User); err != nil { + if err := user_model.UpdateUser(ctx.User, false); err != nil { ctx.InternalServerError(err) return } |