user.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. )
  14. // SignedInId returns the id of signed in user.
  15. func SignedInId(session session.SessionStore) int64 {
  16. if !models.HasEngine {
  17. return 0
  18. }
  19. userId := session.Get("userId")
  20. if userId == nil {
  21. return 0
  22. }
  23. if s, ok := userId.(int64); ok {
  24. if _, err := models.GetUserById(s); err != nil {
  25. return 0
  26. }
  27. return s
  28. }
  29. return 0
  30. }
  31. // SignedInName returns the name of signed in user.
  32. func SignedInName(session session.SessionStore) string {
  33. userName := session.Get("userName")
  34. if userName == nil {
  35. return ""
  36. }
  37. if s, ok := userName.(string); ok {
  38. return s
  39. }
  40. return ""
  41. }
  42. // SignedInUser returns the user object of signed user.
  43. func SignedInUser(session session.SessionStore) *models.User {
  44. id := SignedInId(session)
  45. if id <= 0 {
  46. return nil
  47. }
  48. user, err := models.GetUserById(id)
  49. if err != nil {
  50. log.Error("user.SignedInUser: %v", err)
  51. return nil
  52. }
  53. return user
  54. }
  55. // IsSignedIn check if any user has signed in.
  56. func IsSignedIn(session session.SessionStore) bool {
  57. return SignedInId(session) > 0
  58. }
  59. type FeedsForm struct {
  60. UserId int64 `form:"userid" binding:"Required"`
  61. Page int64 `form:"p"`
  62. }
  63. type UpdateProfileForm struct {
  64. UserName string `form:"username" binding:"Required;AlphaDash;MaxSize(30)"`
  65. FullName string `form:"fullname" binding:"MaxSize(40)"`
  66. Email string `form:"email" binding:"Required;Email;MaxSize(50)"`
  67. Website string `form:"website" binding:"MaxSize(50)"`
  68. Location string `form:"location" binding:"MaxSize(50)"`
  69. Avatar string `form:"avatar" binding:"Required;Email;MaxSize(50)"`
  70. }
  71. func (f *UpdateProfileForm) Name(field string) string {
  72. names := map[string]string{
  73. "UserName": "Username",
  74. "Email": "E-mail address",
  75. "Website": "Website",
  76. "Location": "Location",
  77. "Avatar": "Gravatar Email",
  78. }
  79. return names[field]
  80. }
  81. func (f *UpdateProfileForm) Validate(errors *base.BindingErrors, req *http.Request, context martini.Context) {
  82. if req.Method == "GET" || errors.Count() == 0 {
  83. return
  84. }
  85. data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
  86. data["HasError"] = true
  87. if len(errors.Overall) > 0 {
  88. for _, err := range errors.Overall {
  89. log.Error("UpdateProfileForm.Validate: %v", err)
  90. }
  91. return
  92. }
  93. validate(errors, data, f)
  94. }
  95. type UpdatePasswdForm struct {
  96. OldPasswd string `form:"oldpasswd" binding:"Required;MinSize(6);MaxSize(30)"`
  97. NewPasswd string `form:"newpasswd" binding:"Required;MinSize(6);MaxSize(30)"`
  98. RetypePasswd string `form:"retypepasswd"`
  99. }
  100. func (f *UpdatePasswdForm) Name(field string) string {
  101. names := map[string]string{
  102. "OldPasswd": "Old password",
  103. "NewPasswd": "New password",
  104. "RetypePasswd": "Re-type password",
  105. }
  106. return names[field]
  107. }
  108. func (f *UpdatePasswdForm) Validate(errors *base.BindingErrors, req *http.Request, context martini.Context) {
  109. if req.Method == "GET" || errors.Count() == 0 {
  110. return
  111. }
  112. data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
  113. data["HasError"] = true
  114. if len(errors.Overall) > 0 {
  115. for _, err := range errors.Overall {
  116. log.Error("UpdatePasswdForm.Validate: %v", err)
  117. }
  118. return
  119. }
  120. validate(errors, data, f)
  121. }