user.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. repos, err := models.GetRepositories(&models.User{Id: auth.SignedInId(session)})
  19. if err != nil {
  20. data["ErrorMsg"] = err
  21. log.Error("dashboard: %v", err)
  22. r.HTML(200, "base/error", data)
  23. return
  24. }
  25. data["MyRepos"] = repos
  26. r.HTML(200, "user/dashboard", data)
  27. }
  28. func Profile(params martini.Params, r render.Render, data base.TmplData, session sessions.Session) {
  29. data["Title"] = "Profile"
  30. // TODO: Need to check view self or others.
  31. user, err := models.GetUserByName(params["username"])
  32. if err != nil {
  33. data["ErrorMsg"] = err
  34. log.Error("user.Profile: %v", err)
  35. r.HTML(200, "base/error", data)
  36. return
  37. }
  38. data["Owner"] = user
  39. r.HTML(200, "user/profile", data)
  40. }
  41. func SignIn(form auth.LogInForm, data base.TmplData, req *http.Request, r render.Render, session sessions.Session) {
  42. data["Title"] = "Log In"
  43. if req.Method == "GET" {
  44. r.HTML(200, "user/signin", data)
  45. return
  46. }
  47. if hasErr, ok := data["HasError"]; ok && hasErr.(bool) {
  48. r.HTML(200, "user/signin", data)
  49. return
  50. }
  51. user, err := models.LoginUserPlain(form.UserName, form.Password)
  52. if err != nil {
  53. if err.Error() == models.ErrUserNotExist.Error() {
  54. data["HasError"] = true
  55. data["ErrorMsg"] = "Username or password is not correct"
  56. auth.AssignForm(form, data)
  57. r.HTML(200, "user/signin", data)
  58. return
  59. }
  60. data["ErrorMsg"] = err
  61. log.Error("user.SignIn: %v", err)
  62. r.HTML(200, "base/error", data)
  63. return
  64. }
  65. session.Set("userId", user.Id)
  66. session.Set("userName", user.Name)
  67. r.Redirect("/")
  68. }
  69. func SignOut(r render.Render, session sessions.Session) {
  70. session.Delete("userId")
  71. session.Delete("userName")
  72. r.Redirect("/")
  73. }
  74. func SignUp(form auth.RegisterForm, data base.TmplData, req *http.Request, r render.Render) {
  75. data["Title"] = "Sign Up"
  76. data["PageIsSignUp"] = true
  77. if req.Method == "GET" {
  78. r.HTML(200, "user/signup", data)
  79. return
  80. }
  81. if form.Password != form.RetypePasswd {
  82. data["HasError"] = true
  83. data["Err_Password"] = true
  84. data["Err_RetypePasswd"] = true
  85. data["ErrorMsg"] = "Password and re-type password are not same"
  86. auth.AssignForm(form, data)
  87. }
  88. if hasErr, ok := data["HasError"]; ok && hasErr.(bool) {
  89. r.HTML(200, "user/signup", data)
  90. return
  91. }
  92. u := &models.User{
  93. Name: form.UserName,
  94. Email: form.Email,
  95. Passwd: form.Password,
  96. }
  97. if err := models.RegisterUser(u); err != nil {
  98. data["HasError"] = true
  99. auth.AssignForm(form, data)
  100. switch err.Error() {
  101. case models.ErrUserAlreadyExist.Error():
  102. data["Err_Username"] = true
  103. data["ErrorMsg"] = "Username has been already taken"
  104. r.HTML(200, "user/signup", data)
  105. case models.ErrEmailAlreadyUsed.Error():
  106. data["Err_Email"] = true
  107. data["ErrorMsg"] = "E-mail address has been already used"
  108. r.HTML(200, "user/signup", data)
  109. default:
  110. data["ErrorMsg"] = err
  111. log.Error("user.SignUp: %v", data)
  112. r.HTML(200, "base/error", nil)
  113. }
  114. return
  115. }
  116. r.Redirect("/user/login")
  117. }
  118. func Delete(data base.TmplData, req *http.Request, session sessions.Session, r render.Render) {
  119. data["Title"] = "Delete Account"
  120. if req.Method == "GET" {
  121. r.HTML(200, "user/delete", data)
  122. return
  123. }
  124. id := auth.SignedInId(session)
  125. u := &models.User{Id: id}
  126. if err := models.DeleteUser(u); err != nil {
  127. data["HasError"] = true
  128. switch err.Error() {
  129. case models.ErrUserOwnRepos.Error():
  130. data["ErrorMsg"] = "Your account still have ownership of repository, you have to delete or transfer them first."
  131. default:
  132. data["ErrorMsg"] = err
  133. log.Error("user.Delete: %v", data)
  134. r.HTML(200, "base/error", nil)
  135. return
  136. }
  137. }
  138. r.HTML(200, "user/delete", data)
  139. }
  140. func Feeds(form auth.FeedsForm, r render.Render) {
  141. actions, err := models.GetFeeds(form.UserId, form.Offset)
  142. if err != nil {
  143. r.JSON(500, err)
  144. }
  145. r.JSON(200, actions)
  146. }