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.

214 lines
5.7 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
  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/binding"
  11. "github.com/gogits/gogs/modules/base"
  12. "github.com/gogits/gogs/modules/log"
  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;AlphaDash;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. }
  24. func (f *RegisterForm) Name(field string) string {
  25. names := map[string]string{
  26. "UserName": "Username",
  27. "Email": "E-mail address",
  28. "Password": "Password",
  29. "RetypePasswd": "Re-type password",
  30. }
  31. return names[field]
  32. }
  33. func (f *RegisterForm) Validate(errors *binding.Errors, req *http.Request, context martini.Context) {
  34. if req.Method == "GET" || errors.Count() == 0 {
  35. return
  36. }
  37. data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
  38. data["HasError"] = true
  39. AssignForm(f, data)
  40. if len(errors.Overall) > 0 {
  41. for _, err := range errors.Overall {
  42. log.Error("RegisterForm.Validate: %v", err)
  43. }
  44. return
  45. }
  46. validate(errors, data, f)
  47. }
  48. type LogInForm struct {
  49. UserName string `form:"username" binding:"Required;AlphaDash;MaxSize(30)"`
  50. Password string `form:"passwd" binding:"Required;MinSize(6);MaxSize(30)"`
  51. Remember string `form:"remember"`
  52. }
  53. func (f *LogInForm) Name(field string) string {
  54. names := map[string]string{
  55. "UserName": "Username",
  56. "Password": "Password",
  57. }
  58. return names[field]
  59. }
  60. func (f *LogInForm) Validate(errors *binding.Errors, req *http.Request, context martini.Context) {
  61. if req.Method == "GET" || errors.Count() == 0 {
  62. return
  63. }
  64. data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
  65. data["HasError"] = true
  66. AssignForm(f, data)
  67. if len(errors.Overall) > 0 {
  68. for _, err := range errors.Overall {
  69. log.Error("LogInForm.Validate: %v", err)
  70. }
  71. return
  72. }
  73. validate(errors, data, f)
  74. }
  75. func getMinMaxSize(field reflect.StructField) string {
  76. for _, rule := range strings.Split(field.Tag.Get("binding"), ";") {
  77. if strings.HasPrefix(rule, "MinSize(") || strings.HasPrefix(rule, "MaxSize(") {
  78. return rule[8 : len(rule)-1]
  79. }
  80. }
  81. return ""
  82. }
  83. func validate(errors *binding.Errors, data base.TmplData, form Form) {
  84. typ := reflect.TypeOf(form)
  85. val := reflect.ValueOf(form)
  86. if typ.Kind() == reflect.Ptr {
  87. typ = typ.Elem()
  88. val = val.Elem()
  89. }
  90. for i := 0; i < typ.NumField(); i++ {
  91. field := typ.Field(i)
  92. fieldName := field.Tag.Get("form")
  93. // Allow ignored fields in the struct
  94. if fieldName == "-" {
  95. continue
  96. }
  97. if err, ok := errors.Fields[field.Name]; ok {
  98. data["Err_"+field.Name] = true
  99. switch err {
  100. case binding.RequireError:
  101. data["ErrorMsg"] = form.Name(field.Name) + " cannot be empty"
  102. case binding.AlphaDashError:
  103. data["ErrorMsg"] = form.Name(field.Name) + " must be valid alpha or numeric or dash(-_) characters"
  104. case binding.MinSizeError:
  105. data["ErrorMsg"] = form.Name(field.Name) + " must contain at least " + getMinMaxSize(field) + " characters"
  106. case binding.MaxSizeError:
  107. data["ErrorMsg"] = form.Name(field.Name) + " must contain at most " + getMinMaxSize(field) + " characters"
  108. case binding.EmailError:
  109. data["ErrorMsg"] = form.Name(field.Name) + " is not valid"
  110. default:
  111. data["ErrorMsg"] = "Unknown error: " + err
  112. }
  113. return
  114. }
  115. }
  116. }
  117. // AssignForm assign form values back to the template data.
  118. func AssignForm(form interface{}, data base.TmplData) {
  119. typ := reflect.TypeOf(form)
  120. val := reflect.ValueOf(form)
  121. if typ.Kind() == reflect.Ptr {
  122. typ = typ.Elem()
  123. val = val.Elem()
  124. }
  125. for i := 0; i < typ.NumField(); i++ {
  126. field := typ.Field(i)
  127. fieldName := field.Tag.Get("form")
  128. // Allow ignored fields in the struct
  129. if fieldName == "-" {
  130. continue
  131. }
  132. data[fieldName] = val.Field(i).Interface()
  133. }
  134. }
  135. type InstallForm struct {
  136. Database string `form:"database" binding:"Required"`
  137. Host string `form:"host"`
  138. User string `form:"user"`
  139. Passwd string `form:"passwd"`
  140. DatabaseName string `form:"database_name"`
  141. SslMode string `form:"ssl_mode"`
  142. DatabasePath string `form:"database_path"`
  143. RepoRootPath string `form:"repo_path"`
  144. RunUser string `form:"run_user"`
  145. Domain string `form:"domain"`
  146. AppUrl string `form:"app_url"`
  147. AdminName string `form:"admin_name" binding:"Required"`
  148. AdminPasswd string `form:"admin_pwd" binding:"Required;MinSize(6);MaxSize(30)"`
  149. AdminEmail string `form:"admin_email" binding:"Required;Email;MaxSize(50)"`
  150. SmtpHost string `form:"smtp_host"`
  151. SmtpEmail string `form:"mailer_user"`
  152. SmtpPasswd string `form:"mailer_pwd"`
  153. RegisterConfirm string `form:"register_confirm"`
  154. MailNotify string `form:"mail_notify"`
  155. }
  156. func (f *InstallForm) Name(field string) string {
  157. names := map[string]string{
  158. "Database": "Database name",
  159. "AdminName": "Admin user name",
  160. "AdminPasswd": "Admin password",
  161. "AdminEmail": "Admin e-maill address",
  162. }
  163. return names[field]
  164. }
  165. func (f *InstallForm) Validate(errors *binding.Errors, req *http.Request, context martini.Context) {
  166. if req.Method == "GET" || errors.Count() == 0 {
  167. return
  168. }
  169. data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
  170. data["HasError"] = true
  171. AssignForm(f, data)
  172. if len(errors.Overall) > 0 {
  173. for _, err := range errors.Overall {
  174. log.Error("InstallForm.Validate: %v", err)
  175. }
  176. return
  177. }
  178. validate(errors, data, f)
  179. }