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

126 lines
3.3 KiB

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