user.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. var id int64
  22. if setting.Service.EnableReverseProxyAuth {
  23. id, _ = base.StrTo(header.Get(setting.ReverseProxyAuthUid)).Int64()
  24. }
  25. if id <= 0 {
  26. uid := sess.Get("userId")
  27. if uid == nil {
  28. return 0
  29. }
  30. var ok bool
  31. if id, ok = uid.(int64); !ok {
  32. return 0
  33. }
  34. }
  35. if id > 0 {
  36. if _, err := models.GetUserById(id); err != nil {
  37. if err != models.ErrUserNotExist {
  38. log.Error("auth.user.SignedInId(GetUserById): %v", err)
  39. }
  40. return 0
  41. }
  42. return id
  43. }
  44. return 0
  45. }
  46. // SignedInUser returns the user object of signed user.
  47. func SignedInUser(header http.Header, sess session.SessionStore) *models.User {
  48. uid := SignedInId(header, sess)
  49. if uid <= 0 {
  50. return nil
  51. }
  52. u, err := models.GetUserById(uid)
  53. if err != nil {
  54. log.Error("user.SignedInUser: %v", err)
  55. return nil
  56. }
  57. return u
  58. }
  59. // IsSignedIn check if any user has signed in.
  60. func IsSignedIn(header http.Header, sess session.SessionStore) bool {
  61. return SignedInId(header, sess) > 0
  62. }
  63. type FeedsForm struct {
  64. UserId int64 `form:"userid" binding:"Required"`
  65. Page int64 `form:"p"`
  66. }
  67. type UpdateProfileForm struct {
  68. UserName string `form:"username" binding:"Required;AlphaDash;MaxSize(30)"`
  69. FullName string `form:"fullname" binding:"MaxSize(40)"`
  70. Email string `form:"email" binding:"Required;Email;MaxSize(50)"`
  71. Website string `form:"website" binding:"Url;MaxSize(50)"`
  72. Location string `form:"location" binding:"MaxSize(50)"`
  73. Avatar string `form:"avatar" binding:"Required;Email;MaxSize(50)"`
  74. }
  75. func (f *UpdateProfileForm) Name(field string) string {
  76. names := map[string]string{
  77. "UserName": "Username",
  78. "Email": "E-mail address",
  79. "Website": "Website",
  80. "Location": "Location",
  81. "Avatar": "Gravatar Email",
  82. }
  83. return names[field]
  84. }
  85. func (f *UpdateProfileForm) Validate(errs *binding.Errors, req *http.Request, ctx martini.Context) {
  86. data := ctx.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
  87. validate(errs, data, f)
  88. }
  89. type UpdatePasswdForm struct {
  90. OldPasswd string `form:"oldpasswd" binding:"Required;MinSize(6);MaxSize(30)"`
  91. NewPasswd string `form:"newpasswd" binding:"Required;MinSize(6);MaxSize(30)"`
  92. RetypePasswd string `form:"retypepasswd"`
  93. }
  94. func (f *UpdatePasswdForm) Name(field string) string {
  95. names := map[string]string{
  96. "OldPasswd": "Old password",
  97. "NewPasswd": "New password",
  98. "RetypePasswd": "Re-type password",
  99. }
  100. return names[field]
  101. }
  102. func (f *UpdatePasswdForm) Validate(errs *binding.Errors, req *http.Request, ctx martini.Context) {
  103. data := ctx.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
  104. validate(errs, data, f)
  105. }