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.2 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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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/gogits/gogs/models"
  10. "github.com/gogits/gogs/modules/auth"
  11. "github.com/gogits/gogs/modules/base"
  12. "github.com/gogits/gogs/modules/log"
  13. "github.com/gogits/gogs/modules/mailer"
  14. "github.com/gogits/gogs/modules/middleware"
  15. )
  16. func Dashboard(ctx *middleware.Context) {
  17. ctx.Data["Title"] = "Dashboard"
  18. ctx.Data["PageIsUserDashboard"] = true
  19. repos, err := models.GetRepositories(&models.User{Id: ctx.User.Id})
  20. if err != nil {
  21. ctx.Handle(200, "user.Dashboard", err)
  22. return
  23. }
  24. ctx.Data["MyRepos"] = repos
  25. feeds, err := models.GetFeeds(ctx.User.Id, 0, false)
  26. if err != nil {
  27. ctx.Handle(200, "user.Dashboard", err)
  28. return
  29. }
  30. ctx.Data["Feeds"] = feeds
  31. ctx.HTML(200, "user/dashboard", ctx.Data)
  32. }
  33. func Profile(ctx *middleware.Context, params martini.Params) {
  34. ctx.Data["Title"] = "Profile"
  35. // TODO: Need to check view self or others.
  36. user, err := models.GetUserByName(params["username"])
  37. if err != nil {
  38. ctx.Handle(200, "user.Profile", err)
  39. return
  40. }
  41. ctx.Data["Owner"] = user
  42. tab := ctx.Query("tab")
  43. ctx.Data["TabName"] = tab
  44. switch tab {
  45. case "activity":
  46. feeds, err := models.GetFeeds(user.Id, 0, true)
  47. if err != nil {
  48. ctx.Handle(200, "user.Profile", err)
  49. return
  50. }
  51. ctx.Data["Feeds"] = feeds
  52. default:
  53. repos, err := models.GetRepositories(user)
  54. if err != nil {
  55. ctx.Handle(200, "user.Profile", err)
  56. return
  57. }
  58. ctx.Data["Repos"] = repos
  59. }
  60. ctx.Data["PageIsUserProfile"] = true
  61. ctx.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.HTML(200, "user/signin", ctx.Data)
  67. return
  68. }
  69. if hasErr, ok := ctx.Data["HasError"]; ok && hasErr.(bool) {
  70. ctx.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.Redirect("/")
  85. }
  86. func SignOut(ctx *middleware.Context) {
  87. ctx.Session.Delete("userId")
  88. ctx.Session.Delete("userName")
  89. ctx.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.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.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. mailer.SendRegisterMail(ctx.Render, u)
  131. ctx.Data["IsSendRegisterMail"] = true
  132. ctx.Data["Email"] = u.Email
  133. ctx.Data["Hours"] = base.Service.ActiveCodeLives / 60
  134. ctx.Render.HTML(200, "user/active", ctx.Data)
  135. return
  136. }
  137. ctx.Redirect("/user/login")
  138. }
  139. func Delete(ctx *middleware.Context) {
  140. ctx.Data["Title"] = "Delete Account"
  141. ctx.Data["PageIsUserSetting"] = true
  142. ctx.Data["IsUserPageSettingDelete"] = true
  143. if ctx.Req.Method == "GET" {
  144. ctx.HTML(200, "user/delete", ctx.Data)
  145. return
  146. }
  147. tmpUser := models.User{Passwd: ctx.Query("password")}
  148. tmpUser.EncodePasswd()
  149. if len(tmpUser.Passwd) == 0 || tmpUser.Passwd != ctx.User.Passwd {
  150. ctx.Data["HasError"] = true
  151. ctx.Data["ErrorMsg"] = "Password is not correct. Make sure you are owner of this account."
  152. } else {
  153. if err := models.DeleteUser(ctx.User); err != nil {
  154. ctx.Data["HasError"] = true
  155. switch err {
  156. case models.ErrUserOwnRepos:
  157. ctx.Data["ErrorMsg"] = "Your account still have ownership of repository, you have to delete or transfer them first."
  158. default:
  159. ctx.Handle(200, "user.Delete", err)
  160. return
  161. }
  162. } else {
  163. ctx.Redirect("/")
  164. return
  165. }
  166. }
  167. ctx.HTML(200, "user/delete", ctx.Data)
  168. }
  169. const (
  170. TPL_FEED = `<i class="icon fa fa-%s"></i>
  171. <div class="info"><span class="meta">%s</span><br>%s</div>`
  172. )
  173. func Feeds(ctx *middleware.Context, form auth.FeedsForm) {
  174. actions, err := models.GetFeeds(form.UserId, form.Page*20, false)
  175. if err != nil {
  176. ctx.JSON(500, err)
  177. }
  178. feeds := make([]string, len(actions))
  179. for i := range actions {
  180. feeds[i] = fmt.Sprintf(TPL_FEED, base.ActionIcon(actions[i].OpType),
  181. base.TimeSince(actions[i].Created), base.ActionDesc(actions[i], ctx.User.AvatarLink()))
  182. }
  183. ctx.JSON(200, &feeds)
  184. }
  185. func Issues(ctx *middleware.Context) {
  186. ctx.HTML(200, "user/issues", ctx.Data)
  187. }
  188. func Pulls(ctx *middleware.Context) {
  189. ctx.HTML(200, "user/pulls", ctx.Data)
  190. }
  191. func Stars(ctx *middleware.Context) {
  192. ctx.HTML(200, "user/stars", ctx.Data)
  193. }
  194. func Activate(ctx *middleware.Context) {
  195. code := ctx.Query("code")
  196. if len(code) == 0 {
  197. ctx.Data["IsActivatePage"] = true
  198. // Resend confirmation e-mail.
  199. if base.Service.RegisterEmailConfirm {
  200. ctx.Data["Hours"] = base.Service.ActiveCodeLives / 60
  201. mailer.SendActiveMail(ctx.Render, ctx.User)
  202. } else {
  203. ctx.Data["ServiceNotEnabled"] = true
  204. }
  205. ctx.Render.HTML(200, "user/active", ctx.Data)
  206. return
  207. }
  208. }