Skip to content

Commit 51ae1b7

Browse files
committed
fix form validation
1 parent 9b3cb10 commit 51ae1b7

12 files changed

+108
-94
lines changed

modules/context/context.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,26 @@ type contextKeyType struct{}
7070

7171
var contextKey interface{} = contextKeyType{}
7272

73-
// GetContext retrieves install context from request
7473
func GetContext(req *http.Request) *Context {
75-
return req.Context().Value(contextKey).(*Context)
74+
ctx, _ := req.Context().Value(contextKey).(*Context)
75+
return ctx
76+
}
77+
78+
type ValidateContext struct {
79+
*Base
80+
Locale translation.Locale
81+
}
82+
83+
// GetValidateContext gets a context for middleware form validation
84+
func GetValidateContext(req *http.Request) (ctx *ValidateContext) {
85+
if ctxAPI, ok := req.Context().Value(apiContextKey).(*APIContext); ok {
86+
ctx = &ValidateContext{Base: ctxAPI.Base, Locale: ctxAPI.Base.Locale}
87+
} else if ctxWeb, ok := req.Context().Value(contextKey).(*Context); ok {
88+
ctx = &ValidateContext{Base: ctxWeb.Base, Locale: ctxWeb.Base.Locale}
89+
} else {
90+
panic("invalid context, expect either APIContext or Context")
91+
}
92+
return ctx
7693
}
7794

7895
// Contexter initializes a classic context for a request.

modules/context/package.go

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"code.gitea.io/gitea/models/perm"
1313
"code.gitea.io/gitea/models/unit"
1414
user_model "code.gitea.io/gitea/models/user"
15-
mc "code.gitea.io/gitea/modules/cache"
1615
"code.gitea.io/gitea/modules/setting"
1716
"code.gitea.io/gitea/modules/structs"
1817
)
@@ -30,22 +29,6 @@ type packageAssignmentCtx struct {
3029
ContextUser *user_model.User
3130
}
3231

33-
func PackageContexter() func(next http.Handler) http.Handler {
34-
return func(next http.Handler) http.Handler {
35-
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
36-
base, baseCleanUp := NewBaseContext(resp, req)
37-
ctx := &Context{
38-
Base: base,
39-
Cache: mc.GetCache(),
40-
}
41-
defer baseCleanUp()
42-
43-
ctx.Base.AppendContextValue(contextKey, ctx)
44-
next.ServeHTTP(ctx.Resp, ctx.Req)
45-
})
46-
}
47-
}
48-
4932
// PackageAssignmentWeb returns a middleware to handle Context.Package assignment
5033
func PackageAssignmentWeb() func(ctx *Context) {
5134
return func(ctx *Context) {
@@ -153,3 +136,17 @@ func determineAccessMode(ctx *Base, pkg *Package, doer *user_model.User) (perm.A
153136

154137
return accessMode, nil
155138
}
139+
140+
// PackageContexter initializes a package context for a request.
141+
func PackageContexter() func(next http.Handler) http.Handler {
142+
return func(next http.Handler) http.Handler {
143+
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
144+
base, baseCleanUp := NewBaseContext(resp, req)
145+
ctx := &Context{Base: base}
146+
defer baseCleanUp()
147+
148+
ctx.Base.AppendContextValue(contextKey, ctx)
149+
next.ServeHTTP(ctx.Resp, ctx.Req)
150+
})
151+
}
152+
}

services/forms/admin.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type AdminCreateUserForm struct {
2727

2828
// Validate validates form fields
2929
func (f *AdminCreateUserForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
30-
ctx := context.GetContext(req)
30+
ctx := context.GetValidateContext(req)
3131
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
3232
}
3333

@@ -55,7 +55,7 @@ type AdminEditUserForm struct {
5555

5656
// Validate validates form fields
5757
func (f *AdminEditUserForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
58-
ctx := context.GetContext(req)
58+
ctx := context.GetValidateContext(req)
5959
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
6060
}
6161

@@ -67,6 +67,6 @@ type AdminDashboardForm struct {
6767

6868
// Validate validates form fields
6969
func (f *AdminDashboardForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
70-
ctx := context.GetContext(req)
70+
ctx := context.GetValidateContext(req)
7171
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
7272
}

services/forms/auth_form.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,6 @@ type AuthenticationForm struct {
8686

8787
// Validate validates fields
8888
func (f *AuthenticationForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
89-
ctx := context.GetContext(req)
89+
ctx := context.GetValidateContext(req)
9090
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
9191
}

services/forms/org.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type CreateOrgForm struct {
3030

3131
// Validate validates the fields
3232
func (f *CreateOrgForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
33-
ctx := context.GetContext(req)
33+
ctx := context.GetValidateContext(req)
3434
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
3535
}
3636

@@ -48,7 +48,7 @@ type UpdateOrgSettingForm struct {
4848

4949
// Validate validates the fields
5050
func (f *UpdateOrgSettingForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
51-
ctx := context.GetContext(req)
51+
ctx := context.GetValidateContext(req)
5252
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
5353
}
5454

@@ -70,6 +70,6 @@ type CreateTeamForm struct {
7070

7171
// Validate validates the fields
7272
func (f *CreateTeamForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
73-
ctx := context.GetContext(req)
73+
ctx := context.GetValidateContext(req)
7474
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
7575
}

services/forms/package_form.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ type PackageCleanupRuleForm struct {
2525
}
2626

2727
func (f *PackageCleanupRuleForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
28-
ctx := context.GetContext(req)
28+
ctx := context.GetValidateContext(req)
2929
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
3030
}

services/forms/repo_branch_form.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type NewBranchForm struct {
2121

2222
// Validate validates the fields
2323
func (f *NewBranchForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
24-
ctx := context.GetContext(req)
24+
ctx := context.GetValidateContext(req)
2525
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
2626
}
2727

@@ -33,6 +33,6 @@ type RenameBranchForm struct {
3333

3434
// Validate validates the fields
3535
func (f *RenameBranchForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
36-
ctx := context.GetContext(req)
36+
ctx := context.GetValidateContext(req)
3737
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
3838
}

0 commit comments

Comments
 (0)