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.5 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
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. Email string `form:"email" binding:"Required;Email;MaxSize(50)"`
  66. Website string `form:"website" binding:"MaxSize(50)"`
  67. Location string `form:"location" binding:"MaxSize(50)"`
  68. Avatar string `form:"avatar" binding:"Required;Email;MaxSize(50)"`
  69. }
  70. func (f *UpdateProfileForm) Name(field string) string {
  71. names := map[string]string{
  72. "UserName": "Username",
  73. "Email": "E-mail address",
  74. "Website": "Website",
  75. "Location": "Location",
  76. "Avatar": "Gravatar Email",
  77. }
  78. return names[field]
  79. }
  80. func (f *UpdateProfileForm) Validate(errors *base.BindingErrors, req *http.Request, context martini.Context) {
  81. if req.Method == "GET" || errors.Count() == 0 {
  82. return
  83. }
  84. data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
  85. data["HasError"] = true
  86. if len(errors.Overall) > 0 {
  87. for _, err := range errors.Overall {
  88. log.Error("UpdateProfileForm.Validate: %v", err)
  89. }
  90. return
  91. }
  92. validate(errors, data, f)
  93. }
  94. type UpdatePasswdForm struct {
  95. OldPasswd string `form:"oldpasswd" binding:"Required;MinSize(6);MaxSize(30)"`
  96. NewPasswd string `form:"newpasswd" binding:"Required;MinSize(6);MaxSize(30)"`
  97. RetypePasswd string `form:"retypepasswd"`
  98. }
  99. func (f *UpdatePasswdForm) Name(field string) string {
  100. names := map[string]string{
  101. "OldPasswd": "Old password",
  102. "NewPasswd": "New password",
  103. "RetypePasswd": "Re-type password",
  104. }
  105. return names[field]
  106. }
  107. func (f *UpdatePasswdForm) Validate(errors *base.BindingErrors, req *http.Request, context martini.Context) {
  108. if req.Method == "GET" || errors.Count() == 0 {
  109. return
  110. }
  111. data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
  112. data["HasError"] = true
  113. if len(errors.Overall) > 0 {
  114. for _, err := range errors.Overall {
  115. log.Error("UpdatePasswdForm.Validate: %v", err)
  116. }
  117. return
  118. }
  119. validate(errors, data, f)
  120. }