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.

120 lines
3.0 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
  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(session session.SessionStore) int64 {
  17. if !models.HasEngine {
  18. return 0
  19. }
  20. userId := session.Get("userId")
  21. if userId == nil {
  22. return 0
  23. }
  24. if s, ok := userId.(int64); ok {
  25. if _, err := models.GetUserById(s); err != nil {
  26. return 0
  27. }
  28. return s
  29. }
  30. return 0
  31. }
  32. // SignedInName returns the name of signed in user.
  33. func SignedInName(session session.SessionStore) string {
  34. userName := session.Get("userName")
  35. if userName == nil {
  36. return ""
  37. }
  38. if s, ok := userName.(string); ok {
  39. return s
  40. }
  41. return ""
  42. }
  43. // SignedInUser returns the user object of signed user.
  44. func SignedInUser(session session.SessionStore) *models.User {
  45. id := SignedInId(session)
  46. if id <= 0 {
  47. return nil
  48. }
  49. user, err := models.GetUserById(id)
  50. if err != nil {
  51. log.Error("user.SignedInUser: %v", err)
  52. return nil
  53. }
  54. return user
  55. }
  56. // IsSignedIn check if any user has signed in.
  57. func IsSignedIn(session session.SessionStore) bool {
  58. return SignedInId(session) > 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. }