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.6 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
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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")
  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")
  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")
  67. return
  68. }
  69. if hasErr, ok := ctx.Data["HasError"]; ok && hasErr.(bool) {
  70. ctx.HTML(200, "user/signin")
  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")
  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")
  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 {
  118. case models.ErrUserAlreadyExist:
  119. ctx.RenderWithErr("Username has been already taken", "user/signup", &form)
  120. case models.ErrEmailAlreadyUsed:
  121. ctx.RenderWithErr("E-mail address has been already used", "user/signup", &form)
  122. case models.ErrUserNameIllegal:
  123. ctx.RenderWithErr(models.ErrRepoNameIllegal.Error(), "user/signup", &form)
  124. default:
  125. ctx.Handle(200, "user.SignUp", err)
  126. }
  127. return
  128. }
  129. log.Trace("%s User created: %s", ctx.Req.RequestURI, strings.ToLower(form.UserName))
  130. // Send confirmation e-mail.
  131. if base.Service.RegisterEmailConfirm && u.Id > 1 {
  132. mailer.SendRegisterMail(ctx.Render, u)
  133. ctx.Data["IsSendRegisterMail"] = true
  134. ctx.Data["Email"] = u.Email
  135. ctx.Data["Hours"] = base.Service.ActiveCodeLives / 60
  136. ctx.HTML(200, "user/active")
  137. return
  138. }
  139. ctx.Redirect("/user/login")
  140. }
  141. func Delete(ctx *middleware.Context) {
  142. ctx.Data["Title"] = "Delete Account"
  143. ctx.Data["PageIsUserSetting"] = true
  144. ctx.Data["IsUserPageSettingDelete"] = true
  145. if ctx.Req.Method == "GET" {
  146. ctx.HTML(200, "user/delete")
  147. return
  148. }
  149. tmpUser := models.User{Passwd: ctx.Query("password")}
  150. tmpUser.EncodePasswd()
  151. if len(tmpUser.Passwd) == 0 || tmpUser.Passwd != ctx.User.Passwd {
  152. ctx.Data["HasError"] = true
  153. ctx.Data["ErrorMsg"] = "Password is not correct. Make sure you are owner of this account."
  154. } else {
  155. if err := models.DeleteUser(ctx.User); err != nil {
  156. ctx.Data["HasError"] = true
  157. switch err {
  158. case models.ErrUserOwnRepos:
  159. ctx.Data["ErrorMsg"] = "Your account still have ownership of repository, you have to delete or transfer them first."
  160. default:
  161. ctx.Handle(200, "user.Delete", err)
  162. return
  163. }
  164. } else {
  165. ctx.Redirect("/")
  166. return
  167. }
  168. }
  169. ctx.HTML(200, "user/delete")
  170. }
  171. const (
  172. TPL_FEED = `<i class="icon fa fa-%s"></i>
  173. <div class="info"><span class="meta">%s</span><br>%s</div>`
  174. )
  175. func Feeds(ctx *middleware.Context, form auth.FeedsForm) {
  176. actions, err := models.GetFeeds(form.UserId, form.Page*20, false)
  177. if err != nil {
  178. ctx.JSON(500, err)
  179. }
  180. feeds := make([]string, len(actions))
  181. for i := range actions {
  182. feeds[i] = fmt.Sprintf(TPL_FEED, base.ActionIcon(actions[i].OpType),
  183. base.TimeSince(actions[i].Created), base.ActionDesc(actions[i], ctx.User.AvatarLink()))
  184. }
  185. ctx.JSON(200, &feeds)
  186. }
  187. func Issues(ctx *middleware.Context) {
  188. ctx.HTML(200, "user/issues")
  189. }
  190. func Pulls(ctx *middleware.Context) {
  191. ctx.HTML(200, "user/pulls")
  192. }
  193. func Stars(ctx *middleware.Context) {
  194. ctx.HTML(200, "user/stars")
  195. }
  196. func Activate(ctx *middleware.Context) {
  197. code := ctx.Query("code")
  198. if len(code) == 0 {
  199. ctx.Data["IsActivatePage"] = true
  200. if ctx.User.IsActive {
  201. ctx.Error(404)
  202. return
  203. }
  204. // Resend confirmation e-mail.
  205. if base.Service.RegisterEmailConfirm {
  206. ctx.Data["Hours"] = base.Service.ActiveCodeLives / 60
  207. mailer.SendActiveMail(ctx.Render, ctx.User)
  208. } else {
  209. ctx.Data["ServiceNotEnabled"] = true
  210. }
  211. ctx.HTML(200, "user/active")
  212. return
  213. }
  214. // Verify code.
  215. if user := models.VerifyUserActiveCode(code); user != nil {
  216. user.IsActive = true
  217. user.Rands = models.GetUserSalt()
  218. models.UpdateUser(user)
  219. log.Trace("%s User activated: %s", ctx.Req.RequestURI, user.LowerName)
  220. ctx.Session.Set("userId", user.Id)
  221. ctx.Session.Set("userName", user.Name)
  222. ctx.Redirect("/", 302)
  223. return
  224. }
  225. ctx.Data["IsActivateFailed"] = true
  226. ctx.HTML(200, "user/active")
  227. }