summaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
authorUnknown <joe2010xtmf@163.com>2014-05-05 16:21:43 -0400
committerUnknown <joe2010xtmf@163.com>2014-05-05 16:21:43 -0400
commitbbdfe2576966210cfffc830bfbe3731bcf653b3b (patch)
treebc0fc83c71887270c4f38d53be8383985ffd01c5 /modules
parentc1eb4d894a092aed1b87ddf5f80ee824fd56789d (diff)
downloadgitea-bbdfe2576966210cfffc830bfbe3731bcf653b3b.tar.gz
gitea-bbdfe2576966210cfffc830bfbe3731bcf653b3b.zip
User code clean and ui improve
Diffstat (limited to 'modules')
-rw-r--r--modules/auth/apiv1/miscellaneous.go24
-rw-r--r--modules/auth/auth.go76
-rw-r--r--modules/auth/setting.go21
-rw-r--r--modules/auth/user.go40
-rw-r--r--modules/base/template.go17
5 files changed, 62 insertions, 116 deletions
diff --git a/modules/auth/apiv1/miscellaneous.go b/modules/auth/apiv1/miscellaneous.go
index c34bdfa43b..4d0d25659d 100644
--- a/modules/auth/apiv1/miscellaneous.go
+++ b/modules/auth/apiv1/miscellaneous.go
@@ -22,19 +22,12 @@ type MarkdownForm struct {
Context string `form:"context"`
}
-func (f *MarkdownForm) Name(field string) string {
- names := map[string]string{
- "Text": "text",
- }
- return names[field]
-}
-
func (f *MarkdownForm) Validate(errs *binding.BindingErrors, req *http.Request, ctx martini.Context) {
data := ctx.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
validateApiReq(errs, data, f)
}
-func validateApiReq(errs *binding.BindingErrors, data base.TmplData, f auth.Form) {
+func validateApiReq(errs *binding.BindingErrors, data base.TmplData, f interface{}) {
if errs.Count() == 0 {
return
} else if len(errs.Overall) > 0 {
@@ -64,22 +57,21 @@ func validateApiReq(errs *binding.BindingErrors, data base.TmplData, f auth.Form
}
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"
+ data["ErrorMsg"] = fieldName + " cannot be empty"
case binding.BindingAlphaDashError:
- data["ErrorMsg"] = f.Name(field.Name) + " must be valid alpha or numeric or dash(-_) characters"
+ data["ErrorMsg"] = fieldName + " 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"
+ data["ErrorMsg"] = fieldName + " must be valid alpha or numeric or dash(-_) or dot characters"
case binding.BindingMinSizeError:
- data["ErrorMsg"] = f.Name(field.Name) + " must contain at least " + auth.GetMinMaxSize(field) + " characters"
+ data["ErrorMsg"] = fieldName + " must contain at least " + auth.GetMinMaxSize(field) + " characters"
case binding.BindingMaxSizeError:
- data["ErrorMsg"] = f.Name(field.Name) + " must contain at most " + auth.GetMinMaxSize(field) + " characters"
+ data["ErrorMsg"] = fieldName + " must contain at most " + auth.GetMinMaxSize(field) + " characters"
case binding.BindingEmailError:
- data["ErrorMsg"] = f.Name(field.Name) + " is not a valid e-mail address"
+ data["ErrorMsg"] = fieldName + " is not a valid e-mail address"
case binding.BindingUrlError:
- data["ErrorMsg"] = f.Name(field.Name) + " is not a valid URL"
+ data["ErrorMsg"] = fieldName + " is not a valid URL"
default:
data["ErrorMsg"] = "Unknown error: " + err
}
diff --git a/modules/auth/auth.go b/modules/auth/auth.go
index 62728acceb..a0e00c10c5 100644
--- a/modules/auth/auth.go
+++ b/modules/auth/auth.go
@@ -39,29 +39,15 @@ func (f *RegisterForm) Name(field string) string {
return names[field]
}
-func (f *RegisterForm) Validate(errors *binding.BindingErrors, req *http.Request, context martini.Context) {
- if req.Method == "GET" || errors.Count() == 0 {
- return
- }
-
- data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
- data["HasError"] = true
- AssignForm(f, data)
-
- if len(errors.Overall) > 0 {
- for _, err := range errors.Overall {
- log.Error("RegisterForm.Validate: %v", err)
- }
- return
- }
-
- validate(errors, data, f)
+func (f *RegisterForm) Validate(errs *binding.BindingErrors, 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 string `form:"remember"`
+ Remember bool `form:"remember"`
}
func (f *LogInForm) Name(field string) string {
@@ -72,23 +58,9 @@ func (f *LogInForm) Name(field string) string {
return names[field]
}
-func (f *LogInForm) Validate(errors *binding.BindingErrors, req *http.Request, context martini.Context) {
- if req.Method == "GET" || errors.Count() == 0 {
- return
- }
-
- data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
- data["HasError"] = true
- AssignForm(f, data)
-
- if len(errors.Overall) > 0 {
- for _, err := range errors.Overall {
- log.Error("LogInForm.Validate: %v", err)
- }
- return
- }
-
- validate(errors, data, f)
+func (f *LogInForm) Validate(errs *binding.BindingErrors, 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 {
@@ -100,9 +72,21 @@ func GetMinMaxSize(field reflect.StructField) string {
return ""
}
-func validate(errors *binding.BindingErrors, data base.TmplData, form Form) {
- typ := reflect.TypeOf(form)
- val := reflect.ValueOf(form)
+func validate(errs *binding.BindingErrors, 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()
@@ -118,23 +102,23 @@ func validate(errors *binding.BindingErrors, data base.TmplData, form Form) {
continue
}
- if err, ok := errors.Fields[field.Name]; ok {
+ if err, ok := errs.Fields[field.Name]; ok {
data["Err_"+field.Name] = true
switch err {
case binding.BindingRequireError:
- data["ErrorMsg"] = form.Name(field.Name) + " cannot be empty"
+ data["ErrorMsg"] = f.Name(field.Name) + " cannot be empty"
case binding.BindingAlphaDashError:
- data["ErrorMsg"] = form.Name(field.Name) + " must be valid alpha or numeric or dash(-_) characters"
+ data["ErrorMsg"] = f.Name(field.Name) + " must be valid alpha or numeric or dash(-_) characters"
case binding.BindingAlphaDashDotError:
- data["ErrorMsg"] = form.Name(field.Name) + " must be valid alpha or numeric or dash(-_) or dot characters"
+ data["ErrorMsg"] = f.Name(field.Name) + " must be valid alpha or numeric or dash(-_) or dot characters"
case binding.BindingMinSizeError:
- data["ErrorMsg"] = form.Name(field.Name) + " must contain at least " + GetMinMaxSize(field) + " characters"
+ data["ErrorMsg"] = f.Name(field.Name) + " must contain at least " + GetMinMaxSize(field) + " characters"
case binding.BindingMaxSizeError:
- data["ErrorMsg"] = form.Name(field.Name) + " must contain at most " + GetMinMaxSize(field) + " characters"
+ data["ErrorMsg"] = f.Name(field.Name) + " must contain at most " + GetMinMaxSize(field) + " characters"
case binding.BindingEmailError:
- data["ErrorMsg"] = form.Name(field.Name) + " is not a valid e-mail address"
+ data["ErrorMsg"] = f.Name(field.Name) + " is not a valid e-mail address"
case binding.BindingUrlError:
- data["ErrorMsg"] = form.Name(field.Name) + " is not a valid URL"
+ data["ErrorMsg"] = f.Name(field.Name) + " is not a valid URL"
default:
data["ErrorMsg"] = "Unknown error: " + err
}
diff --git a/modules/auth/setting.go b/modules/auth/setting.go
index 3316d7b9bc..7bae2451fb 100644
--- a/modules/auth/setting.go
+++ b/modules/auth/setting.go
@@ -7,12 +7,10 @@ package auth
import (
"net/http"
"reflect"
- "strings"
"github.com/go-martini/martini"
"github.com/gogits/gogs/modules/base"
- "github.com/gogits/gogs/modules/log"
"github.com/gogits/gogs/modules/middleware/binding"
)
@@ -31,24 +29,5 @@ func (f *AddSSHKeyForm) Name(field string) string {
func (f *AddSSHKeyForm) Validate(errors *binding.BindingErrors, req *http.Request, context martini.Context) {
data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
- AssignForm(f, data)
-
- if req.Method == "GET" || errors.Count() == 0 {
- if req.Method == "POST" &&
- (len(f.KeyContent) < 100 || !strings.HasPrefix(f.KeyContent, "ssh-rsa")) {
- data["HasError"] = true
- data["ErrorMsg"] = "SSH key content is not valid"
- }
- return
- }
-
- data["HasError"] = true
- if len(errors.Overall) > 0 {
- for _, err := range errors.Overall {
- log.Error("AddSSHKeyForm.Validate: %v", err)
- }
- return
- }
-
validate(errors, data, f)
}
diff --git a/modules/auth/user.go b/modules/auth/user.go
index 0e591398c0..bc3216fe36 100644
--- a/modules/auth/user.go
+++ b/modules/auth/user.go
@@ -78,7 +78,7 @@ 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:"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)"`
}
@@ -94,22 +94,9 @@ func (f *UpdateProfileForm) Name(field string) string {
return names[field]
}
-func (f *UpdateProfileForm) Validate(errors *binding.BindingErrors, req *http.Request, context martini.Context) {
- if req.Method == "GET" || errors.Count() == 0 {
- return
- }
-
- data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
- data["HasError"] = true
-
- if len(errors.Overall) > 0 {
- for _, err := range errors.Overall {
- log.Error("UpdateProfileForm.Validate: %v", err)
- }
- return
- }
-
- validate(errors, data, f)
+func (f *UpdateProfileForm) Validate(errs *binding.BindingErrors, req *http.Request, ctx martini.Context) {
+ data := ctx.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
+ validate(errs, data, f)
}
type UpdatePasswdForm struct {
@@ -127,20 +114,7 @@ func (f *UpdatePasswdForm) Name(field string) string {
return names[field]
}
-func (f *UpdatePasswdForm) Validate(errors *binding.BindingErrors, req *http.Request, context martini.Context) {
- if req.Method == "GET" || errors.Count() == 0 {
- return
- }
-
- data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
- data["HasError"] = true
-
- if len(errors.Overall) > 0 {
- for _, err := range errors.Overall {
- log.Error("UpdatePasswdForm.Validate: %v", err)
- }
- return
- }
-
- validate(errors, data, f)
+func (f *UpdatePasswdForm) Validate(errs *binding.BindingErrors, req *http.Request, ctx martini.Context) {
+ data := ctx.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
+ validate(errs, data, f)
}
diff --git a/modules/base/template.go b/modules/base/template.go
index dd98df75b1..a69a54618d 100644
--- a/modules/base/template.go
+++ b/modules/base/template.go
@@ -93,6 +93,7 @@ var TemplateFuncs template.FuncMap = map[string]interface{}{
"DiffLineTypeToStr": DiffLineTypeToStr,
"ShortSha": ShortSha,
"Oauth2Icon": Oauth2Icon,
+ "Oauth2Name": Oauth2Name,
}
type Actioner interface {
@@ -217,3 +218,19 @@ func Oauth2Icon(t int) string {
}
return ""
}
+
+func Oauth2Name(t int) string {
+ switch t {
+ case 1:
+ return "GitHub"
+ case 2:
+ return "Google"
+ case 3:
+ return "Twitter"
+ case 4:
+ return "Tencent QQ"
+ case 5:
+ return "Weibo"
+ }
+ return ""
+}