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.

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