fix(user): unify email validation for registration and settings (#39304)

Co-authored-by: silverwind <me@silverwind.io>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
Abhay Pratap Singh
2026-09-16 17:34:41 +00:00
committed by GitHub
co-authored by silverwind wxiaoguang
parent c6c671e113
commit 7ebb2caa9e
21 changed files with 138 additions and 209 deletions
+5 -17
View File
@@ -4,7 +4,6 @@
package user
import (
"fmt"
"net/http"
user_model "gitea.dev/models/user"
@@ -55,6 +54,10 @@ func AddEmail(ctx *context.APIContext) {
// responses:
// '201':
// "$ref": "#/responses/EmailList"
// "400":
// "$ref": "#/responses/error"
// "409":
// "$ref": "#/responses/error"
// "422":
// "$ref": "#/responses/validationError"
@@ -70,22 +73,7 @@ func AddEmail(ctx *context.APIContext) {
}
if err := user_service.AddEmailAddresses(ctx, ctx.Doer, form.Emails); err != nil {
if errEmailAlreadyUsed, ok := err.(user_model.ErrEmailAlreadyUsed); ok {
ctx.APIError(http.StatusUnprocessableEntity, "Email address has been used: "+errEmailAlreadyUsed.Email)
} else if user_model.IsErrEmailCharIsNotSupported(err) || user_model.IsErrEmailInvalid(err) {
email := ""
if typedError, ok := err.(user_model.ErrEmailInvalid); ok {
email = typedError.Email
}
if typedError, ok := err.(user_model.ErrEmailCharIsNotSupported); ok {
email = typedError.Email
}
errMsg := fmt.Sprintf("Email address %q invalid", email)
ctx.APIError(http.StatusUnprocessableEntity, errMsg)
} else {
ctx.APIErrorInternal(err)
}
ctx.APIErrorAuto(err)
return
}