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.

299 lines
8.5 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
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
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/session"
  11. "github.com/gogits/gogs/models"
  12. "github.com/gogits/gogs/modules/base"
  13. "github.com/gogits/gogs/modules/log"
  14. "github.com/gogits/gogs/modules/middleware/binding"
  15. "github.com/gogits/gogs/modules/setting"
  16. )
  17. // Web form interface.
  18. type Form interface {
  19. Name(field string) string
  20. }
  21. type RegisterForm struct {
  22. UserName string `form:"username" binding:"Required;AlphaDashDot;MaxSize(30)"`
  23. Email string `form:"email" binding:"Required;Email;MaxSize(50)"`
  24. Password string `form:"passwd" binding:"Required;MinSize(6);MaxSize(30)"`
  25. RetypePasswd string `form:"retypepasswd"`
  26. LoginType string `form:"logintype"`
  27. LoginName string `form:"loginname"`
  28. }
  29. func (f *RegisterForm) Name(field string) string {
  30. names := map[string]string{
  31. "UserName": "Username",
  32. "Email": "E-mail address",
  33. "Password": "Password",
  34. "RetypePasswd": "Re-type password",
  35. }
  36. return names[field]
  37. }
  38. func (f *RegisterForm) Validate(errs *binding.Errors, req *http.Request, ctx martini.Context) {
  39. data := ctx.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
  40. validate(errs, data, f)
  41. }
  42. type LogInForm struct {
  43. UserName string `form:"username" binding:"Required;MaxSize(35)"`
  44. Password string `form:"passwd" binding:"Required;MinSize(6);MaxSize(30)"`
  45. Remember bool `form:"remember"`
  46. }
  47. func (f *LogInForm) Name(field string) string {
  48. names := map[string]string{
  49. "UserName": "Username",
  50. "Password": "Password",
  51. }
  52. return names[field]
  53. }
  54. func (f *LogInForm) Validate(errs *binding.Errors, req *http.Request, ctx martini.Context) {
  55. data := ctx.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
  56. validate(errs, data, f)
  57. }
  58. func GetMinMaxSize(field reflect.StructField) string {
  59. for _, rule := range strings.Split(field.Tag.Get("binding"), ";") {
  60. if strings.HasPrefix(rule, "MinSize(") || strings.HasPrefix(rule, "MaxSize(") {
  61. return rule[8 : len(rule)-1]
  62. }
  63. }
  64. return ""
  65. }
  66. func validate(errs *binding.Errors, data base.TmplData, f Form) {
  67. if errs.Count() == 0 {
  68. return
  69. } else if len(errs.Overall) > 0 {
  70. for _, err := range errs.Overall {
  71. log.Error("%s: %v", reflect.TypeOf(f), err)
  72. }
  73. return
  74. }
  75. data["HasError"] = true
  76. AssignForm(f, data)
  77. typ := reflect.TypeOf(f)
  78. val := reflect.ValueOf(f)
  79. if typ.Kind() == reflect.Ptr {
  80. typ = typ.Elem()
  81. val = val.Elem()
  82. }
  83. for i := 0; i < typ.NumField(); i++ {
  84. field := typ.Field(i)
  85. fieldName := field.Tag.Get("form")
  86. // Allow ignored fields in the struct
  87. if fieldName == "-" {
  88. continue
  89. }
  90. if err, ok := errs.Fields[field.Name]; ok {
  91. data["Err_"+field.Name] = true
  92. switch err {
  93. case binding.BindingRequireError:
  94. data["ErrorMsg"] = f.Name(field.Name) + " cannot be empty"
  95. case binding.BindingAlphaDashError:
  96. data["ErrorMsg"] = f.Name(field.Name) + " must be valid alpha or numeric or dash(-_) characters"
  97. case binding.BindingAlphaDashDotError:
  98. data["ErrorMsg"] = f.Name(field.Name) + " must be valid alpha or numeric or dash(-_) or dot characters"
  99. case binding.BindingMinSizeError:
  100. data["ErrorMsg"] = f.Name(field.Name) + " must contain at least " + GetMinMaxSize(field) + " characters"
  101. case binding.BindingMaxSizeError:
  102. data["ErrorMsg"] = f.Name(field.Name) + " must contain at most " + GetMinMaxSize(field) + " characters"
  103. case binding.BindingEmailError:
  104. data["ErrorMsg"] = f.Name(field.Name) + " is not a valid e-mail address"
  105. case binding.BindingUrlError:
  106. data["ErrorMsg"] = f.Name(field.Name) + " is not a valid URL"
  107. default:
  108. data["ErrorMsg"] = "Unknown error: " + err
  109. }
  110. return
  111. }
  112. }
  113. }
  114. // AssignForm assign form values back to the template data.
  115. func AssignForm(form interface{}, data base.TmplData) {
  116. typ := reflect.TypeOf(form)
  117. val := reflect.ValueOf(form)
  118. if typ.Kind() == reflect.Ptr {
  119. typ = typ.Elem()
  120. val = val.Elem()
  121. }
  122. for i := 0; i < typ.NumField(); i++ {
  123. field := typ.Field(i)
  124. fieldName := field.Tag.Get("form")
  125. // Allow ignored fields in the struct
  126. if fieldName == "-" {
  127. continue
  128. }
  129. data[fieldName] = val.Field(i).Interface()
  130. }
  131. }
  132. type InstallForm struct {
  133. Database string `form:"database" binding:"Required"`
  134. Host string `form:"host"`
  135. User string `form:"user"`
  136. Passwd string `form:"passwd"`
  137. DatabaseName string `form:"database_name"`
  138. SslMode string `form:"ssl_mode"`
  139. DatabasePath string `form:"database_path"`
  140. RepoRootPath string `form:"repo_path"`
  141. RunUser string `form:"run_user"`
  142. Domain string `form:"domain"`
  143. AppUrl string `form:"app_url"`
  144. AdminName string `form:"admin_name" binding:"Required;AlphaDashDot;MaxSize(30)"`
  145. AdminPasswd string `form:"admin_pwd" binding:"Required;MinSize(6);MaxSize(30)"`
  146. AdminEmail string `form:"admin_email" binding:"Required;Email;MaxSize(50)"`
  147. SmtpHost string `form:"smtp_host"`
  148. SmtpEmail string `form:"mailer_user"`
  149. SmtpPasswd string `form:"mailer_pwd"`
  150. RegisterConfirm string `form:"register_confirm"`
  151. MailNotify string `form:"mail_notify"`
  152. }
  153. func (f *InstallForm) Name(field string) string {
  154. names := map[string]string{
  155. "Database": "Database name",
  156. "AdminName": "Admin user name",
  157. "AdminPasswd": "Admin password",
  158. "AdminEmail": "Admin e-maill address",
  159. }
  160. return names[field]
  161. }
  162. func (f *InstallForm) Validate(errors *binding.Errors, req *http.Request, context martini.Context) {
  163. data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
  164. validate(errors, data, f)
  165. }
  166. // SignedInId returns the id of signed in user.
  167. func SignedInId(header http.Header, sess session.SessionStore) int64 {
  168. if !models.HasEngine {
  169. return 0
  170. }
  171. if setting.Service.EnableReverseProxyAuth {
  172. webAuthUser := header.Get(setting.ReverseProxyAuthUser)
  173. if len(webAuthUser) > 0 {
  174. u, err := models.GetUserByName(webAuthUser)
  175. if err != nil {
  176. if err != models.ErrUserNotExist {
  177. log.Error("auth.user.SignedInId(GetUserByName): %v", err)
  178. }
  179. return 0
  180. }
  181. return u.Id
  182. }
  183. }
  184. uid := sess.Get("userId")
  185. if uid == nil {
  186. return 0
  187. }
  188. if id, ok := uid.(int64); ok {
  189. if _, err := models.GetUserById(id); err != nil {
  190. if err != models.ErrUserNotExist {
  191. log.Error("auth.user.SignedInId(GetUserById): %v", err)
  192. }
  193. return 0
  194. }
  195. return id
  196. }
  197. return 0
  198. }
  199. // SignedInUser returns the user object of signed user.
  200. func SignedInUser(header http.Header, sess session.SessionStore) *models.User {
  201. uid := SignedInId(header, sess)
  202. if uid <= 0 {
  203. return nil
  204. }
  205. u, err := models.GetUserById(uid)
  206. if err != nil {
  207. log.Error("user.SignedInUser: %v", err)
  208. return nil
  209. }
  210. return u
  211. }
  212. // IsSignedIn check if any user has signed in.
  213. func IsSignedIn(header http.Header, sess session.SessionStore) bool {
  214. return SignedInId(header, sess) > 0
  215. }
  216. type FeedsForm struct {
  217. UserId int64 `form:"userid" binding:"Required"`
  218. Page int64 `form:"p"`
  219. }
  220. type UpdateProfileForm struct {
  221. UserName string `form:"username" binding:"Required;AlphaDash;MaxSize(30)"`
  222. FullName string `form:"fullname" binding:"MaxSize(40)"`
  223. Email string `form:"email" binding:"Required;Email;MaxSize(50)"`
  224. Website string `form:"website" binding:"Url;MaxSize(50)"`
  225. Location string `form:"location" binding:"MaxSize(50)"`
  226. Avatar string `form:"avatar" binding:"Required;Email;MaxSize(50)"`
  227. }
  228. func (f *UpdateProfileForm) Name(field string) string {
  229. names := map[string]string{
  230. "UserName": "Username",
  231. "Email": "E-mail address",
  232. "Website": "Website address",
  233. "Location": "Location",
  234. "Avatar": "Gravatar Email",
  235. }
  236. return names[field]
  237. }
  238. func (f *UpdateProfileForm) Validate(errs *binding.Errors, req *http.Request, ctx martini.Context) {
  239. data := ctx.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
  240. validate(errs, data, f)
  241. }
  242. type UpdatePasswdForm struct {
  243. OldPasswd string `form:"oldpasswd" binding:"Required;MinSize(6);MaxSize(30)"`
  244. NewPasswd string `form:"newpasswd" binding:"Required;MinSize(6);MaxSize(30)"`
  245. RetypePasswd string `form:"retypepasswd"`
  246. }
  247. func (f *UpdatePasswdForm) Name(field string) string {
  248. names := map[string]string{
  249. "OldPasswd": "Old password",
  250. "NewPasswd": "New password",
  251. "RetypePasswd": "Re-type password",
  252. }
  253. return names[field]
  254. }
  255. func (f *UpdatePasswdForm) Validate(errs *binding.Errors, req *http.Request, ctx martini.Context) {
  256. data := ctx.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
  257. validate(errs, data, f)
  258. }