You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
5.6 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package auth
  5. import (
  6. "net/http"
  7. "reflect"
  8. "strings"
  9. "github.com/go-martini/martini"
  10. "github.com/gogits/gogs/modules/base"
  11. "github.com/gogits/gogs/modules/log"
  12. "github.com/gogits/gogs/modules/middleware/binding"
  13. )
  14. // Web form interface.
  15. type Form interface {
  16. Name(field string) string
  17. }
  18. type RegisterForm struct {
  19. UserName string `form:"username" binding:"Required;AlphaDashDot;MaxSize(30)"`
  20. Email string `form:"email" binding:"Required;Email;MaxSize(50)"`
  21. Password string `form:"passwd" binding:"Required;MinSize(6);MaxSize(30)"`
  22. RetypePasswd string `form:"retypepasswd"`
  23. LoginType string `form:"logintype"`
  24. LoginName string `form:"loginname"`
  25. }
  26. func (f *RegisterForm) Name(field string) string {
  27. names := map[string]string{
  28. "UserName": "Username",
  29. "Email": "E-mail address",
  30. "Password": "Password",
  31. "RetypePasswd": "Re-type password",
  32. }
  33. return names[field]
  34. }
  35. func (f *RegisterForm) Validate(errs *binding.Errors, req *http.Request, ctx martini.Context) {
  36. data := ctx.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
  37. validate(errs, data, f)
  38. }
  39. type LogInForm struct {
  40. UserName string `form:"username" binding:"Required;MaxSize(35)"`
  41. Password string `form:"passwd" binding:"Required;MinSize(6);MaxSize(30)"`
  42. Remember bool `form:"remember"`
  43. }
  44. func (f *LogInForm) Name(field string) string {
  45. names := map[string]string{
  46. "UserName": "Username",
  47. "Password": "Password",
  48. }
  49. return names[field]
  50. }
  51. func (f *LogInForm) Validate(errs *binding.Errors, req *http.Request, ctx martini.Context) {
  52. data := ctx.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
  53. validate(errs, data, f)
  54. }
  55. func GetMinMaxSize(field reflect.StructField) string {
  56. for _, rule := range strings.Split(field.Tag.Get("binding"), ";") {
  57. if strings.HasPrefix(rule, "MinSize(") || strings.HasPrefix(rule, "MaxSize(") {
  58. return rule[8 : len(rule)-1]
  59. }
  60. }
  61. return ""
  62. }
  63. func validate(errs *binding.Errors, data base.TmplData, f Form) {
  64. if errs.Count() == 0 {
  65. return
  66. } else if len(errs.Overall) > 0 {
  67. for _, err := range errs.Overall {
  68. log.Error("%s: %v", reflect.TypeOf(f), err)
  69. }
  70. return
  71. }
  72. data["HasError"] = true
  73. AssignForm(f, data)
  74. typ := reflect.TypeOf(f)
  75. val := reflect.ValueOf(f)
  76. if typ.Kind() == reflect.Ptr {
  77. typ = typ.Elem()
  78. val = val.Elem()
  79. }
  80. for i := 0; i < typ.NumField(); i++ {
  81. field := typ.Field(i)
  82. fieldName := field.Tag.Get("form")
  83. // Allow ignored fields in the struct
  84. if fieldName == "-" {
  85. continue
  86. }
  87. if err, ok := errs.Fields[field.Name]; ok {
  88. data["Err_"+field.Name] = true
  89. switch err {
  90. case binding.BindingRequireError:
  91. data["ErrorMsg"] = f.Name(field.Name) + " cannot be empty"
  92. case binding.BindingAlphaDashError:
  93. data["ErrorMsg"] = f.Name(field.Name) + " must be valid alpha or numeric or dash(-_) characters"
  94. case binding.BindingAlphaDashDotError:
  95. data["ErrorMsg"] = f.Name(field.Name) + " must be valid alpha or numeric or dash(-_) or dot characters"
  96. case binding.BindingMinSizeError:
  97. data["ErrorMsg"] = f.Name(field.Name) + " must contain at least " + GetMinMaxSize(field) + " characters"
  98. case binding.BindingMaxSizeError:
  99. data["ErrorMsg"] = f.Name(field.Name) + " must contain at most " + GetMinMaxSize(field) + " characters"
  100. case binding.BindingEmailError:
  101. data["ErrorMsg"] = f.Name(field.Name) + " is not a valid e-mail address"
  102. case binding.BindingUrlError:
  103. data["ErrorMsg"] = f.Name(field.Name) + " is not a valid URL"
  104. default:
  105. data["ErrorMsg"] = "Unknown error: " + err
  106. }
  107. return
  108. }
  109. }
  110. }
  111. // AssignForm assign form values back to the template data.
  112. func AssignForm(form interface{}, data base.TmplData) {
  113. typ := reflect.TypeOf(form)
  114. val := reflect.ValueOf(form)
  115. if typ.Kind() == reflect.Ptr {
  116. typ = typ.Elem()
  117. val = val.Elem()
  118. }
  119. for i := 0; i < typ.NumField(); i++ {
  120. field := typ.Field(i)
  121. fieldName := field.Tag.Get("form")
  122. // Allow ignored fields in the struct
  123. if fieldName == "-" {
  124. continue
  125. }
  126. data[fieldName] = val.Field(i).Interface()
  127. }
  128. }
  129. type InstallForm struct {
  130. Database string `form:"database" binding:"Required"`
  131. Host string `form:"host"`
  132. User string `form:"user"`
  133. Passwd string `form:"passwd"`
  134. DatabaseName string `form:"database_name"`
  135. SslMode string `form:"ssl_mode"`
  136. DatabasePath string `form:"database_path"`
  137. RepoRootPath string `form:"repo_path"`
  138. RunUser string `form:"run_user"`
  139. Domain string `form:"domain"`
  140. AppUrl string `form:"app_url"`
  141. AdminName string `form:"admin_name" binding:"Required;AlphaDashDot;MaxSize(30)"`
  142. AdminPasswd string `form:"admin_pwd" binding:"Required;MinSize(6);MaxSize(30)"`
  143. AdminEmail string `form:"admin_email" binding:"Required;Email;MaxSize(50)"`
  144. SmtpHost string `form:"smtp_host"`
  145. SmtpEmail string `form:"mailer_user"`
  146. SmtpPasswd string `form:"mailer_pwd"`
  147. RegisterConfirm string `form:"register_confirm"`
  148. MailNotify string `form:"mail_notify"`
  149. }
  150. func (f *InstallForm) Name(field string) string {
  151. names := map[string]string{
  152. "Database": "Database name",
  153. "AdminName": "Admin user name",
  154. "AdminPasswd": "Admin password",
  155. "AdminEmail": "Admin e-maill address",
  156. }
  157. return names[field]
  158. }
  159. func (f *InstallForm) Validate(errors *binding.Errors, req *http.Request, context martini.Context) {
  160. data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
  161. validate(errors, data, f)
  162. }