Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

auth.go 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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/codegangsta/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;MinSize(5);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;MinSize(5);MaxSize(30)"`
  50. Password string `form:"passwd" binding:"Required;MinSize(6);MaxSize(30)"`
  51. }
  52. func (f *LogInForm) Name(field string) string {
  53. names := map[string]string{
  54. "UserName": "Username",
  55. "Password": "Password",
  56. }
  57. return names[field]
  58. }
  59. func (f *LogInForm) Validate(errors *binding.Errors, req *http.Request, context martini.Context) {
  60. if req.Method == "GET" || errors.Count() == 0 {
  61. return
  62. }
  63. data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
  64. data["HasError"] = true
  65. AssignForm(f, data)
  66. if len(errors.Overall) > 0 {
  67. for _, err := range errors.Overall {
  68. log.Error("LogInForm.Validate: %v", err)
  69. }
  70. return
  71. }
  72. validate(errors, data, f)
  73. }
  74. func getMinMaxSize(field reflect.StructField) string {
  75. for _, rule := range strings.Split(field.Tag.Get("binding"), ";") {
  76. if strings.HasPrefix(rule, "MinSize(") || strings.HasPrefix(rule, "MaxSize(") {
  77. return rule[8 : len(rule)-1]
  78. }
  79. }
  80. return ""
  81. }
  82. func validate(errors *binding.Errors, data base.TmplData, form Form) {
  83. typ := reflect.TypeOf(form)
  84. val := reflect.ValueOf(form)
  85. if typ.Kind() == reflect.Ptr {
  86. typ = typ.Elem()
  87. val = val.Elem()
  88. }
  89. for i := 0; i < typ.NumField(); i++ {
  90. field := typ.Field(i)
  91. fieldName := field.Tag.Get("form")
  92. // Allow ignored fields in the struct
  93. if fieldName == "-" {
  94. continue
  95. }
  96. if err, ok := errors.Fields[field.Name]; ok {
  97. data["Err_"+field.Name] = true
  98. switch err {
  99. case binding.RequireError:
  100. data["ErrorMsg"] = form.Name(field.Name) + " cannot be empty"
  101. case binding.AlphaDashError:
  102. data["ErrorMsg"] = form.Name(field.Name) + " must be valid alpha or numeric or dash(-_) characters"
  103. case binding.MinSizeError:
  104. data["ErrorMsg"] = form.Name(field.Name) + " must contain at least " + getMinMaxSize(field) + " characters"
  105. case binding.MaxSizeError:
  106. data["ErrorMsg"] = form.Name(field.Name) + " must contain at most " + getMinMaxSize(field) + " characters"
  107. case binding.EmailError:
  108. data["ErrorMsg"] = form.Name(field.Name) + " is not valid"
  109. default:
  110. data["ErrorMsg"] = "Unknown error: " + err
  111. }
  112. return
  113. }
  114. }
  115. }
  116. // AssignForm assign form values back to the template data.
  117. func AssignForm(form interface{}, data base.TmplData) {
  118. typ := reflect.TypeOf(form)
  119. val := reflect.ValueOf(form)
  120. if typ.Kind() == reflect.Ptr {
  121. typ = typ.Elem()
  122. val = val.Elem()
  123. }
  124. for i := 0; i < typ.NumField(); i++ {
  125. field := typ.Field(i)
  126. fieldName := field.Tag.Get("form")
  127. // Allow ignored fields in the struct
  128. if fieldName == "-" {
  129. continue
  130. }
  131. data[fieldName] = val.Field(i).Interface()
  132. }
  133. }