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.

135 lines
3.1 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. func SignedInId(session sessions.Session) int64 {
  16. userId := session.Get("userId")
  17. if userId == nil {
  18. return 0
  19. }
  20. if s, ok := userId.(int64); ok {
  21. if _, err := models.GetUserById(s); err != nil {
  22. return 0
  23. }
  24. return s
  25. }
  26. return 0
  27. }
  28. func SignedInName(session sessions.Session) string {
  29. userName := session.Get("userName")
  30. if userName == nil {
  31. return ""
  32. }
  33. if s, ok := userName.(string); ok {
  34. return s
  35. }
  36. return ""
  37. }
  38. func SignedInUser(session sessions.Session) *models.User {
  39. id := SignedInId(session)
  40. if id <= 0 {
  41. return nil
  42. }
  43. user, err := models.GetUserById(id)
  44. if err != nil {
  45. log.Error("user.SignedInUser: %v", err)
  46. return nil
  47. }
  48. return user
  49. }
  50. func IsSignedIn(session sessions.Session) bool {
  51. return SignedInId(session) > 0
  52. }
  53. type FeedsForm struct {
  54. UserId int64 `form:"userid" binding:"Required"`
  55. Page int64 `form:"p"`
  56. }
  57. type UpdateProfileForm struct {
  58. Email string `form:"email" binding:"Required;Email;MaxSize(50)"`
  59. Website string `form:"website" binding:"MaxSize(50)"`
  60. Location string `form:"location" binding:"MaxSize(50)"`
  61. Avatar string `form:"avatar" binding:"Required;Email;MaxSize(50)"`
  62. }
  63. func (f *UpdateProfileForm) Name(field string) string {
  64. names := map[string]string{
  65. "Email": "Email address",
  66. "Website": "Website",
  67. "Location": "Location",
  68. "Avatar": "Gravatar Email",
  69. }
  70. return names[field]
  71. }
  72. func (f *UpdateProfileForm) Validate(errors *binding.Errors, req *http.Request, context martini.Context) {
  73. if req.Method == "GET" || errors.Count() == 0 {
  74. return
  75. }
  76. data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
  77. data["HasError"] = true
  78. if len(errors.Overall) > 0 {
  79. for _, err := range errors.Overall {
  80. log.Error("UpdateProfileForm.Validate: %v", err)
  81. }
  82. return
  83. }
  84. validate(errors, 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(errors *binding.Errors, req *http.Request, context martini.Context) {
  100. if req.Method == "GET" || errors.Count() == 0 {
  101. return
  102. }
  103. data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
  104. data["HasError"] = true
  105. if len(errors.Overall) > 0 {
  106. for _, err := range errors.Overall {
  107. log.Error("UpdatePasswdForm.Validate: %v", err)
  108. }
  109. return
  110. }
  111. validate(errors, data, f)
  112. }