diff options
author | Gusted <gusted@noreply.codeberg.org> | 2024-10-21 16:31:32 +0200 |
---|---|---|
committer | Gusted <gusted@noreply.codeberg.org> | 2024-10-21 16:31:32 +0200 |
commit | f298bf125a9d1b3d24d873401621c204a1f17b10 (patch) | |
tree | b8109a82a39bb159991c8548a8310e03ef8b4d79 /routers/api | |
parent | Merge pull request 'feat(ci): allow manual triggering of the test suite' (#56... (diff) | |
parent | fix: use ValidateEmail as binding across web forms (diff) | |
download | forgejo-f298bf125a9d1b3d24d873401621c204a1f17b10.tar.xz forgejo-f298bf125a9d1b3d24d873401621c204a1f17b10.zip |
Merge pull request 'fix: use ValidateEmail as binding across web forms' (#5158) from solomonv/consolidate-email-validation into forgejo
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/5158
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
Diffstat (limited to 'routers/api')
-rw-r--r-- | routers/api/v1/activitypub/repository_test.go | 8 | ||||
-rw-r--r-- | routers/api/v1/admin/user.go | 11 | ||||
-rw-r--r-- | routers/api/v1/user/email.go | 7 |
3 files changed, 14 insertions, 12 deletions
diff --git a/routers/api/v1/activitypub/repository_test.go b/routers/api/v1/activitypub/repository_test.go index acd588d99b..1e5af6acac 100644 --- a/routers/api/v1/activitypub/repository_test.go +++ b/routers/api/v1/activitypub/repository_test.go @@ -6,22 +6,22 @@ package activitypub import ( "testing" - "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/validation" ) func Test_UserEmailValidate(t *testing.T) { sut := "ab@cd.ef" - if err := user.ValidateEmail(sut); err != nil { + if err := validation.ValidateEmail(sut); err != nil { t.Errorf("sut should be valid, %v, %v", sut, err) } sut = "83ce13c8-af0b-4112-8327-55a54e54e664@code.cartoon-aa.xyz" - if err := user.ValidateEmail(sut); err != nil { + if err := validation.ValidateEmail(sut); err != nil { t.Errorf("sut should be valid, %v, %v", sut, err) } sut = "1" - if err := user.ValidateEmail(sut); err == nil { + if err := validation.ValidateEmail(sut); err == nil { t.Errorf("sut should not be valid, %v", sut) } } diff --git a/routers/api/v1/admin/user.go b/routers/api/v1/admin/user.go index 9ea210ee4e..b17200381a 100644 --- a/routers/api/v1/admin/user.go +++ b/routers/api/v1/admin/user.go @@ -20,6 +20,7 @@ import ( "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/timeutil" + "code.gitea.io/gitea/modules/validation" "code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/routers/api/v1/user" "code.gitea.io/gitea/routers/api/v1/utils" @@ -138,8 +139,8 @@ func CreateUser(ctx *context.APIContext) { user_model.IsErrEmailAlreadyUsed(err) || db.IsErrNameReserved(err) || db.IsErrNameCharsNotAllowed(err) || - user_model.IsErrEmailCharIsNotSupported(err) || - user_model.IsErrEmailInvalid(err) || + validation.IsErrEmailCharIsNotSupported(err) || + validation.IsErrEmailInvalid(err) || db.IsErrNamePatternNotAllowed(err) { ctx.Error(http.StatusUnprocessableEntity, "", err) } else { @@ -148,7 +149,7 @@ func CreateUser(ctx *context.APIContext) { return } - if !user_model.IsEmailDomainAllowed(u.Email) { + if !validation.IsEmailDomainAllowed(u.Email) { ctx.Resp.Header().Add("X-Gitea-Warning", fmt.Sprintf("the domain of user email %s conflicts with EMAIL_DOMAIN_ALLOWLIST or EMAIL_DOMAIN_BLOCKLIST", u.Email)) } @@ -224,7 +225,7 @@ func EditUser(ctx *context.APIContext) { if form.Email != nil { if err := user_service.AdminAddOrSetPrimaryEmailAddress(ctx, ctx.ContextUser, *form.Email); err != nil { switch { - case user_model.IsErrEmailCharIsNotSupported(err), user_model.IsErrEmailInvalid(err): + case validation.IsErrEmailCharIsNotSupported(err), validation.IsErrEmailInvalid(err): ctx.Error(http.StatusBadRequest, "EmailInvalid", err) case user_model.IsErrEmailAlreadyUsed(err): ctx.Error(http.StatusBadRequest, "EmailUsed", err) @@ -234,7 +235,7 @@ func EditUser(ctx *context.APIContext) { return } - if !user_model.IsEmailDomainAllowed(*form.Email) { + if !validation.IsEmailDomainAllowed(*form.Email) { ctx.Resp.Header().Add("X-Gitea-Warning", fmt.Sprintf("the domain of user email %s conflicts with EMAIL_DOMAIN_ALLOWLIST or EMAIL_DOMAIN_BLOCKLIST", *form.Email)) } } diff --git a/routers/api/v1/user/email.go b/routers/api/v1/user/email.go index 33aa851a80..1e9d0984df 100644 --- a/routers/api/v1/user/email.go +++ b/routers/api/v1/user/email.go @@ -9,6 +9,7 @@ import ( user_model "code.gitea.io/gitea/models/user" api "code.gitea.io/gitea/modules/structs" + "code.gitea.io/gitea/modules/validation" "code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/services/context" "code.gitea.io/gitea/services/convert" @@ -66,12 +67,12 @@ func AddEmail(ctx *context.APIContext) { if err := user_service.AddEmailAddresses(ctx, ctx.Doer, form.Emails); err != nil { if user_model.IsErrEmailAlreadyUsed(err) { ctx.Error(http.StatusUnprocessableEntity, "", "Email address has been used: "+err.(user_model.ErrEmailAlreadyUsed).Email) - } else if user_model.IsErrEmailCharIsNotSupported(err) || user_model.IsErrEmailInvalid(err) { + } else if validation.IsErrEmailCharIsNotSupported(err) || validation.IsErrEmailInvalid(err) { email := "" - if typedError, ok := err.(user_model.ErrEmailInvalid); ok { + if typedError, ok := err.(validation.ErrEmailInvalid); ok { email = typedError.Email } - if typedError, ok := err.(user_model.ErrEmailCharIsNotSupported); ok { + if typedError, ok := err.(validation.ErrEmailCharIsNotSupported); ok { email = typedError.Email } |