auth.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. type FeedsForm struct {
  75. UserId int64 `form:"userid" binding:"Required"`
  76. Offset int64 `form:"offset"`
  77. }
  78. func getMinMaxSize(field reflect.StructField) string {
  79. for _, rule := range strings.Split(field.Tag.Get("binding"), ";") {
  80. if strings.HasPrefix(rule, "MinSize(") || strings.HasPrefix(rule, "MaxSize(") {
  81. return rule[8 : len(rule)-1]
  82. }
  83. }
  84. return ""
  85. }
  86. func validate(errors *binding.Errors, data base.TmplData, form Form) {
  87. typ := reflect.TypeOf(form)
  88. val := reflect.ValueOf(form)
  89. if typ.Kind() == reflect.Ptr {
  90. typ = typ.Elem()
  91. val = val.Elem()
  92. }
  93. for i := 0; i < typ.NumField(); i++ {
  94. field := typ.Field(i)
  95. fieldName := field.Tag.Get("form")
  96. // Allow ignored fields in the struct
  97. if fieldName == "-" {
  98. continue
  99. }
  100. if err, ok := errors.Fields[field.Name]; ok {
  101. data["Err_"+field.Name] = true
  102. switch err {
  103. case binding.RequireError:
  104. data["ErrorMsg"] = form.Name(field.Name) + " cannot be empty"
  105. case binding.AlphaDashError:
  106. data["ErrorMsg"] = form.Name(field.Name) + " must be valid alpha or numeric or dash(-_) characters"
  107. case binding.MinSizeError:
  108. data["ErrorMsg"] = form.Name(field.Name) + " must contain at least " + getMinMaxSize(field) + " characters"
  109. case binding.MaxSizeError:
  110. data["ErrorMsg"] = form.Name(field.Name) + " must contain at most " + getMinMaxSize(field) + " characters"
  111. case binding.EmailError:
  112. data["ErrorMsg"] = form.Name(field.Name) + " is not valid"
  113. default:
  114. data["ErrorMsg"] = "Unknown error: " + err
  115. }
  116. return
  117. }
  118. }
  119. }
  120. // AssignForm assign form values back to the template data.
  121. func AssignForm(form interface{}, data base.TmplData) {
  122. typ := reflect.TypeOf(form)
  123. val := reflect.ValueOf(form)
  124. if typ.Kind() == reflect.Ptr {
  125. typ = typ.Elem()
  126. val = val.Elem()
  127. }
  128. for i := 0; i < typ.NumField(); i++ {
  129. field := typ.Field(i)
  130. fieldName := field.Tag.Get("form")
  131. // Allow ignored fields in the struct
  132. if fieldName == "-" {
  133. continue
  134. }
  135. data[fieldName] = val.Field(i).Interface()
  136. }
  137. }