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.

149 lines
3.6 KiB

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 user
  5. import (
  6. "net/http"
  7. "github.com/codegangsta/martini"
  8. "github.com/martini-contrib/render"
  9. "github.com/martini-contrib/sessions"
  10. "github.com/gogits/gogs/models"
  11. "github.com/gogits/gogs/modules/auth"
  12. "github.com/gogits/gogs/modules/base"
  13. "github.com/gogits/gogs/modules/log"
  14. )
  15. func Dashboard(r render.Render, data base.TmplData, session sessions.Session) {
  16. data["Title"] = "Dashboard"
  17. data["PageIsUserDashboard"] = true
  18. r.HTML(200, "user/dashboard", data)
  19. }
  20. func Profile(params martini.Params, r render.Render, data base.TmplData, session sessions.Session) {
  21. data["Title"] = "Profile"
  22. // TODO: Need to check view self or others.
  23. user, err := models.GetUserByName(params["username"])
  24. if err != nil {
  25. data["ErrorMsg"] = err
  26. log.Error("user.Profile: %v", err)
  27. r.HTML(200, "base/error", data)
  28. return
  29. }
  30. data["Avatar"] = user.Avatar
  31. data["Username"] = user.Name
  32. r.HTML(200, "user/profile", data)
  33. }
  34. func SignIn(form auth.LogInForm, data base.TmplData, req *http.Request, r render.Render, session sessions.Session) {
  35. data["Title"] = "Log In"
  36. if req.Method == "GET" {
  37. r.HTML(200, "user/signin", data)
  38. return
  39. }
  40. if hasErr, ok := data["HasError"]; ok && hasErr.(bool) {
  41. r.HTML(200, "user/signin", data)
  42. return
  43. }
  44. user, err := models.LoginUserPlain(form.UserName, form.Password)
  45. if err != nil {
  46. if err.Error() == models.ErrUserNotExist.Error() {
  47. data["HasError"] = true
  48. data["ErrorMsg"] = "Username or password is not correct"
  49. auth.AssignForm(form, data)
  50. r.HTML(200, "user/signin", data)
  51. return
  52. }
  53. data["ErrorMsg"] = err
  54. log.Error("user.SignIn: %v", err)
  55. r.HTML(200, "base/error", data)
  56. return
  57. }
  58. session.Set("userId", user.Id)
  59. session.Set("userName", user.Name)
  60. r.Redirect("/")
  61. }
  62. func SignOut(r render.Render, session sessions.Session) {
  63. session.Delete("userId")
  64. session.Delete("userName")
  65. r.Redirect("/")
  66. }
  67. func SignUp(form auth.RegisterForm, data base.TmplData, req *http.Request, r render.Render) {
  68. data["Title"] = "Sign Up"
  69. data["PageIsSignUp"] = true
  70. if req.Method == "GET" {
  71. r.HTML(200, "user/signup", data)
  72. return
  73. }
  74. if form.Password != form.RetypePasswd {
  75. data["HasError"] = true
  76. data["Err_Password"] = true
  77. data["Err_RetypePasswd"] = true
  78. data["ErrorMsg"] = "Password and re-type password are not same"
  79. auth.AssignForm(form, data)
  80. }
  81. if hasErr, ok := data["HasError"]; ok && hasErr.(bool) {
  82. r.HTML(200, "user/signup", data)
  83. return
  84. }
  85. u := &models.User{
  86. Name: form.UserName,
  87. Email: form.Email,
  88. Passwd: form.Password,
  89. }
  90. if err := models.RegisterUser(u); err != nil {
  91. data["HasError"] = true
  92. auth.AssignForm(form, data)
  93. switch err.Error() {
  94. case models.ErrUserAlreadyExist.Error():
  95. data["Err_Username"] = true
  96. data["ErrorMsg"] = "Username has been already taken"
  97. r.HTML(200, "user/signup", data)
  98. case models.ErrEmailAlreadyUsed.Error():
  99. data["Err_Email"] = true
  100. data["ErrorMsg"] = "E-mail address has been already used"
  101. r.HTML(200, "user/signup", data)
  102. default:
  103. data["ErrorMsg"] = err
  104. log.Error("user.SignUp: %v", data)
  105. r.HTML(200, "base/error", nil)
  106. }
  107. return
  108. }
  109. r.Redirect("/user/login")
  110. }
  111. // TODO: unfinished
  112. func Delete(data base.TmplData, req *http.Request, r render.Render) {
  113. data["Title"] = "Delete Account"
  114. if req.Method == "GET" {
  115. r.HTML(200, "user/delete", data)
  116. return
  117. }
  118. u := &models.User{}
  119. err := models.DeleteUser(u)
  120. data["ErrorMsg"] = err
  121. log.Error("user.Delete: %v", data)
  122. r.HTML(200, "base/error", nil)
  123. }