You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

user.go 4.5 kB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. }
  53. ctx.Render.HTML(200, "user/profile", ctx.Data)
  54. }
  55. func SignIn(ctx *middleware.Context, form auth.LogInForm) {
  56. ctx.Data["Title"] = "Log In"
  57. if ctx.Req.Method == "GET" {
  58. ctx.Render.HTML(200, "user/signin", ctx.Data)
  59. return
  60. }
  61. if hasErr, ok := ctx.Data["HasError"]; ok && hasErr.(bool) {
  62. ctx.Render.HTML(200, "user/signin", ctx.Data)
  63. return
  64. }
  65. user, err := models.LoginUserPlain(form.UserName, form.Password)
  66. if err != nil {
  67. if err.Error() == models.ErrUserNotExist.Error() {
  68. ctx.RenderWithErr("Username or password is not correct", "user/signin", &form)
  69. return
  70. }
  71. ctx.Handle(200, "user.SignIn", err)
  72. return
  73. }
  74. ctx.Session.Set("userId", user.Id)
  75. ctx.Session.Set("userName", user.Name)
  76. ctx.Render.Redirect("/")
  77. }
  78. func SignOut(r render.Render, session sessions.Session) {
  79. session.Delete("userId")
  80. session.Delete("userName")
  81. r.Redirect("/")
  82. }
  83. func SignUp(ctx *middleware.Context, form auth.RegisterForm) {
  84. ctx.Data["Title"] = "Sign Up"
  85. ctx.Data["PageIsSignUp"] = true
  86. if ctx.Req.Method == "GET" {
  87. ctx.Render.HTML(200, "user/signup", ctx.Data)
  88. return
  89. }
  90. if form.Password != form.RetypePasswd {
  91. ctx.Data["HasError"] = true
  92. ctx.Data["Err_Password"] = true
  93. ctx.Data["Err_RetypePasswd"] = true
  94. ctx.Data["ErrorMsg"] = "Password and re-type password are not same"
  95. auth.AssignForm(form, ctx.Data)
  96. }
  97. if ctx.HasError() {
  98. ctx.Render.HTML(200, "user/signup", ctx.Data)
  99. return
  100. }
  101. u := &models.User{
  102. Name: form.UserName,
  103. Email: form.Email,
  104. Passwd: form.Password,
  105. }
  106. if err := models.RegisterUser(u); err != nil {
  107. switch err.Error() {
  108. case models.ErrUserAlreadyExist.Error():
  109. ctx.RenderWithErr("Username has been already taken", "user/signup", &form)
  110. case models.ErrEmailAlreadyUsed.Error():
  111. ctx.RenderWithErr("E-mail address has been already used", "user/signup", &form)
  112. default:
  113. ctx.Handle(200, "user.SignUp", err)
  114. }
  115. return
  116. }
  117. ctx.Render.Redirect("/user/login")
  118. }
  119. func Delete(ctx *middleware.Context) {
  120. ctx.Data["Title"] = "Delete Account"
  121. if ctx.Req.Method == "GET" {
  122. ctx.Render.HTML(200, "user/delete", ctx.Data)
  123. return
  124. }
  125. if err := models.DeleteUser(ctx.User); err != nil {
  126. ctx.Data["HasError"] = true
  127. switch err.Error() {
  128. case models.ErrUserOwnRepos.Error():
  129. ctx.Data["ErrorMsg"] = "Your account still have ownership of repository, you have to delete or transfer them first."
  130. default:
  131. ctx.Handle(200, "user.Delete", err)
  132. return
  133. }
  134. }
  135. ctx.Render.HTML(200, "user/delete", ctx.Data)
  136. }
  137. const (
  138. feedTpl = `<i class="icon fa fa-%s"></i>
  139. <div class="info"><span class="meta">%s</span><br>%s</div>`
  140. )
  141. func Feeds(ctx *middleware.Context, form auth.FeedsForm) {
  142. actions, err := models.GetFeeds(form.UserId, form.Page*20, false)
  143. if err != nil {
  144. ctx.Render.JSON(500, err)
  145. }
  146. feeds := make([]string, len(actions))
  147. for i := range actions {
  148. feeds[i] = fmt.Sprintf(feedTpl, base.ActionIcon(actions[i].OpType),
  149. base.TimeSince(actions[i].Created), base.ActionDesc(actions[i]))
  150. }
  151. ctx.Render.JSON(200, &feeds)
  152. }