You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

user.go 3.4 kB

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