Fix user primary email changed (#17840)
This commit is contained in:
parent
04517e17d6
commit
d29a0fc3be
6 changed files with 58 additions and 14 deletions
|
@ -9,6 +9,7 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
|
@ -199,12 +200,20 @@ 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 := models.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
|
||||
|
@ -243,7 +252,7 @@ func EditUser(ctx *context.APIContext) {
|
|||
u.IsRestricted = *form.Restricted
|
||||
}
|
||||
|
||||
if err := models.UpdateUser(u); err != nil {
|
||||
if err := models.UpdateUser(u, emailChanged); err != nil {
|
||||
if models.IsErrEmailAlreadyUsed(err) || models.IsErrEmailInvalid(err) {
|
||||
ctx.Error(http.StatusUnprocessableEntity, "", err)
|
||||
} else {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue