user.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. user := SignedInUser(session)
  66. if user == nil {
  67. r.Redirect("/")
  68. return
  69. }
  70. data["IsSigned"] = true
  71. data["SignedUser"] = user
  72. data["SignedUserId"] = user.Id
  73. data["SignedUserName"] = user.LowerName
  74. }
  75. }
  76. func SignOutRequire() martini.Handler {
  77. return func(r render.Render, session sessions.Session) {
  78. if IsSignedIn(session) {
  79. r.Redirect("/")
  80. }
  81. }
  82. }
  83. type FeedsForm struct {
  84. UserId int64 `form:"userid" binding:"Required"`
  85. Page int64 `form:"p"`
  86. }
  87. type UpdateProfileForm struct {
  88. Email string `form:"email" binding:"Required;Email;MaxSize(50)"`
  89. Website string `form:"website" binding:"MaxSize(50)"`
  90. Location string `form:"location" binding:"MaxSize(50)"`
  91. Avatar string `form:"avatar" binding:"Required;Email;MaxSize(50)"`
  92. }
  93. func (f *UpdateProfileForm) Name(field string) string {
  94. names := map[string]string{
  95. "Email": "Email address",
  96. "Website": "Website",
  97. "Location": "Location",
  98. "Avatar": "Gravatar Email",
  99. }
  100. return names[field]
  101. }
  102. func (f *UpdateProfileForm) Validate(errors *binding.Errors, req *http.Request, context martini.Context) {
  103. if req.Method == "GET" || errors.Count() == 0 {
  104. return
  105. }
  106. data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
  107. data["HasError"] = true
  108. if len(errors.Overall) > 0 {
  109. for _, err := range errors.Overall {
  110. log.Error("UpdateProfileForm.Validate: %v", err)
  111. }
  112. return
  113. }
  114. validate(errors, data, f)
  115. }
  116. type UpdatePasswdForm struct {
  117. OldPasswd string `form:"oldpasswd" binding:"Required;MinSize(6);MaxSize(30)"`
  118. NewPasswd string `form:"newpasswd" binding:"Required;MinSize(6);MaxSize(30)"`
  119. RetypePasswd string `form:"retypepasswd"`
  120. }
  121. func (f *UpdatePasswdForm) Name(field string) string {
  122. names := map[string]string{
  123. "OldPasswd": "Old password",
  124. "NewPasswd": "New password",
  125. "RetypePasswd": "Re-type password",
  126. }
  127. return names[field]
  128. }
  129. func (f *UpdatePasswdForm) Validate(errors *binding.Errors, req *http.Request, context martini.Context) {
  130. if req.Method == "GET" || errors.Count() == 0 {
  131. return
  132. }
  133. data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
  134. data["HasError"] = true
  135. if len(errors.Overall) > 0 {
  136. for _, err := range errors.Overall {
  137. log.Error("UpdatePasswdForm.Validate: %v", err)
  138. }
  139. return
  140. }
  141. validate(errors, data, f)
  142. }