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.

166 lines
3.9 KiB

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