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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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.HTML(200, "user/profile", ctx.Data)
  61. }
  62. func SignIn(ctx *middleware.Context, form auth.LogInForm) {
  63. ctx.Data["Title"] = "Log In"
  64. if ctx.Req.Method == "GET" {
  65. ctx.HTML(200, "user/signin", ctx.Data)
  66. return
  67. }
  68. if hasErr, ok := ctx.Data["HasError"]; ok && hasErr.(bool) {
  69. ctx.HTML(200, "user/signin", ctx.Data)
  70. return
  71. }
  72. user, err := models.LoginUserPlain(form.UserName, form.Password)
  73. if err != nil {
  74. if err.Error() == models.ErrUserNotExist.Error() {
  75. ctx.RenderWithErr("Username or password is not correct", "user/signin", &form)
  76. return
  77. }
  78. ctx.Handle(200, "user.SignIn", err)
  79. return
  80. }
  81. ctx.Session.Set("userId", user.Id)
  82. ctx.Session.Set("userName", user.Name)
  83. ctx.Redirect("/")
  84. }
  85. func SignOut(ctx *middleware.Context) {
  86. ctx.Session.Delete("userId")
  87. ctx.Session.Delete("userName")
  88. ctx.Redirect("/")
  89. }
  90. func SignUp(ctx *middleware.Context, form auth.RegisterForm) {
  91. ctx.Data["Title"] = "Sign Up"
  92. ctx.Data["PageIsSignUp"] = true
  93. if ctx.Req.Method == "GET" {
  94. ctx.HTML(200, "user/signup", ctx.Data)
  95. return
  96. }
  97. if form.Password != form.RetypePasswd {
  98. ctx.Data["HasError"] = true
  99. ctx.Data["Err_Password"] = true
  100. ctx.Data["Err_RetypePasswd"] = true
  101. ctx.Data["ErrorMsg"] = "Password and re-type password are not same"
  102. auth.AssignForm(form, ctx.Data)
  103. }
  104. if ctx.HasError() {
  105. ctx.HTML(200, "user/signup", ctx.Data)
  106. return
  107. }
  108. u := &models.User{
  109. Name: form.UserName,
  110. Email: form.Email,
  111. Passwd: form.Password,
  112. IsActive: !base.Service.RegisterEmailConfirm,
  113. }
  114. var err error
  115. if u, err = models.RegisterUser(u); err != nil {
  116. switch err.Error() {
  117. case models.ErrUserAlreadyExist.Error():
  118. ctx.RenderWithErr("Username has been already taken", "user/signup", &form)
  119. case models.ErrEmailAlreadyUsed.Error():
  120. ctx.RenderWithErr("E-mail address has been already used", "user/signup", &form)
  121. default:
  122. ctx.Handle(200, "user.SignUp", err)
  123. }
  124. return
  125. }
  126. log.Trace("%s User created: %s", ctx.Req.RequestURI, strings.ToLower(form.UserName))
  127. // Send confirmation e-mail.
  128. if base.Service.RegisterEmailConfirm {
  129. mailer.SendRegisterMail(ctx.Render, u)
  130. ctx.Data["IsSendRegisterMail"] = true
  131. ctx.Data["Email"] = u.Email
  132. ctx.Data["Hours"] = base.Service.ActiveCodeLives / 60
  133. ctx.Render.HTML(200, "user/active", ctx.Data)
  134. return
  135. }
  136. ctx.Redirect("/user/login")
  137. }
  138. func Delete(ctx *middleware.Context) {
  139. ctx.Data["Title"] = "Delete Account"
  140. ctx.Data["PageIsUserSetting"] = true
  141. ctx.Data["IsUserPageSettingDelete"] = true
  142. if ctx.Req.Method == "GET" {
  143. ctx.HTML(200, "user/delete", ctx.Data)
  144. return
  145. }
  146. tmpUser := models.User{Passwd: ctx.Query("password")}
  147. tmpUser.EncodePasswd()
  148. if len(tmpUser.Passwd) == 0 || tmpUser.Passwd != ctx.User.Passwd {
  149. ctx.Data["HasError"] = true
  150. ctx.Data["ErrorMsg"] = "Password is not correct. Make sure you are owner of this account."
  151. } else {
  152. if err := models.DeleteUser(ctx.User); err != nil {
  153. ctx.Data["HasError"] = true
  154. switch err {
  155. case models.ErrUserOwnRepos:
  156. ctx.Data["ErrorMsg"] = "Your account still have ownership of repository, you have to delete or transfer them first."
  157. default:
  158. ctx.Handle(200, "user.Delete", err)
  159. return
  160. }
  161. } else {
  162. ctx.Redirect("/")
  163. return
  164. }
  165. }
  166. ctx.HTML(200, "user/delete", ctx.Data)
  167. }
  168. const (
  169. TPL_FEED = `<i class="icon fa fa-%s"></i>
  170. <div class="info"><span class="meta">%s</span><br>%s</div>`
  171. )
  172. func Feeds(ctx *middleware.Context, form auth.FeedsForm) {
  173. actions, err := models.GetFeeds(form.UserId, form.Page*20, false)
  174. if err != nil {
  175. ctx.JSON(500, err)
  176. }
  177. feeds := make([]string, len(actions))
  178. for i := range actions {
  179. feeds[i] = fmt.Sprintf(TPL_FEED, base.ActionIcon(actions[i].OpType),
  180. base.TimeSince(actions[i].Created), base.ActionDesc(actions[i], ctx.User.AvatarLink()))
  181. }
  182. ctx.JSON(200, &feeds)
  183. }
  184. func Issues(ctx *middleware.Context) {
  185. ctx.HTML(200, "user/issues", ctx.Data)
  186. }
  187. func Pulls(ctx *middleware.Context) {
  188. ctx.HTML(200, "user/pulls", ctx.Data)
  189. }
  190. func Stars(ctx *middleware.Context) {
  191. ctx.HTML(200, "user/stars", ctx.Data)
  192. }
  193. func Activate(ctx *middleware.Context) {
  194. code := ctx.Query("code")
  195. if len(code) == 0 {
  196. ctx.Data["IsActivatePage"] = true
  197. // Resend confirmation e-mail.
  198. if base.Service.RegisterEmailConfirm {
  199. ctx.Data["Hours"] = base.Service.ActiveCodeLives / 60
  200. mailer.SendActiveMail(ctx.Render, ctx.User)
  201. } else {
  202. ctx.Data["ServiceNotEnabled"] = true
  203. }
  204. ctx.Render.HTML(200, "user/active", ctx.Data)
  205. return
  206. }
  207. }