user.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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/codegangsta/martini"
  9. "github.com/martini-contrib/render"
  10. "github.com/martini-contrib/sessions"
  11. "github.com/gogits/binding"
  12. "github.com/gogits/gogs/models"
  13. "github.com/gogits/gogs/modules/base"
  14. "github.com/gogits/gogs/modules/log"
  15. )
  16. func SignedInId(session sessions.Session) int64 {
  17. userId := session.Get("userId")
  18. if userId == nil {
  19. return 0
  20. }
  21. if s, ok := userId.(int64); ok {
  22. if _, err := models.GetUserById(s); err != nil {
  23. return 0
  24. }
  25. return s
  26. }
  27. return 0
  28. }
  29. func SignedInName(session sessions.Session) string {
  30. userName := session.Get("userName")
  31. if userName == nil {
  32. return ""
  33. }
  34. if s, ok := userName.(string); ok {
  35. return s
  36. }
  37. return ""
  38. }
  39. func SignedInUser(session sessions.Session) *models.User {
  40. id := SignedInId(session)
  41. if id <= 0 {
  42. return nil
  43. }
  44. user, err := models.GetUserById(id)
  45. if err != nil {
  46. log.Error("user.SignedInUser: %v", err)
  47. return nil
  48. }
  49. return user
  50. }
  51. func IsSignedIn(session sessions.Session) bool {
  52. return SignedInId(session) > 0
  53. }
  54. // SignInRequire checks user status from session.
  55. // It will assign correspoding values to
  56. // template data map if user has signed in.
  57. func SignInRequire(redirect bool) martini.Handler {
  58. return func(r render.Render, data base.TmplData, session sessions.Session) {
  59. if !IsSignedIn(session) {
  60. if redirect {
  61. r.Redirect("/")
  62. }
  63. return
  64. }
  65. data["IsSigned"] = true
  66. data["SignedUserId"] = SignedInId(session)
  67. data["SignedUserName"] = SignedInName(session)
  68. data["SignedAvatar"] = SignedInUser(session).Avatar
  69. }
  70. }
  71. func SignOutRequire() martini.Handler {
  72. return func(r render.Render, session sessions.Session) {
  73. if IsSignedIn(session) {
  74. r.Redirect("/")
  75. }
  76. }
  77. }
  78. type FeedsForm struct {
  79. UserId int64 `form:"userid" binding:"Required"`
  80. Offset int64 `form:"offset"`
  81. }
  82. type UpdateProfileForm struct {
  83. Email string `form:"email" binding:"Required;Email;MaxSize(50)"`
  84. Website string `form:"website" binding:"AlphaDash;MaxSize(50)"`
  85. Location string `form:"location" binding:"MaxSize(50)"`
  86. Avatar string `form:"avatar" binding:"Required;Email;MaxSize(50)"`
  87. }
  88. func (f *UpdateProfileForm) Name(field string) string {
  89. names := map[string]string{
  90. "Email": "Email address",
  91. "Website": "Website",
  92. "Location": "Location",
  93. "Avatar": "Gravatar Email",
  94. }
  95. return names[field]
  96. }
  97. func (f *UpdateProfileForm) Validate(errors *binding.Errors, req *http.Request, context martini.Context) {
  98. if req.Method == "GET" || errors.Count() == 0 {
  99. return
  100. }
  101. data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
  102. data["HasError"] = true
  103. if len(errors.Overall) > 0 {
  104. for _, err := range errors.Overall {
  105. log.Error("UpdateProfileForm.Validate: %v", err)
  106. }
  107. return
  108. }
  109. validate(errors, data, f)
  110. }