user.go 3.0 KB

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