Skip to content

Commit

Permalink
fix form validation
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoguang committed May 18, 2023
1 parent 9b3cb10 commit 51ae1b7
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 94 deletions.
21 changes: 19 additions & 2 deletions modules/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,26 @@ type contextKeyType struct{}

var contextKey interface{} = contextKeyType{}

// GetContext retrieves install context from request
func GetContext(req *http.Request) *Context {
return req.Context().Value(contextKey).(*Context)
ctx, _ := req.Context().Value(contextKey).(*Context)
return ctx
}

type ValidateContext struct {
*Base
Locale translation.Locale
}

// GetValidateContext gets a context for middleware form validation
func GetValidateContext(req *http.Request) (ctx *ValidateContext) {
if ctxAPI, ok := req.Context().Value(apiContextKey).(*APIContext); ok {
ctx = &ValidateContext{Base: ctxAPI.Base, Locale: ctxAPI.Base.Locale}
} else if ctxWeb, ok := req.Context().Value(contextKey).(*Context); ok {
ctx = &ValidateContext{Base: ctxWeb.Base, Locale: ctxWeb.Base.Locale}
} else {
panic("invalid context, expect either APIContext or Context")
}
return ctx
}

// Contexter initializes a classic context for a request.
Expand Down
31 changes: 14 additions & 17 deletions modules/context/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"code.gitea.io/gitea/models/perm"
"code.gitea.io/gitea/models/unit"
user_model "code.gitea.io/gitea/models/user"
mc "code.gitea.io/gitea/modules/cache"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/structs"
)
Expand All @@ -30,22 +29,6 @@ type packageAssignmentCtx struct {
ContextUser *user_model.User
}

func PackageContexter() func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
base, baseCleanUp := NewBaseContext(resp, req)
ctx := &Context{
Base: base,
Cache: mc.GetCache(),
}
defer baseCleanUp()

ctx.Base.AppendContextValue(contextKey, ctx)
next.ServeHTTP(ctx.Resp, ctx.Req)
})
}
}

// PackageAssignmentWeb returns a middleware to handle Context.Package assignment
func PackageAssignmentWeb() func(ctx *Context) {
return func(ctx *Context) {
Expand Down Expand Up @@ -153,3 +136,17 @@ func determineAccessMode(ctx *Base, pkg *Package, doer *user_model.User) (perm.A

return accessMode, nil
}

// PackageContexter initializes a package context for a request.
func PackageContexter() func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
base, baseCleanUp := NewBaseContext(resp, req)
ctx := &Context{Base: base}
defer baseCleanUp()

ctx.Base.AppendContextValue(contextKey, ctx)
next.ServeHTTP(ctx.Resp, ctx.Req)
})
}
}
6 changes: 3 additions & 3 deletions services/forms/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type AdminCreateUserForm struct {

// Validate validates form fields
func (f *AdminCreateUserForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetContext(req)
ctx := context.GetValidateContext(req)
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
}

Expand Down Expand Up @@ -55,7 +55,7 @@ type AdminEditUserForm struct {

// Validate validates form fields
func (f *AdminEditUserForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetContext(req)
ctx := context.GetValidateContext(req)
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
}

Expand All @@ -67,6 +67,6 @@ type AdminDashboardForm struct {

// Validate validates form fields
func (f *AdminDashboardForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetContext(req)
ctx := context.GetValidateContext(req)
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
}
2 changes: 1 addition & 1 deletion services/forms/auth_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,6 @@ type AuthenticationForm struct {

// Validate validates fields
func (f *AuthenticationForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetContext(req)
ctx := context.GetValidateContext(req)
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
}
6 changes: 3 additions & 3 deletions services/forms/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type CreateOrgForm struct {

// Validate validates the fields
func (f *CreateOrgForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetContext(req)
ctx := context.GetValidateContext(req)
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
}

Expand All @@ -48,7 +48,7 @@ type UpdateOrgSettingForm struct {

// Validate validates the fields
func (f *UpdateOrgSettingForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetContext(req)
ctx := context.GetValidateContext(req)
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
}

Expand All @@ -70,6 +70,6 @@ type CreateTeamForm struct {

// Validate validates the fields
func (f *CreateTeamForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetContext(req)
ctx := context.GetValidateContext(req)
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
}
2 changes: 1 addition & 1 deletion services/forms/package_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ type PackageCleanupRuleForm struct {
}

func (f *PackageCleanupRuleForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetContext(req)
ctx := context.GetValidateContext(req)
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
}
4 changes: 2 additions & 2 deletions services/forms/repo_branch_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type NewBranchForm struct {

// Validate validates the fields
func (f *NewBranchForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetContext(req)
ctx := context.GetValidateContext(req)
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
}

Expand All @@ -33,6 +33,6 @@ type RenameBranchForm struct {

// Validate validates the fields
func (f *RenameBranchForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetContext(req)
ctx := context.GetValidateContext(req)
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
}
Loading

0 comments on commit 51ae1b7

Please sign in to comment.