diff options
author | Unknwon <joe2010xtmf@163.com> | 2014-10-15 11:19:20 -0400 |
---|---|---|
committer | Unknwon <joe2010xtmf@163.com> | 2014-10-15 11:19:20 -0400 |
commit | fa241efa6d5e934f599e43714e970fa48c9a0f47 (patch) | |
tree | 0a9e74e4849758a5970fd52589e96662bc105600 /modules/auth | |
parent | ecf3eb4307c13c74d928656a140b7bb2ae804fd9 (diff) | |
download | gitea-fa241efa6d5e934f599e43714e970fa48c9a0f47.tar.gz gitea-fa241efa6d5e934f599e43714e970fa48c9a0f47.zip |
Use binding middleware
Diffstat (limited to 'modules/auth')
-rw-r--r-- | modules/auth/admin.go | 7 | ||||
-rw-r--r-- | modules/auth/apiv1/miscellaneous.go | 42 | ||||
-rw-r--r-- | modules/auth/auth.go | 42 | ||||
-rw-r--r-- | modules/auth/auth_form.go | 8 | ||||
-rw-r--r-- | modules/auth/org.go | 16 | ||||
-rw-r--r-- | modules/auth/publickey_form.go | 8 | ||||
-rw-r--r-- | modules/auth/repo_form.go | 44 | ||||
-rw-r--r-- | modules/auth/user_form.go | 24 |
8 files changed, 87 insertions, 104 deletions
diff --git a/modules/auth/admin.go b/modules/auth/admin.go index 1f1260d65a..60ea3a3cc2 100644 --- a/modules/auth/admin.go +++ b/modules/auth/admin.go @@ -6,9 +6,8 @@ package auth import ( "github.com/Unknwon/macaron" - "github.com/macaron-contrib/i18n" - "github.com/gogits/gogs/modules/middleware/binding" + "github.com/macaron-contrib/binding" ) type AdminEditUserForm struct { @@ -22,6 +21,6 @@ type AdminEditUserForm struct { LoginType int `form:"login_type"` } -func (f *AdminEditUserForm) Validate(ctx *macaron.Context, errs *binding.Errors, l i18n.Locale) { - validate(errs, ctx.Data, f, l) +func (f *AdminEditUserForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors { + return validate(errs, ctx.Data, f, ctx.Locale) } diff --git a/modules/auth/apiv1/miscellaneous.go b/modules/auth/apiv1/miscellaneous.go index ea3892977a..71f9541a9c 100644 --- a/modules/auth/apiv1/miscellaneous.go +++ b/modules/auth/apiv1/miscellaneous.go @@ -8,11 +8,9 @@ import ( "reflect" "github.com/Unknwon/macaron" - "github.com/macaron-contrib/i18n" + "github.com/macaron-contrib/binding" "github.com/gogits/gogs/modules/auth" - "github.com/gogits/gogs/modules/log" - "github.com/gogits/gogs/modules/middleware/binding" ) type MarkdownForm struct { @@ -21,18 +19,13 @@ type MarkdownForm struct { Context string `form:"context"` } -func (f *MarkdownForm) Validate(ctx *macaron.Context, errs *binding.Errors, l i18n.Locale) { - validateApiReq(errs, ctx.Data, f, l) +func (f *MarkdownForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors { + return validateApiReq(errs, ctx.Data, f) } -func validateApiReq(errs *binding.Errors, data map[string]interface{}, f interface{}, l i18n.Locale) { - if errs.Count() == 0 { - return - } else if len(errs.Overall) > 0 { - for _, err := range errs.Overall { - log.Error(4, "%s: %v", reflect.TypeOf(f), err) - } - return +func validateApiReq(errs binding.Errors, data map[string]interface{}, f auth.Form) binding.Errors { + if errs.Len() == 0 { + return errs } data["HasError"] = true @@ -54,26 +47,27 @@ func validateApiReq(errs *binding.Errors, data map[string]interface{}, f interfa continue } - if err, ok := errs.Fields[field.Name]; ok { - switch err { - case binding.BindingRequireError: + if errs[0].FieldNames[0] == field.Name { + switch errs[0].Classification { + case binding.RequiredError: data["ErrorMsg"] = fieldName + " cannot be empty" - case binding.BindingAlphaDashError: + case binding.AlphaDashError: data["ErrorMsg"] = fieldName + " must be valid alpha or numeric or dash(-_) characters" - case binding.BindingAlphaDashDotError: + case binding.AlphaDashDotError: data["ErrorMsg"] = fieldName + " must be valid alpha or numeric or dash(-_) or dot characters" - case binding.BindingMinSizeError: + case binding.MinSizeError: data["ErrorMsg"] = fieldName + " must contain at least " + auth.GetMinSize(field) + " characters" - case binding.BindingMaxSizeError: + case binding.MaxSizeError: data["ErrorMsg"] = fieldName + " must contain at most " + auth.GetMaxSize(field) + " characters" - case binding.BindingEmailError: + case binding.EmailError: data["ErrorMsg"] = fieldName + " is not a valid e-mail address" - case binding.BindingUrlError: + case binding.UrlError: data["ErrorMsg"] = fieldName + " is not a valid URL" default: - data["ErrorMsg"] = "Unknown error: " + err + data["ErrorMsg"] = "Unknown error: " + errs[0].Classification } - return + return errs } } + return errs } diff --git a/modules/auth/auth.go b/modules/auth/auth.go index bcd2fcb35d..95e647f53a 100644 --- a/modules/auth/auth.go +++ b/modules/auth/auth.go @@ -9,12 +9,12 @@ import ( "reflect" "strings" - "github.com/macaron-contrib/i18n" + "github.com/Unknwon/macaron" + "github.com/macaron-contrib/binding" "github.com/macaron-contrib/session" "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/log" - "github.com/gogits/gogs/modules/middleware/binding" "github.com/gogits/gogs/modules/setting" ) @@ -69,6 +69,10 @@ func SignedInUser(header http.Header, sess session.Store) *models.User { return u } +type Form interface { + binding.Validator +} + // AssignForm assign form values back to the template data. func AssignForm(form interface{}, data map[string]interface{}) { typ := reflect.TypeOf(form) @@ -109,14 +113,9 @@ func GetMaxSize(field reflect.StructField) string { return getSize(field, "MaxSize(") } -func validate(errs *binding.Errors, data map[string]interface{}, f interface{}, l i18n.Locale) { - if errs.Count() == 0 { - return - } else if len(errs.Overall) > 0 { - for _, err := range errs.Overall { - log.Error(4, "%s: %v", reflect.TypeOf(f), err) - } - return +func validate(errs binding.Errors, data map[string]interface{}, f Form, l macaron.Locale) binding.Errors { + if errs.Len() == 0 { + return errs } data["HasError"] = true @@ -139,28 +138,29 @@ func validate(errs *binding.Errors, data map[string]interface{}, f interface{}, continue } - if err, ok := errs.Fields[field.Name]; ok { + if errs[0].FieldNames[0] == field.Name { data["Err_"+field.Name] = true trName := l.Tr("form." + field.Name) - switch err { - case binding.BindingRequireError: + switch errs[0].Classification { + case binding.RequiredError: data["ErrorMsg"] = trName + l.Tr("form.require_error") - case binding.BindingAlphaDashError: + case binding.AlphaDashError: data["ErrorMsg"] = trName + l.Tr("form.alpha_dash_error") - case binding.BindingAlphaDashDotError: + case binding.AlphaDashDotError: data["ErrorMsg"] = trName + l.Tr("form.alpha_dash_dot_error") - case binding.BindingMinSizeError: + case binding.MinSizeError: data["ErrorMsg"] = trName + l.Tr("form.min_size_error", GetMinSize(field)) - case binding.BindingMaxSizeError: + case binding.MaxSizeError: data["ErrorMsg"] = trName + l.Tr("form.max_size_error", GetMaxSize(field)) - case binding.BindingEmailError: + case binding.EmailError: data["ErrorMsg"] = trName + l.Tr("form.email_error") - case binding.BindingUrlError: + case binding.UrlError: data["ErrorMsg"] = trName + l.Tr("form.url_error") default: - data["ErrorMsg"] = l.Tr("form.unknown_error") + " " + err + data["ErrorMsg"] = l.Tr("form.unknown_error") + " " + errs[0].Classification } - return + return errs } } + return errs } diff --git a/modules/auth/auth_form.go b/modules/auth/auth_form.go index cb9da5dff4..e9789634c9 100644 --- a/modules/auth/auth_form.go +++ b/modules/auth/auth_form.go @@ -6,9 +6,7 @@ package auth import ( "github.com/Unknwon/macaron" - "github.com/macaron-contrib/i18n" - - "github.com/gogits/gogs/modules/middleware/binding" + "github.com/macaron-contrib/binding" ) type AuthenticationForm struct { @@ -31,6 +29,6 @@ type AuthenticationForm struct { AllowAutoRegister bool `form:"allowautoregister"` } -func (f *AuthenticationForm) Validate(ctx *macaron.Context, errs *binding.Errors, l i18n.Locale) { - validate(errs, ctx.Data, f, l) +func (f *AuthenticationForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors { + return validate(errs, ctx.Data, f, ctx.Locale) } diff --git a/modules/auth/org.go b/modules/auth/org.go index 6183e8c826..3e6c55c182 100644 --- a/modules/auth/org.go +++ b/modules/auth/org.go @@ -6,9 +6,7 @@ package auth import ( "github.com/Unknwon/macaron" - "github.com/macaron-contrib/i18n" - - "github.com/gogits/gogs/modules/middleware/binding" + "github.com/macaron-contrib/binding" ) // ________ .__ __ .__ @@ -23,8 +21,8 @@ type CreateOrgForm struct { Email string `form:"email" binding:"Required;Email;MaxSize(50)"` } -func (f *CreateOrgForm) Validate(ctx *macaron.Context, errs *binding.Errors, l i18n.Locale) { - validate(errs, ctx.Data, f, l) +func (f *CreateOrgForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors { + return validate(errs, ctx.Data, f, ctx.Locale) } type UpdateOrgSettingForm struct { @@ -37,8 +35,8 @@ type UpdateOrgSettingForm struct { Avatar string `form:"avatar" binding:"Required;Email;MaxSize(50)"` } -func (f *UpdateOrgSettingForm) Validate(ctx *macaron.Context, errs *binding.Errors, l i18n.Locale) { - validate(errs, ctx.Data, f, l) +func (f *UpdateOrgSettingForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors { + return validate(errs, ctx.Data, f, ctx.Locale) } // ___________ @@ -54,6 +52,6 @@ type CreateTeamForm struct { Permission string `form:"permission"` } -func (f *CreateTeamForm) Validate(ctx *macaron.Context, errs *binding.Errors, l i18n.Locale) { - validate(errs, ctx.Data, f, l) +func (f *CreateTeamForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors { + return validate(errs, ctx.Data, f, ctx.Locale) } diff --git a/modules/auth/publickey_form.go b/modules/auth/publickey_form.go index 4618a7ea36..5a1d44c04b 100644 --- a/modules/auth/publickey_form.go +++ b/modules/auth/publickey_form.go @@ -6,9 +6,7 @@ package auth import ( "github.com/Unknwon/macaron" - "github.com/macaron-contrib/i18n" - - "github.com/gogits/gogs/modules/middleware/binding" + "github.com/macaron-contrib/binding" ) type AddSSHKeyForm struct { @@ -16,6 +14,6 @@ type AddSSHKeyForm struct { Content string `form:"content" binding:"Required"` } -func (f *AddSSHKeyForm) Validate(ctx *macaron.Context, errs *binding.Errors, l i18n.Locale) { - validate(errs, ctx.Data, f, l) +func (f *AddSSHKeyForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors { + return validate(errs, ctx.Data, f, ctx.Locale) } diff --git a/modules/auth/repo_form.go b/modules/auth/repo_form.go index df5b8b69fb..86fbdc4926 100644 --- a/modules/auth/repo_form.go +++ b/modules/auth/repo_form.go @@ -6,9 +6,7 @@ package auth import ( "github.com/Unknwon/macaron" - "github.com/macaron-contrib/i18n" - - "github.com/gogits/gogs/modules/middleware/binding" + "github.com/macaron-contrib/binding" ) // _______________________________________ _________.______________________ _______________.___. @@ -28,8 +26,8 @@ type CreateRepoForm struct { InitReadme bool `form:"init_readme"` } -func (f *CreateRepoForm) Validate(ctx *macaron.Context, errs *binding.Errors, l i18n.Locale) { - validate(errs, ctx.Data, f, l) +func (f *CreateRepoForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors { + return validate(errs, ctx.Data, f, ctx.Locale) } type MigrateRepoForm struct { @@ -43,8 +41,8 @@ type MigrateRepoForm struct { Description string `form:"desc" binding:"MaxSize(255)"` } -func (f *MigrateRepoForm) Validate(ctx *macaron.Context, errs *binding.Errors, l i18n.Locale) { - validate(errs, ctx.Data, f, l) +func (f *MigrateRepoForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors { + return validate(errs, ctx.Data, f, ctx.Locale) } type RepoSettingForm struct { @@ -57,8 +55,8 @@ type RepoSettingForm struct { GoGet bool `form:"goget"` } -func (f *RepoSettingForm) Validate(ctx *macaron.Context, errs *binding.Errors, l i18n.Locale) { - validate(errs, ctx.Data, f, l) +func (f *RepoSettingForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors { + return validate(errs, ctx.Data, f, ctx.Locale) } // __ __ ___. .__ .__ __ @@ -77,8 +75,8 @@ type NewWebhookForm struct { Active bool `form:"active"` } -func (f *NewWebhookForm) Validate(ctx *macaron.Context, errs *binding.Errors, l i18n.Locale) { - validate(errs, ctx.Data, f, l) +func (f *NewWebhookForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors { + return validate(errs, ctx.Data, f, ctx.Locale) } type NewSlackHookForm struct { @@ -90,8 +88,8 @@ type NewSlackHookForm struct { Active bool `form:"active"` } -func (f *NewSlackHookForm) Validate(ctx *macaron.Context, errs *binding.Errors, l i18n.Locale) { - validate(errs, ctx.Data, f, l) +func (f *NewSlackHookForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors { + return validate(errs, ctx.Data, f, ctx.Locale) } // .___ @@ -109,8 +107,8 @@ type CreateIssueForm struct { Content string `form:"content"` } -func (f *CreateIssueForm) Validate(ctx *macaron.Context, errs *binding.Errors, l i18n.Locale) { - validate(errs, ctx.Data, f, l) +func (f *CreateIssueForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors { + return validate(errs, ctx.Data, f, ctx.Locale) } // _____ .__.__ __ @@ -126,8 +124,8 @@ type CreateMilestoneForm struct { Deadline string `form:"due_date"` } -func (f *CreateMilestoneForm) Validate(ctx *macaron.Context, errs *binding.Errors, l i18n.Locale) { - validate(errs, ctx.Data, f, l) +func (f *CreateMilestoneForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors { + return validate(errs, ctx.Data, f, ctx.Locale) } // .____ ___. .__ @@ -142,8 +140,8 @@ type CreateLabelForm struct { Color string `form:"color" binding:"Required;Size(7)"` } -func (f *CreateLabelForm) Validate(ctx *macaron.Context, errs *binding.Errors, l i18n.Locale) { - validate(errs, ctx.Data, f, l) +func (f *CreateLabelForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors { + return validate(errs, ctx.Data, f, ctx.Locale) } // __________ .__ @@ -162,8 +160,8 @@ type NewReleaseForm struct { Prerelease bool `form:"prerelease"` } -func (f *NewReleaseForm) Validate(ctx *macaron.Context, errs *binding.Errors, l i18n.Locale) { - validate(errs, ctx.Data, f, l) +func (f *NewReleaseForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors { + return validate(errs, ctx.Data, f, ctx.Locale) } type EditReleaseForm struct { @@ -174,6 +172,6 @@ type EditReleaseForm struct { Prerelease bool `form:"prerelease"` } -func (f *EditReleaseForm) Validate(ctx *macaron.Context, errs *binding.Errors, l i18n.Locale) { - validate(errs, ctx.Data, f, l) +func (f *EditReleaseForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors { + return validate(errs, ctx.Data, f, ctx.Locale) } diff --git a/modules/auth/user_form.go b/modules/auth/user_form.go index 93bd01a9fd..72bdd4589f 100644 --- a/modules/auth/user_form.go +++ b/modules/auth/user_form.go @@ -6,9 +6,7 @@ package auth import ( "github.com/Unknwon/macaron" - "github.com/macaron-contrib/i18n" - - "github.com/gogits/gogs/modules/middleware/binding" + "github.com/macaron-contrib/binding" ) type InstallForm struct { @@ -34,8 +32,8 @@ type InstallForm struct { AdminEmail string `form:"admin_email" binding:"Required;Email;MaxSize(50)"` } -func (f *InstallForm) Validate(ctx *macaron.Context, errs *binding.Errors, l i18n.Locale) { - validate(errs, ctx.Data, f, l) +func (f *InstallForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors { + return validate(errs, ctx.Data, f, ctx.Locale) } // _____ ____ _________________ ___ @@ -54,8 +52,8 @@ type RegisterForm struct { LoginName string `form:"loginname"` } -func (f *RegisterForm) Validate(ctx *macaron.Context, errs *binding.Errors, l i18n.Locale) { - validate(errs, ctx.Data, f, l) +func (f *RegisterForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors { + return validate(errs, ctx.Data, f, ctx.Locale) } type SignInForm struct { @@ -64,8 +62,8 @@ type SignInForm struct { Remember bool `form:"remember"` } -func (f *SignInForm) Validate(ctx *macaron.Context, errs *binding.Errors, l i18n.Locale) { - validate(errs, ctx.Data, f, l) +func (f *SignInForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors { + return validate(errs, ctx.Data, f, ctx.Locale) } // __________________________________________.___ _______ ________ _________ @@ -84,8 +82,8 @@ type UpdateProfileForm struct { Avatar string `form:"avatar" binding:"Required;Email;MaxSize(50)"` } -func (f *UpdateProfileForm) Validate(ctx *macaron.Context, errs *binding.Errors, l i18n.Locale) { - validate(errs, ctx.Data, f, l) +func (f *UpdateProfileForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors { + return validate(errs, ctx.Data, f, ctx.Locale) } type ChangePasswordForm struct { @@ -94,6 +92,6 @@ type ChangePasswordForm struct { Retype string `form:"retype"` } -func (f *ChangePasswordForm) Validate(ctx *macaron.Context, errs *binding.Errors, l i18n.Locale) { - validate(errs, ctx.Data, f, l) +func (f *ChangePasswordForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors { + return validate(errs, ctx.Data, f, ctx.Locale) } |