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