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.

191 lines
4.4 KiB

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/Unknwon/macaron"
  10. "github.com/macaron-contrib/binding"
  11. "github.com/macaron-contrib/session"
  12. "github.com/gogits/gogs/models"
  13. "github.com/gogits/gogs/modules/base"
  14. "github.com/gogits/gogs/modules/log"
  15. "github.com/gogits/gogs/modules/setting"
  16. )
  17. // SignedInId returns the id of signed in user.
  18. func SignedInId(header http.Header, sess session.Store) int64 {
  19. if !models.HasEngine {
  20. return 0
  21. }
  22. uid := sess.Get("uid")
  23. if uid == nil {
  24. return 0
  25. }
  26. if id, ok := uid.(int64); ok {
  27. if _, err := models.GetUserById(id); err != nil {
  28. if err != models.ErrUserNotExist {
  29. log.Error(4, "GetUserById: %v", err)
  30. }
  31. return 0
  32. }
  33. return id
  34. }
  35. return 0
  36. }
  37. // SignedInUser returns the user object of signed user.
  38. func SignedInUser(header http.Header, sess session.Store) *models.User {
  39. if !models.HasEngine {
  40. return nil
  41. }
  42. uid := SignedInId(header, sess)
  43. if uid <= 0 {
  44. if setting.Service.EnableReverseProxyAuth {
  45. webAuthUser := header.Get(setting.ReverseProxyAuthUser)
  46. if len(webAuthUser) > 0 {
  47. u, err := models.GetUserByName(webAuthUser)
  48. if err != nil {
  49. if err != models.ErrUserNotExist {
  50. log.Error(4, "GetUserByName: %v", err)
  51. }
  52. return nil
  53. }
  54. return u
  55. }
  56. }
  57. // Check with basic auth.
  58. baHead := header.Get("Authorization")
  59. if len(baHead) > 0 {
  60. auths := strings.Fields(baHead)
  61. if len(auths) == 2 && auths[0] == "Basic" {
  62. uname, passwd, _ := base.BasicAuthDecode(auths[1])
  63. u, err := models.GetUserByName(uname)
  64. if err != nil {
  65. if err != models.ErrUserNotExist {
  66. log.Error(4, "GetUserByName: %v", err)
  67. }
  68. return nil
  69. }
  70. if u.ValidtePassword(passwd) {
  71. return u
  72. }
  73. }
  74. }
  75. return nil
  76. }
  77. u, err := models.GetUserById(uid)
  78. if err != nil {
  79. log.Error(4, "GetUserById: %v", err)
  80. return nil
  81. }
  82. return u
  83. }
  84. type Form interface {
  85. binding.Validator
  86. }
  87. // AssignForm assign form values back to the template data.
  88. func AssignForm(form interface{}, data map[string]interface{}) {
  89. typ := reflect.TypeOf(form)
  90. val := reflect.ValueOf(form)
  91. if typ.Kind() == reflect.Ptr {
  92. typ = typ.Elem()
  93. val = val.Elem()
  94. }
  95. for i := 0; i < typ.NumField(); i++ {
  96. field := typ.Field(i)
  97. fieldName := field.Tag.Get("form")
  98. // Allow ignored fields in the struct
  99. if fieldName == "-" {
  100. continue
  101. }
  102. data[fieldName] = val.Field(i).Interface()
  103. }
  104. }
  105. func getSize(field reflect.StructField, prefix string) string {
  106. for _, rule := range strings.Split(field.Tag.Get("binding"), ";") {
  107. if strings.HasPrefix(rule, prefix) {
  108. return rule[8 : len(rule)-1]
  109. }
  110. }
  111. return ""
  112. }
  113. func GetMinSize(field reflect.StructField) string {
  114. return getSize(field, "MinSize(")
  115. }
  116. func GetMaxSize(field reflect.StructField) string {
  117. return getSize(field, "MaxSize(")
  118. }
  119. func validate(errs binding.Errors, data map[string]interface{}, f Form, l macaron.Locale) binding.Errors {
  120. if errs.Len() == 0 {
  121. return errs
  122. }
  123. data["HasError"] = true
  124. AssignForm(f, data)
  125. typ := reflect.TypeOf(f)
  126. val := reflect.ValueOf(f)
  127. if typ.Kind() == reflect.Ptr {
  128. typ = typ.Elem()
  129. val = val.Elem()
  130. }
  131. for i := 0; i < typ.NumField(); i++ {
  132. field := typ.Field(i)
  133. fieldName := field.Tag.Get("form")
  134. // Allow ignored fields in the struct
  135. if fieldName == "-" {
  136. continue
  137. }
  138. if errs[0].FieldNames[0] == field.Name {
  139. data["Err_"+field.Name] = true
  140. trName := l.Tr("form." + field.Name)
  141. switch errs[0].Classification {
  142. case binding.RequiredError:
  143. data["ErrorMsg"] = trName + l.Tr("form.require_error")
  144. case binding.AlphaDashError:
  145. data["ErrorMsg"] = trName + l.Tr("form.alpha_dash_error")
  146. case binding.AlphaDashDotError:
  147. data["ErrorMsg"] = trName + l.Tr("form.alpha_dash_dot_error")
  148. case binding.MinSizeError:
  149. data["ErrorMsg"] = trName + l.Tr("form.min_size_error", GetMinSize(field))
  150. case binding.MaxSizeError:
  151. data["ErrorMsg"] = trName + l.Tr("form.max_size_error", GetMaxSize(field))
  152. case binding.EmailError:
  153. data["ErrorMsg"] = trName + l.Tr("form.email_error")
  154. case binding.UrlError:
  155. data["ErrorMsg"] = trName + l.Tr("form.url_error")
  156. default:
  157. data["ErrorMsg"] = l.Tr("form.unknown_error") + " " + errs[0].Classification
  158. }
  159. return errs
  160. }
  161. }
  162. return errs
  163. }