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.

139 lines
3.3 KiB

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/codegangsta/martini"
  9. "github.com/martini-contrib/sessions"
  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 sessions.Session) 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 sessions.Session) 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 sessions.Session) *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 sessions.Session) 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. }