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.

213 lines
5.1 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
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
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
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
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. "fmt"
  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/middleware"
  14. )
  15. func Dashboard(ctx *middleware.Context) {
  16. ctx.Data["Title"] = "Dashboard"
  17. ctx.Data["PageIsUserDashboard"] = true
  18. repos, err := models.GetRepositories(&models.User{Id: ctx.User.Id})
  19. if err != nil {
  20. ctx.Handle(200, "user.Dashboard", err)
  21. return
  22. }
  23. ctx.Data["MyRepos"] = repos
  24. feeds, err := models.GetFeeds(ctx.User.Id, 0, false)
  25. if err != nil {
  26. ctx.Handle(200, "user.Dashboard", err)
  27. return
  28. }
  29. ctx.Data["Feeds"] = feeds
  30. ctx.Render.HTML(200, "user/dashboard", ctx.Data)
  31. }
  32. func Profile(ctx *middleware.Context, params martini.Params) {
  33. ctx.Data["Title"] = "Profile"
  34. // TODO: Need to check view self or others.
  35. user, err := models.GetUserByName(params["username"])
  36. if err != nil {
  37. ctx.Handle(200, "user.Profile", err)
  38. return
  39. }
  40. ctx.Data["Owner"] = user
  41. tab := ctx.Query("tab")
  42. ctx.Data["TabName"] = tab
  43. switch tab {
  44. case "activity":
  45. feeds, err := models.GetFeeds(user.Id, 0, true)
  46. if err != nil {
  47. ctx.Handle(200, "user.Profile", err)
  48. return
  49. }
  50. ctx.Data["Feeds"] = feeds
  51. default:
  52. repos, err := models.GetRepositories(user)
  53. if err != nil {
  54. ctx.Handle(200, "user.Profile", err)
  55. return
  56. }
  57. ctx.Data["Repos"] = repos
  58. }
  59. ctx.Render.HTML(200, "user/profile", ctx.Data)
  60. }
  61. func SignIn(ctx *middleware.Context, form auth.LogInForm) {
  62. ctx.Data["Title"] = "Log In"
  63. if ctx.Req.Method == "GET" {
  64. ctx.Render.HTML(200, "user/signin", ctx.Data)
  65. return
  66. }
  67. if hasErr, ok := ctx.Data["HasError"]; ok && hasErr.(bool) {
  68. ctx.Render.HTML(200, "user/signin", ctx.Data)
  69. return
  70. }
  71. user, err := models.LoginUserPlain(form.UserName, form.Password)
  72. if err != nil {
  73. if err.Error() == models.ErrUserNotExist.Error() {
  74. ctx.RenderWithErr("Username or password is not correct", "user/signin", &form)
  75. return
  76. }
  77. ctx.Handle(200, "user.SignIn", err)
  78. return
  79. }
  80. ctx.Session.Set("userId", user.Id)
  81. ctx.Session.Set("userName", user.Name)
  82. ctx.Render.Redirect("/")
  83. }
  84. func SignOut(r render.Render, session sessions.Session) {
  85. session.Delete("userId")
  86. session.Delete("userName")
  87. r.Redirect("/")
  88. }
  89. func SignUp(ctx *middleware.Context, form auth.RegisterForm) {
  90. ctx.Data["Title"] = "Sign Up"
  91. ctx.Data["PageIsSignUp"] = true
  92. if ctx.Req.Method == "GET" {
  93. ctx.Render.HTML(200, "user/signup", ctx.Data)
  94. return
  95. }
  96. if form.Password != form.RetypePasswd {
  97. ctx.Data["HasError"] = true
  98. ctx.Data["Err_Password"] = true
  99. ctx.Data["Err_RetypePasswd"] = true
  100. ctx.Data["ErrorMsg"] = "Password and re-type password are not same"
  101. auth.AssignForm(form, ctx.Data)
  102. }
  103. if ctx.HasError() {
  104. ctx.Render.HTML(200, "user/signup", ctx.Data)
  105. return
  106. }
  107. u := &models.User{
  108. Name: form.UserName,
  109. Email: form.Email,
  110. Passwd: form.Password,
  111. }
  112. if err := models.RegisterUser(u); err != nil {
  113. switch err.Error() {
  114. case models.ErrUserAlreadyExist.Error():
  115. ctx.RenderWithErr("Username has been already taken", "user/signup", &form)
  116. case models.ErrEmailAlreadyUsed.Error():
  117. ctx.RenderWithErr("E-mail address has been already used", "user/signup", &form)
  118. default:
  119. ctx.Handle(200, "user.SignUp", err)
  120. }
  121. return
  122. }
  123. ctx.Render.Redirect("/user/login")
  124. }
  125. func Delete(ctx *middleware.Context) {
  126. ctx.Data["Title"] = "Delete Account"
  127. if ctx.Req.Method == "GET" {
  128. ctx.Render.HTML(200, "user/delete", ctx.Data)
  129. return
  130. }
  131. tmpUser := models.User{Passwd: ctx.Query("password")}
  132. tmpUser.EncodePasswd()
  133. if len(tmpUser.Passwd) == 0 || tmpUser.Passwd != ctx.User.Passwd {
  134. ctx.Data["HasError"] = true
  135. ctx.Data["ErrorMsg"] = "Password is not correct. Make sure you are owner of this account."
  136. } else {
  137. if err := models.DeleteUser(ctx.User); err != nil {
  138. ctx.Data["HasError"] = true
  139. switch err {
  140. case models.ErrUserOwnRepos:
  141. ctx.Data["ErrorMsg"] = "Your account still have ownership of repository, you have to delete or transfer them first."
  142. default:
  143. ctx.Handle(200, "user.Delete", err)
  144. return
  145. }
  146. } else {
  147. ctx.Render.Redirect("/")
  148. return
  149. }
  150. }
  151. ctx.Render.HTML(200, "user/delete", ctx.Data)
  152. }
  153. const (
  154. feedTpl = `<i class="icon fa fa-%s"></i>
  155. <div class="info"><span class="meta">%s</span><br>%s</div>`
  156. )
  157. func Feeds(ctx *middleware.Context, form auth.FeedsForm) {
  158. actions, err := models.GetFeeds(form.UserId, form.Page*20, false)
  159. if err != nil {
  160. ctx.Render.JSON(500, err)
  161. }
  162. feeds := make([]string, len(actions))
  163. for i := range actions {
  164. feeds[i] = fmt.Sprintf(feedTpl, base.ActionIcon(actions[i].OpType),
  165. base.TimeSince(actions[i].Created), base.ActionDesc(actions[i], ctx.User.AvatarLink()))
  166. }
  167. ctx.Render.JSON(200, &feeds)
  168. }
  169. func Issues(ctx *middleware.Context) string {
  170. return "This is issues page"
  171. }
  172. func Pulls(ctx *middleware.Context) string {
  173. return "This is pulls page"
  174. }
  175. func Stars(ctx *middleware.Context) string {
  176. return "This is stars page"
  177. }