user.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. "github.com/go-martini/martini"
  9. "github.com/gogits/session"
  10. "github.com/gogits/gogs/models"
  11. "github.com/gogits/gogs/modules/base"
  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.SessionStore) 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("auth.user.SignedInId(GetUserByName): %v", err)
  28. }
  29. return 0
  30. }
  31. return u.Id
  32. }
  33. }
  34. uid := sess.Get("userId")
  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("auth.user.SignedInId(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.SessionStore) *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("user.SignedInUser: %v", err)
  58. return nil
  59. }
  60. return u
  61. }
  62. // IsSignedIn check if any user has signed in.
  63. func IsSignedIn(header http.Header, sess session.SessionStore) bool {
  64. return SignedInId(header, sess) > 0
  65. }
  66. type FeedsForm struct {
  67. UserId int64 `form:"userid" binding:"Required"`
  68. Page int64 `form:"p"`
  69. }
  70. type UpdateProfileForm struct {
  71. UserName string `form:"username" binding:"Required;AlphaDash;MaxSize(30)"`
  72. FullName string `form:"fullname" binding:"MaxSize(40)"`
  73. Email string `form:"email" binding:"Required;Email;MaxSize(50)"`
  74. Website string `form:"website" binding:"Url;MaxSize(50)"`
  75. Location string `form:"location" binding:"MaxSize(50)"`
  76. Avatar string `form:"avatar" binding:"Required;Email;MaxSize(50)"`
  77. }
  78. func (f *UpdateProfileForm) Name(field string) string {
  79. names := map[string]string{
  80. "UserName": "Username",
  81. "Email": "E-mail address",
  82. "Website": "Website",
  83. "Location": "Location",
  84. "Avatar": "Gravatar Email",
  85. }
  86. return names[field]
  87. }
  88. func (f *UpdateProfileForm) Validate(errs *binding.Errors, req *http.Request, ctx martini.Context) {
  89. data := ctx.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
  90. validate(errs, data, f)
  91. }
  92. type UpdatePasswdForm struct {
  93. OldPasswd string `form:"oldpasswd" binding:"Required;MinSize(6);MaxSize(30)"`
  94. NewPasswd string `form:"newpasswd" binding:"Required;MinSize(6);MaxSize(30)"`
  95. RetypePasswd string `form:"retypepasswd"`
  96. }
  97. func (f *UpdatePasswdForm) Name(field string) string {
  98. names := map[string]string{
  99. "OldPasswd": "Old password",
  100. "NewPasswd": "New password",
  101. "RetypePasswd": "Re-type password",
  102. }
  103. return names[field]
  104. }
  105. func (f *UpdatePasswdForm) Validate(errs *binding.Errors, req *http.Request, ctx martini.Context) {
  106. data := ctx.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
  107. validate(errs, data, f)
  108. }