diff options
author | Unknwon <joe2010xtmf@163.com> | 2014-07-26 00:24:27 -0400 |
---|---|---|
committer | Unknwon <joe2010xtmf@163.com> | 2014-07-26 00:24:27 -0400 |
commit | 8dd07c0ddd99ae626a1ec8c06f75f27fed51269f (patch) | |
tree | 261d3c9911dabc58c1ac54e4e36b3dee24d2032b /modules/auth | |
parent | 0a739cf9ac901f54484c34bba8322418dedb09b0 (diff) | |
download | gitea-8dd07c0ddd99ae626a1ec8c06f75f27fed51269f.tar.gz gitea-8dd07c0ddd99ae626a1ec8c06f75f27fed51269f.zip |
New UI merge in progress
Diffstat (limited to 'modules/auth')
-rw-r--r-- | modules/auth/admin.go | 22 | ||||
-rw-r--r-- | modules/auth/apiv1/miscellaneous.go | 14 | ||||
-rw-r--r-- | modules/auth/auth.go | 175 | ||||
-rw-r--r-- | modules/auth/auth_form.go | 36 | ||||
-rw-r--r-- | modules/auth/ldap/ldap.go | 2 | ||||
-rw-r--r-- | modules/auth/org.go | 49 | ||||
-rw-r--r-- | modules/auth/publickey.go | 33 | ||||
-rw-r--r-- | modules/auth/publickey_form.go | 21 | ||||
-rw-r--r-- | modules/auth/repo.go | 252 | ||||
-rw-r--r-- | modules/auth/repo_form.go | 165 | ||||
-rw-r--r-- | modules/auth/user.go | 299 | ||||
-rw-r--r-- | modules/auth/user_form.go | 98 |
12 files changed, 478 insertions, 688 deletions
diff --git a/modules/auth/admin.go b/modules/auth/admin.go index 69161f908a..38f3292b88 100644 --- a/modules/auth/admin.go +++ b/modules/auth/admin.go @@ -5,12 +5,9 @@ package auth import ( - "net/http" - "reflect" + "github.com/Unknwon/macaron" + "github.com/macaron-contrib/i18n" - "github.com/go-martini/martini" - - "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/middleware/binding" ) @@ -25,17 +22,6 @@ type AdminEditUserForm struct { LoginType int `form:"login_type"` } -func (f *AdminEditUserForm) Name(field string) string { - names := map[string]string{ - "Email": "E-mail address", - "Website": "Website", - "Location": "Location", - "Avatar": "Gravatar Email", - } - return names[field] -} - -func (f *AdminEditUserForm) Validate(errors *binding.Errors, req *http.Request, context martini.Context) { - data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData) - validate(errors, data, f) +func (f *AdminEditUserForm) Validate(ctx *macaron.Context, errs *binding.Errors, l i18n.Locale) { + validate(errs, ctx.Data, f, l) } diff --git a/modules/auth/apiv1/miscellaneous.go b/modules/auth/apiv1/miscellaneous.go index 726a080147..30515eb861 100644 --- a/modules/auth/apiv1/miscellaneous.go +++ b/modules/auth/apiv1/miscellaneous.go @@ -5,13 +5,12 @@ package apiv1 import ( - "net/http" "reflect" - "github.com/go-martini/martini" + "github.com/Unknwon/macaron" + "github.com/macaron-contrib/i18n" "github.com/gogits/gogs/modules/auth" - "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/middleware/binding" ) @@ -22,17 +21,16 @@ type MarkdownForm struct { Context string `form:"context"` } -func (f *MarkdownForm) Validate(errs *binding.Errors, req *http.Request, ctx martini.Context) { - data := ctx.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData) - validateApiReq(errs, data, f) +func (f *MarkdownForm) Validate(ctx *macaron.Context, errs *binding.Errors, l i18n.Locale) { + validateApiReq(errs, ctx.Data, f, l) } -func validateApiReq(errs *binding.Errors, data base.TmplData, f interface{}) { +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("%s: %v", reflect.TypeOf(f), err) + log.Error(4, "%s: %v", reflect.TypeOf(f), err) } return } diff --git a/modules/auth/auth.go b/modules/auth/auth.go index e9b215101b..2236de4761 100644 --- a/modules/auth/auth.go +++ b/modules/auth/auth.go @@ -7,49 +7,152 @@ package auth import ( "net/http" "reflect" + "strings" - "github.com/go-martini/martini" + "github.com/macaron-contrib/i18n" + "github.com/macaron-contrib/session" - "github.com/gogits/gogs/modules/base" + "github.com/gogits/gogs/models" + "github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/middleware/binding" + "github.com/gogits/gogs/modules/setting" ) -type AuthenticationForm struct { - Id int64 `form:"id"` - Type int `form:"type"` - AuthName string `form:"name" binding:"Required;MaxSize(50)"` - Domain string `form:"domain"` - Host string `form:"host"` - Port int `form:"port"` - UseSSL bool `form:"usessl"` - BaseDN string `form:"base_dn"` - Attributes string `form:"attributes"` - Filter string `form:"filter"` - MsAdSA string `form:"ms_ad_sa"` - IsActived bool `form:"is_actived"` - SmtpAuth string `form:"smtpauth"` - SmtpHost string `form:"smtphost"` - SmtpPort int `form:"smtpport"` - Tls bool `form:"tls"` - AllowAutoRegister bool `form:"allowautoregister"` +// SignedInId returns the id of signed in user. +func SignedInId(header http.Header, sess session.Store) int64 { + if !models.HasEngine { + return 0 + } + + if setting.Service.EnableReverseProxyAuth { + webAuthUser := header.Get(setting.ReverseProxyAuthUser) + if len(webAuthUser) > 0 { + u, err := models.GetUserByName(webAuthUser) + if err != nil { + if err != models.ErrUserNotExist { + log.Error(4, "GetUserByName: %v", err) + } + return 0 + } + return u.Id + } + } + + uid := sess.Get("uid") + if uid == nil { + return 0 + } + if id, ok := uid.(int64); ok { + if _, err := models.GetUserById(id); err != nil { + if err != models.ErrUserNotExist { + log.Error(4, "GetUserById: %v", err) + } + return 0 + } + return id + } + return 0 } -func (f *AuthenticationForm) Name(field string) string { - names := map[string]string{ - "AuthName": "Authentication's name", - "Domain": "Domain name", - "Host": "Host address", - "Port": "Port Number", - "UseSSL": "Use SSL", - "BaseDN": "Base DN", - "Attributes": "Search attributes", - "Filter": "Search filter", - "MsAdSA": "Ms Ad SA", - } - return names[field] +// SignedInUser returns the user object of signed user. +func SignedInUser(header http.Header, sess session.Store) *models.User { + uid := SignedInId(header, sess) + if uid <= 0 { + return nil + } + + u, err := models.GetUserById(uid) + if err != nil { + log.Error(4, "GetUserById: %v", err) + return nil + } + return u } -func (f *AuthenticationForm) Validate(errors *binding.Errors, req *http.Request, context martini.Context) { - data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData) - validate(errors, data, f) +// AssignForm assign form values back to the template data. +func AssignForm(form interface{}, data map[string]interface{}) { + typ := reflect.TypeOf(form) + val := reflect.ValueOf(form) + + if typ.Kind() == reflect.Ptr { + typ = typ.Elem() + val = val.Elem() + } + + for i := 0; i < typ.NumField(); i++ { + field := typ.Field(i) + + fieldName := field.Tag.Get("form") + // Allow ignored fields in the struct + if fieldName == "-" { + continue + } + + data[fieldName] = val.Field(i).Interface() + } +} + +func GetMinMaxSize(field reflect.StructField) string { + for _, rule := range strings.Split(field.Tag.Get("binding"), ";") { + if strings.HasPrefix(rule, "MinSize(") || strings.HasPrefix(rule, "MaxSize(") { + return rule[8 : len(rule)-1] + } + } + return "" +} + +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 + } + + data["HasError"] = true + AssignForm(f, data) + + typ := reflect.TypeOf(f) + val := reflect.ValueOf(f) + + if typ.Kind() == reflect.Ptr { + typ = typ.Elem() + val = val.Elem() + } + + for i := 0; i < typ.NumField(); i++ { + field := typ.Field(i) + + fieldName := field.Tag.Get("form") + // Allow ignored fields in the struct + if fieldName == "-" { + continue + } + + if err, ok := errs.Fields[field.Name]; ok { + data["Err_"+field.Name] = true + trName := l.Tr("form." + field.Name) + switch err { + case binding.BindingRequireError: + data["ErrorMsg"] = trName + l.Tr("form.require_error") + case binding.BindingAlphaDashError: + data["ErrorMsg"] = trName + l.Tr("form.alpha_dash_error") + case binding.BindingAlphaDashDotError: + data["ErrorMsg"] = trName + l.Tr("form.alpha_dash_dot_error") + case binding.BindingMinSizeError: + data["ErrorMsg"] = trName + l.Tr("form.min_size_error", GetMinMaxSize(field)) + case binding.BindingMaxSizeError: + data["ErrorMsg"] = trName + l.Tr("form.max_size_error", GetMinMaxSize(field)) + case binding.BindingEmailError: + data["ErrorMsg"] = trName + l.Tr("form.email_error") + case binding.BindingUrlError: + data["ErrorMsg"] = trName + l.Tr("form.url_error") + default: + data["ErrorMsg"] = l.Tr("form.unknown_error") + " " + err + } + return + } + } } diff --git a/modules/auth/auth_form.go b/modules/auth/auth_form.go new file mode 100644 index 0000000000..cb9da5dff4 --- /dev/null +++ b/modules/auth/auth_form.go @@ -0,0 +1,36 @@ +// Copyright 2014 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package auth + +import ( + "github.com/Unknwon/macaron" + "github.com/macaron-contrib/i18n" + + "github.com/gogits/gogs/modules/middleware/binding" +) + +type AuthenticationForm struct { + Id int64 `form:"id"` + Type int `form:"type"` + AuthName string `form:"name" binding:"Required;MaxSize(50)"` + Domain string `form:"domain"` + Host string `form:"host"` + Port int `form:"port"` + UseSSL bool `form:"usessl"` + BaseDN string `form:"base_dn"` + Attributes string `form:"attributes"` + Filter string `form:"filter"` + MsAdSA string `form:"ms_ad_sa"` + IsActived bool `form:"is_actived"` + SmtpAuth string `form:"smtpauth"` + SmtpHost string `form:"smtphost"` + SmtpPort int `form:"smtpport"` + Tls bool `form:"tls"` + AllowAutoRegister bool `form:"allowautoregister"` +} + +func (f *AuthenticationForm) Validate(ctx *macaron.Context, errs *binding.Errors, l i18n.Locale) { + validate(errs, ctx.Data, f, l) +} diff --git a/modules/auth/ldap/ldap.go b/modules/auth/ldap/ldap.go index e944a90a1e..e27e5133ed 100644 --- a/modules/auth/ldap/ldap.go +++ b/modules/auth/ldap/ldap.go @@ -55,7 +55,7 @@ func LoginUser(name, passwd string) (a string, r bool) { func (ls Ldapsource) SearchEntry(name, passwd string) (string, bool) { l, err := ldapDial(ls) if err != nil { - log.Error("LDAP Connect error, %s:%v", ls.Host, err) + log.Error(4, "LDAP Connect error, %s:%v", ls.Host, err) ls.Enabled = false return "", false } diff --git a/modules/auth/org.go b/modules/auth/org.go index e243627ef2..c45d83a711 100644 --- a/modules/auth/org.go +++ b/modules/auth/org.go @@ -5,12 +5,9 @@ package auth import ( - "net/http" - "reflect" + "github.com/Unknwon/macaron" + "github.com/macaron-contrib/i18n" - "github.com/go-martini/martini" - - "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/middleware/binding" ) @@ -26,17 +23,8 @@ type CreateOrgForm struct { Email string `form:"email" binding:"Required;Email;MaxSize(50)"` } -func (f *CreateOrgForm) Name(field string) string { - names := map[string]string{ - "OrgName": "Organization name", - "Email": "E-mail address", - } - return names[field] -} - -func (f *CreateOrgForm) Validate(errs *binding.Errors, req *http.Request, ctx martini.Context) { - data := ctx.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData) - validate(errs, data, f) +func (f *CreateOrgForm) Validate(ctx *macaron.Context, errs *binding.Errors, l i18n.Locale) { + validate(errs, ctx.Data, f, l) } type OrgSettingForm struct { @@ -47,20 +35,8 @@ type OrgSettingForm struct { Location string `form:"location" binding:"MaxSize(50)"` } -func (f *OrgSettingForm) Name(field string) string { - names := map[string]string{ - "DisplayName": "Display name", - "Email": "E-mail address", - "Description": "Description", - "Website": "Website address", - "Location": "Location", - } - return names[field] -} - -func (f *OrgSettingForm) Validate(errors *binding.Errors, req *http.Request, context martini.Context) { - data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData) - validate(errors, data, f) +func (f *OrgSettingForm) Validate(ctx *macaron.Context, errs *binding.Errors, l i18n.Locale) { + validate(errs, ctx.Data, f, l) } // ___________ @@ -76,15 +52,6 @@ type CreateTeamForm struct { Permission string `form:"permission"` } -func (f *CreateTeamForm) Name(field string) string { - names := map[string]string{ - "TeamName": "Team name", - "Description": "Team description", - } - return names[field] -} - -func (f *CreateTeamForm) Validate(errs *binding.Errors, req *http.Request, ctx martini.Context) { - data := ctx.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData) - validate(errs, data, f) +func (f *CreateTeamForm) Validate(ctx *macaron.Context, errs *binding.Errors, l i18n.Locale) { + validate(errs, ctx.Data, f, l) } diff --git a/modules/auth/publickey.go b/modules/auth/publickey.go deleted file mode 100644 index b828c92b57..0000000000 --- a/modules/auth/publickey.go +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright 2014 The Gogs Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package auth - -import ( - "net/http" - "reflect" - - "github.com/go-martini/martini" - - "github.com/gogits/gogs/modules/base" - "github.com/gogits/gogs/modules/middleware/binding" -) - -type AddSSHKeyForm struct { - KeyName string `form:"keyname" binding:"Required"` - KeyContent string `form:"key_content" binding:"Required"` -} - -func (f *AddSSHKeyForm) Name(field string) string { - names := map[string]string{ - "KeyName": "SSH key name", - "KeyContent": "SSH key content", - } - return names[field] -} - -func (f *AddSSHKeyForm) Validate(errors *binding.Errors, req *http.Request, context martini.Context) { - data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData) - validate(errors, data, f) -} diff --git a/modules/auth/publickey_form.go b/modules/auth/publickey_form.go new file mode 100644 index 0000000000..4618a7ea36 --- /dev/null +++ b/modules/auth/publickey_form.go @@ -0,0 +1,21 @@ +// Copyright 2014 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package auth + +import ( + "github.com/Unknwon/macaron" + "github.com/macaron-contrib/i18n" + + "github.com/gogits/gogs/modules/middleware/binding" +) + +type AddSSHKeyForm struct { + SSHTitle string `form:"title" binding:"Required"` + 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) +} diff --git a/modules/auth/repo.go b/modules/auth/repo.go deleted file mode 100644 index d3d215322a..0000000000 --- a/modules/auth/repo.go +++ /dev/null @@ -1,252 +0,0 @@ -// Copyright 2014 The Gogs Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package auth - -import ( - "net/http" - "reflect" - - "github.com/go-martini/martini" - - "github.com/gogits/gogs/modules/base" - "github.com/gogits/gogs/modules/middleware/binding" -) - -// __________ .__ __ -// \______ \ ____ ______ ____ _____|__|/ |_ ___________ ___.__. -// | _// __ \\____ \ / _ \/ ___/ \ __\/ _ \_ __ < | | -// | | \ ___/| |_> > <_> )___ \| || | ( <_> ) | \/\___ | -// |____|_ /\___ > __/ \____/____ >__||__| \____/|__| / ____| -// \/ \/|__| \/ \/ - -type CreateRepoForm struct { - Uid int64 `form:"uid" binding:"Required"` - RepoName string `form:"repo" binding:"Required;AlphaDash;MaxSize(100)"` - Private bool `form:"private"` - Description string `form:"desc" binding:"MaxSize(255)"` - Language string `form:"language"` - License string `form:"license"` - InitReadme bool `form:"initReadme"` -} - -func (f *CreateRepoForm) Name(field string) string { - names := map[string]string{ - "RepoName": "Repository name", - "Description": "Description", - } - return names[field] -} - -func (f *CreateRepoForm) Validate(errors *binding.Errors, req *http.Request, context martini.Context) { - data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData) - validate(errors, data, f) -} - -type MigrateRepoForm struct { - Url string `form:"url" binding:"Url"` - AuthUserName string `form:"auth_username"` - AuthPasswd string `form:"auth_password"` - Uid int64 `form:"uid" binding:"Required"` - RepoName string `form:"repo" binding:"Required;AlphaDash;MaxSize(100)"` - Mirror bool `form:"mirror"` - Private bool `form:"private"` - Description string `form:"desc" binding:"MaxSize(255)"` -} - -func (f *MigrateRepoForm) Name(field string) string { - names := map[string]string{ - "Url": "Migration URL", - "RepoName": "Repository name", - "Description": "Description", - } - return names[field] -} - -func (f *MigrateRepoForm) Validate(errors *binding.Errors, req *http.Request, context martini.Context) { - data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData) - validate(errors, data, f) -} - -type RepoSettingForm struct { - RepoName string `form:"name" binding:"Required;AlphaDash;MaxSize(100)"` - Description string `form:"desc" binding:"MaxSize(255)"` - Website string `form:"site" binding:"Url;MaxSize(100)"` - Branch string `form:"branch"` - Interval int `form:"interval"` - Private bool `form:"private"` - GoGet bool `form:"goget"` -} - -func (f *RepoSettingForm) Name(field string) string { - names := map[string]string{ - "RepoName": "Repository name", - "Description": "Description", - "Website": "Website address", - } - return names[field] -} - -func (f *RepoSettingForm) Validate(errors *binding.Errors, req *http.Request, context martini.Context) { - data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData) - validate(errors, data, f) -} - -// __ __ ___. .__ .__ __ -// / \ / \ ____\_ |__ | |__ | |__ ____ | | __ -// \ \/\/ // __ \| __ \| | \| | \ / _ \| |/ / -// \ /\ ___/| \_\ \ Y \ Y ( <_> ) < -// \__/\ / \___ >___ /___| /___| /\____/|__|_ \ -// \/ \/ \/ \/ \/ \/ - -type NewWebhookForm struct { - Url string `form:"url" binding:"Required;Url"` - ContentType string `form:"content_type" binding:"Required"` - Secret string `form:"secret""` - PushOnly bool `form:"push_only"` - Active bool `form:"active"` -} - -func (f *NewWebhookForm) Name(field string) string { - names := map[string]string{ - "Url": "Payload URL", - "ContentType": "Content type", - } - return names[field] -} - -func (f *NewWebhookForm) Validate(errors *binding.Errors, req *http.Request, context martini.Context) { - data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData) - validate(errors, data, f) -} - -// .___ -// | | ______ ________ __ ____ -// | |/ ___// ___/ | \_/ __ \ -// | |\___ \ \___ \| | /\ ___/ -// |___/____ >____ >____/ \___ > -// \/ \/ \/ - -type CreateIssueForm struct { - IssueName string `form:"title" binding:"Required;MaxSize(50)"` - MilestoneId int64 `form:"milestoneid"` - AssigneeId int64 `form:"assigneeid"` - Labels string `form:"labels"` - Content string `form:"content"` -} - -func (f *CreateIssueForm) Name(field string) string { - names := map[string]string{ - "IssueName": "Issue name", - } - return names[field] -} - -func (f *CreateIssueForm) Validate(errors *binding.Errors, req *http.Request, context martini.Context) { - data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData) - validate(errors, data, f) -} - -// _____ .__.__ __ -// / \ |__| | ____ _______/ |_ ____ ____ ____ -// / \ / \| | | _/ __ \ / ___/\ __\/ _ \ / \_/ __ \ -// / Y \ | |_\ ___/ \___ \ | | ( <_> ) | \ ___/ -// \____|__ /__|____/\___ >____ > |__| \____/|___| /\___ > -// \/ \/ \/ \/ \/ - -type CreateMilestoneForm struct { - Title string `form:"title" binding:"Required;MaxSize(50)"` - Content string `form:"content"` - Deadline string `form:"due_date"` -} - -func (f *CreateMilestoneForm) Name(field string) string { - names := map[string]string{ - "Title": "Milestone name", - } - return names[field] -} - -func (f *CreateMilestoneForm) Validate(errors *binding.Errors, req *http.Request, context martini.Context) { - data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData) - validate(errors, data, f) -} - -// .____ ___. .__ -// | | _____ \_ |__ ____ | | -// | | \__ \ | __ \_/ __ \| | -// | |___ / __ \| \_\ \ ___/| |__ -// |_______ (____ /___ /\___ >____/ -// \/ \/ \/ \/ - -type CreateLabelForm struct { - Title string `form:"title" binding:"Required;MaxSize(50)"` - Color string `form:"color" binding:"Required;Size(7)"` -} - -func (f *CreateLabelForm) Name(field string) string { - names := map[string]string{ - "Title": "Label name", - "Color": "Label color", - } - return names[field] -} - -func (f *CreateLabelForm) Validate(errors *binding.Errors, req *http.Request, context martini.Context) { - data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData) - validate(errors, data, f) -} - -// __________ .__ -// \______ \ ____ | | ____ _____ ______ ____ -// | _// __ \| | _/ __ \\__ \ / ___// __ \ -// | | \ ___/| |_\ ___/ / __ \_\___ \\ ___/ -// |____|_ /\___ >____/\___ >____ /____ >\___ > -// \/ \/ \/ \/ \/ \/ - -type NewReleaseForm struct { - TagName string `form:"tag_name" binding:"Required"` - Target string `form:"tag_target" binding:"Required"` - Title string `form:"title" binding:"Required"` - Content string `form:"content" binding:"Required"` - Draft string `form:"draft"` - Prerelease bool `form:"prerelease"` -} - -func (f *NewReleaseForm) Name(field string) string { - names := map[string]string{ - "TagName": "Tag name", - "Target": "Target", - "Title": "Release title", - "Content": "Release content", - } - return names[field] -} - -func (f *NewReleaseForm) Validate(errors *binding.Errors, req *http.Request, context martini.Context) { - data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData) - validate(errors, data, f) -} - -type EditReleaseForm struct { - Target string `form:"tag_target" binding:"Required"` - Title string `form:"title" binding:"Required"` - Content string `form:"content" binding:"Required"` - Draft string `form:"draft"` - Prerelease bool `form:"prerelease"` -} - -func (f *EditReleaseForm) Name(field string) string { - names := map[string]string{ - "Target": "Target", - "Title": "Release title", - "Content": "Release content", - } - return names[field] -} - -func (f *EditReleaseForm) Validate(errors *binding.Errors, req *http.Request, context martini.Context) { - data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData) - validate(errors, data, f) -} diff --git a/modules/auth/repo_form.go b/modules/auth/repo_form.go new file mode 100644 index 0000000000..11efb24d1c --- /dev/null +++ b/modules/auth/repo_form.go @@ -0,0 +1,165 @@ +// Copyright 2014 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package auth + +import ( + "github.com/Unknwon/macaron" + "github.com/macaron-contrib/i18n" + + "github.com/gogits/gogs/modules/middleware/binding" +) + +// _______________________________________ _________.______________________ _______________.___. +// \______ \_ _____/\______ \_____ \ / _____/| \__ ___/\_____ \\______ \__ | | +// | _/| __)_ | ___// | \ \_____ \ | | | | / | \| _// | | +// | | \| \ | | / | \/ \| | | | / | \ | \\____ | +// |____|_ /_______ / |____| \_______ /_______ /|___| |____| \_______ /____|_ // ______| +// \/ \/ \/ \/ \/ \/ \/ + +type CreateRepoForm struct { + Uid int64 `form:"uid" binding:"Required"` + RepoName string `form:"repo_name" binding:"Required;AlphaDash;MaxSize(100)"` + Private bool `form:"private"` + Description string `form:"desc" binding:"MaxSize(255)"` + Gitignore string `form:"gitignore"` + License string `form:"license"` + InitReadme bool `form:"init_readme"` +} + +func (f *CreateRepoForm) Validate(ctx *macaron.Context, errs *binding.Errors, l i18n.Locale) { + validate(errs, ctx.Data, f, l) +} + +type MigrateRepoForm struct { + Url string `form:"url" binding:"Url"` + AuthUserName string `form:"auth_username"` + AuthPasswd string `form:"auth_password"` + Uid int64 `form:"uid" binding:"Required"` + RepoName string `form:"repo" binding:"Required;AlphaDash;MaxSize(100)"` + Mirror bool `form:"mirror"` + Private bool `form:"private"` + 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) +} + +type RepoSettingForm struct { + RepoName string `form:"name" binding:"Required;AlphaDash;MaxSize(100)"` + Description string `form:"desc" binding:"MaxSize(255)"` + Website string `form:"site" binding:"Url;MaxSize(100)"` + Branch string `form:"branch"` + Interval int `form:"interval"` + Private bool `form:"private"` + GoGet bool `form:"goget"` +} + +func (f *RepoSettingForm) Validate(ctx *macaron.Context, errs *binding.Errors, l i18n.Locale) { + validate(errs, ctx.Data, f, l) +} + +// __ __ ___. .__ .__ __ +// / \ / \ ____\_ |__ | |__ | |__ ____ | | __ +// \ \/\/ // __ \| __ \| | \| | \ / _ \| |/ / +// \ /\ ___/| \_\ \ Y \ Y ( <_> ) < +// \__/\ / \___ >___ /___| /___| /\____/|__|_ \ +// \/ \/ \/ \/ \/ \/ + +type NewWebhookForm struct { + Url string `form:"url" binding:"Required;Url"` + ContentType string `form:"content_type" binding:"Required"` + Secret string `form:"secret""` + PushOnly bool `form:"push_only"` + Active bool `form:"active"` +} + +func (f *NewWebhookForm) Validate(ctx *macaron.Context, errs *binding.Errors, l i18n.Locale) { + validate(errs, ctx.Data, f, l) +} + +// .___ +// | | ______ ________ __ ____ +// | |/ ___// ___/ | \_/ __ \ +// | |\___ \ \___ \| | /\ ___/ +// |___/____ >____ >____/ \___ > +// \/ \/ \/ + +type CreateIssueForm struct { + IssueName string `form:"title" binding:"Required;MaxSize(50)"` + MilestoneId int64 `form:"milestoneid"` + AssigneeId int64 `form:"assigneeid"` + Labels string `form:"labels"` + Content string `form:"content"` +} + +func (f *CreateIssueForm) Validate(ctx *macaron.Context, errs *binding.Errors, l i18n.Locale) { + validate(errs, ctx.Data, f, l) +} + +// _____ .__.__ __ +// / \ |__| | ____ _______/ |_ ____ ____ ____ +// / \ / \| | | _/ __ \ / ___/\ __\/ _ \ / \_/ __ \ +// / Y \ | |_\ ___/ \___ \ | | ( <_> ) | \ ___/ +// \____|__ /__|____/\___ >____ > |__| \____/|___| /\___ > +// \/ \/ \/ \/ \/ + +type CreateMilestoneForm struct { + Title string `form:"title" binding:"Required;MaxSize(50)"` + Content string `form:"content"` + Deadline string `form:"due_date"` +} + +func (f *CreateMilestoneForm) Validate(ctx *macaron.Context, errs *binding.Errors, l i18n.Locale) { + validate(errs, ctx.Data, f, l) +} + +// .____ ___. .__ +// | | _____ \_ |__ ____ | | +// | | \__ \ | __ \_/ __ \| | +// | |___ / __ \| \_\ \ ___/| |__ +// |_______ (____ /___ /\___ >____/ +// \/ \/ \/ \/ + +type CreateLabelForm struct { + Title string `form:"title" binding:"Required;MaxSize(50)"` + 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) +} + +// __________ .__ +// \______ \ ____ | | ____ _____ ______ ____ +// | _// __ \| | _/ __ \\__ \ / ___// __ \ +// | | \ ___/| |_\ ___/ / __ \_\___ \\ ___/ +// |____|_ /\___ >____/\___ >____ /____ >\___ > +// \/ \/ \/ \/ \/ \/ + +type NewReleaseForm struct { + TagName string `form:"tag_name" binding:"Required"` + Target string `form:"tag_target" binding:"Required"` + Title string `form:"title" binding:"Required"` + Content string `form:"content" binding:"Required"` + Draft string `form:"draft"` + Prerelease bool `form:"prerelease"` +} + +func (f *NewReleaseForm) Validate(ctx *macaron.Context, errs *binding.Errors, l i18n.Locale) { + validate(errs, ctx.Data, f, l) +} + +type EditReleaseForm struct { + Target string `form:"tag_target" binding:"Required"` + Title string `form:"title" binding:"Required"` + Content string `form:"content" binding:"Required"` + Draft string `form:"draft"` + Prerelease bool `form:"prerelease"` +} + +func (f *EditReleaseForm) Validate(ctx *macaron.Context, errs *binding.Errors, l i18n.Locale) { + validate(errs, ctx.Data, f, l) +} diff --git a/modules/auth/user.go b/modules/auth/user.go deleted file mode 100644 index dfb969e816..0000000000 --- a/modules/auth/user.go +++ /dev/null @@ -1,299 +0,0 @@ -// Copyright 2014 The Gogs Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package auth - -import ( - "net/http" - "reflect" - "strings" - - "github.com/go-martini/martini" - - "github.com/gogits/session" - - "github.com/gogits/gogs/models" - "github.com/gogits/gogs/modules/base" - "github.com/gogits/gogs/modules/log" - "github.com/gogits/gogs/modules/middleware/binding" - "github.com/gogits/gogs/modules/setting" -) - -// Web form interface. -type Form interface { - Name(field string) string -} - -type RegisterForm struct { - UserName string `form:"username" binding:"Required;AlphaDashDot;MaxSize(30)"` - Email string `form:"email" binding:"Required;Email;MaxSize(50)"` - Password string `form:"passwd" binding:"Required;MinSize(6);MaxSize(30)"` - RetypePasswd string `form:"retypepasswd"` - LoginType string `form:"logintype"` - LoginName string `form:"loginname"` -} - -func (f *RegisterForm) Name(field string) string { - names := map[string]string{ - "UserName": "Username", - "Email": "E-mail address", - "Password": "Password", - "RetypePasswd": "Re-type password", - } - return names[field] -} - -func (f *RegisterForm) Validate(errs *binding.Errors, req *http.Request, ctx martini.Context) { - data := ctx.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData) - validate(errs, data, f) -} - -type LogInForm struct { - UserName string `form:"username" binding:"Required;MaxSize(35)"` - Password string `form:"passwd" binding:"Required;MinSize(6);MaxSize(30)"` - Remember bool `form:"remember"` -} - -func (f *LogInForm) Name(field string) string { - names := map[string]string{ - "UserName": "Username", - "Password": "Password", - } - return names[field] -} - -func (f *LogInForm) Validate(errs *binding.Errors, req *http.Request, ctx martini.Context) { - data := ctx.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData) - validate(errs, data, f) -} - -func GetMinMaxSize(field reflect.StructField) string { - for _, rule := range strings.Split(field.Tag.Get("binding"), ";") { - if strings.HasPrefix(rule, "MinSize(") || strings.HasPrefix(rule, "MaxSize(") { - return rule[8 : len(rule)-1] - } - } - return "" -} - -func validate(errs *binding.Errors, data base.TmplData, f Form) { - if errs.Count() == 0 { - return - } else if len(errs.Overall) > 0 { - for _, err := range errs.Overall { - log.Error("%s: %v", reflect.TypeOf(f), err) - } - return - } - - data["HasError"] = true - AssignForm(f, data) - - typ := reflect.TypeOf(f) - val := reflect.ValueOf(f) - - if typ.Kind() == reflect.Ptr { - typ = typ.Elem() - val = val.Elem() - } - - for i := 0; i < typ.NumField(); i++ { - field := typ.Field(i) - - fieldName := field.Tag.Get("form") - // Allow ignored fields in the struct - if fieldName == "-" { - continue - } - - if err, ok := errs.Fields[field.Name]; ok { - data["Err_"+field.Name] = true - switch err { - case binding.BindingRequireError: - data["ErrorMsg"] = f.Name(field.Name) + " cannot be empty" - case binding.BindingAlphaDashError: - data["ErrorMsg"] = f.Name(field.Name) + " must be valid alpha or numeric or dash(-_) characters" - case binding.BindingAlphaDashDotError: - data["ErrorMsg"] = f.Name(field.Name) + " must be valid alpha or numeric or dash(-_) or dot characters" - case binding.BindingMinSizeError: - data["ErrorMsg"] = f.Name(field.Name) + " must contain at least " + GetMinMaxSize(field) + " characters" - case binding.BindingMaxSizeError: - data["ErrorMsg"] = f.Name(field.Name) + " must contain at most " + GetMinMaxSize(field) + " characters" - case binding.BindingEmailError: - data["ErrorMsg"] = f.Name(field.Name) + " is not a valid e-mail address" - case binding.BindingUrlError: - data["ErrorMsg"] = f.Name(field.Name) + " is not a valid URL" - default: - data["ErrorMsg"] = "Unknown error: " + err - } - return - } - } -} - -// AssignForm assign form values back to the template data. -func AssignForm(form interface{}, data base.TmplData) { - typ := reflect.TypeOf(form) - val := reflect.ValueOf(form) - - if typ.Kind() == reflect.Ptr { - typ = typ.Elem() - val = val.Elem() - } - - for i := 0; i < typ.NumField(); i++ { - field := typ.Field(i) - - fieldName := field.Tag.Get("form") - // Allow ignored fields in the struct - if fieldName == "-" { - continue - } - - data[fieldName] = val.Field(i).Interface() - } -} - -type InstallForm struct { - Database string `form:"database" binding:"Required"` - Host string `form:"host"` - User string `form:"user"` - Passwd string `form:"passwd"` - DatabaseName string `form:"database_name"` - SslMode string `form:"ssl_mode"` - DatabasePath string `form:"database_path"` - RepoRootPath string `form:"repo_path"` - RunUser string `form:"run_user"` - Domain string `form:"domain"` - AppUrl string `form:"app_url"` - AdminName string `form:"admin_name" binding:"Required;AlphaDashDot;MaxSize(30)"` - AdminPasswd string `form:"admin_pwd" binding:"Required;MinSize(6);MaxSize(30)"` - AdminEmail string `form:"admin_email" binding:"Required;Email;MaxSize(50)"` - SmtpHost string `form:"smtp_host"` - SmtpEmail string `form:"mailer_user"` - SmtpPasswd string `form:"mailer_pwd"` - RegisterConfirm string `form:"register_confirm"` - MailNotify string `form:"mail_notify"` -} - -func (f *InstallForm) Name(field string) string { - names := map[string]string{ - "Database": "Database name", - "AdminName": "Admin user name", - "AdminPasswd": "Admin password", - "AdminEmail": "Admin e-maill address", - } - return names[field] -} - -func (f *InstallForm) Validate(errors *binding.Errors, req *http.Request, context martini.Context) { - data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData) - validate(errors, data, f) -} - -// SignedInId returns the id of signed in user. -func SignedInId(header http.Header, sess session.SessionStore) int64 { - if !models.HasEngine { - return 0 - } - - if setting.Service.EnableReverseProxyAuth { - webAuthUser := header.Get(setting.ReverseProxyAuthUser) - if len(webAuthUser) > 0 { - u, err := models.GetUserByName(webAuthUser) - if err != nil { - if err != models.ErrUserNotExist { - log.Error("auth.user.SignedInId(GetUserByName): %v", err) - } - return 0 - } - return u.Id - } - } - - uid := sess.Get("userId") - if uid == nil { - return 0 - } - if id, ok := uid.(int64); ok { - if _, err := models.GetUserById(id); err != nil { - if err != models.ErrUserNotExist { - log.Error("auth.user.SignedInId(GetUserById): %v", err) - } - return 0 - } - return id - } - return 0 -} - -// SignedInUser returns the user object of signed user. -func SignedInUser(header http.Header, sess session.SessionStore) *models.User { - uid := SignedInId(header, sess) - if uid <= 0 { - return nil - } - - u, err := models.GetUserById(uid) - if err != nil { - log.Error("user.SignedInUser: %v", err) - return nil - } - return u -} - -// IsSignedIn check if any user has signed in. -func IsSignedIn(header http.Header, sess session.SessionStore) bool { - return SignedInId(header, sess) > 0 -} - -type FeedsForm struct { - UserId int64 `form:"userid" binding:"Required"` - Page int64 `form:"p"` -} - -type UpdateProfileForm struct { - UserName string `form:"username" binding:"Required;AlphaDash;MaxSize(30)"` - FullName string `form:"fullname" binding:"MaxSize(40)"` - Email string `form:"email" binding:"Required;Email;MaxSize(50)"` - Website string `form:"website" binding:"Url;MaxSize(50)"` - Location string `form:"location" binding:"MaxSize(50)"` - Avatar string `form:"avatar" binding:"Required;Email;MaxSize(50)"` -} - -func (f *UpdateProfileForm) Name(field string) string { - names := map[string]string{ - "UserName": "Username", - "Email": "E-mail address", - "Website": "Website address", - "Location": "Location", - "Avatar": "Gravatar Email", - } - return names[field] -} - -func (f *UpdateProfileForm) Validate(errs *binding.Errors, req *http.Request, ctx martini.Context) { - data := ctx.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData) - validate(errs, data, f) -} - -type UpdatePasswdForm struct { - OldPasswd string `form:"oldpasswd" binding:"Required;MinSize(6);MaxSize(30)"` - NewPasswd string `form:"newpasswd" binding:"Required;MinSize(6);MaxSize(30)"` - RetypePasswd string `form:"retypepasswd"` -} - -func (f *UpdatePasswdForm) Name(field string) string { - names := map[string]string{ - "OldPasswd": "Old password", - "NewPasswd": "New password", - "RetypePasswd": "Re-type password", - } - return names[field] -} - -func (f *UpdatePasswdForm) Validate(errs *binding.Errors, req *http.Request, ctx martini.Context) { - data := ctx.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData) - validate(errs, data, f) -} diff --git a/modules/auth/user_form.go b/modules/auth/user_form.go new file mode 100644 index 0000000000..30080e859b --- /dev/null +++ b/modules/auth/user_form.go @@ -0,0 +1,98 @@ +// Copyright 2014 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package auth + +import ( + "github.com/Unknwon/macaron" + "github.com/macaron-contrib/i18n" + + "github.com/gogits/gogs/modules/middleware/binding" +) + +type InstallForm struct { + Database string `form:"database" binding:"Required"` + Host string `form:"host"` + User string `form:"user"` + Passwd string `form:"passwd"` + DatabaseName string `form:"database_name"` + SslMode string `form:"ssl_mode"` + DatabasePath string `form:"database_path"` + RepoRootPath string `form:"repo_path"` + RunUser string `form:"run_user"` + Domain string `form:"domain"` + AppUrl string `form:"app_url"` + AdminName string `form:"admin_name" binding:"Required;AlphaDashDot;MaxSize(30)"` + AdminPasswd string `form:"admin_pwd" binding:"Required;MinSize(6);MaxSize(30)"` + AdminEmail string `form:"admin_email" binding:"Required;Email;MaxSize(50)"` + SmtpHost string `form:"smtp_host"` + SmtpEmail string `form:"mailer_user"` + SmtpPasswd string `form:"mailer_pwd"` + RegisterConfirm string `form:"register_confirm"` + MailNotify string `form:"mail_notify"` +} + +func (f *InstallForm) Validate(ctx *macaron.Context, errs *binding.Errors, l i18n.Locale) { + validate(errs, ctx.Data, f, l) +} + +// _____ ____ _________________ ___ +// / _ \ | | \__ ___/ | \ +// / /_\ \| | / | | / ~ \ +// / | \ | / | | \ Y / +// \____|__ /______/ |____| \___|_ / +// \/ \/ + +type RegisterForm struct { + UserName string `form:"uname" binding:"Required;AlphaDashDot;MaxSize(35)"` + Email string `form:"email" binding:"Required;Email;MaxSize(50)"` + Password string `form:"password" binding:"Required;MinSize(6);MaxSize(30)"` + Retype string `form:"retype"` + LoginType string `form:"logintype"` + LoginName string `form:"loginname"` +} + +func (f *RegisterForm) Validate(ctx *macaron.Context, errs *binding.Errors, l i18n.Locale) { + validate(errs, ctx.Data, f, l) +} + +type SignInForm struct { + UserName string `form:"uname" binding:"Required;MaxSize(35)"` + Password string `form:"password" binding:"Required;MinSize(6);MaxSize(30)"` + Remember bool `form:"remember"` +} + +func (f *SignInForm) Validate(ctx *macaron.Context, errs *binding.Errors, l i18n.Locale) { + validate(errs, ctx.Data, f, l) +} + +// __________________________________________.___ _______ ________ _________ +// / _____/\_ _____/\__ ___/\__ ___/| |\ \ / _____/ / _____/ +// \_____ \ | __)_ | | | | | |/ | \/ \ ___ \_____ \ +// / \ | \ | | | | | / | \ \_\ \/ \ +// /_______ //_______ / |____| |____| |___\____|__ /\______ /_______ / +// \/ \/ \/ \/ \/ + +type UpdateProfileForm struct { + UserName string `form:"uname" binding:"Required;MaxSize(35)"` + FullName string `form:"fullname" binding:"MaxSize(40)"` + Email string `form:"email" binding:"Required;Email;MaxSize(50)"` + Website string `form:"website" binding:"Url;MaxSize(50)"` + Location string `form:"location" binding:"MaxSize(50)"` + 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) +} + +type ChangePasswordForm struct { + OldPassword string `form:"old_password" binding:"Required;MinSize(6);MaxSize(30)"` + Password string `form:"password" binding:"Required;MinSize(6);MaxSize(30)"` + Retype string `form:"retype"` +} + +func (f *ChangePasswordForm) Validate(ctx *macaron.Context, errs *binding.Errors, l i18n.Locale) { + validate(errs, ctx.Data, f, l) +} |