user.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. // Check auto-login.
  67. userName := ctx.GetCookie(base.CookieUserName)
  68. if len(userName) == 0 {
  69. ctx.HTML(200, "user/signin")
  70. return
  71. }
  72. isSucceed := false
  73. defer func() {
  74. if !isSucceed {
  75. log.Trace("%s auto-login cookie cleared: %s", ctx.Req.RequestURI, userName)
  76. ctx.SetCookie(base.CookieUserName, "", -1)
  77. ctx.SetCookie(base.CookieRememberName, "", -1)
  78. }
  79. }()
  80. user, err := models.GetUserByName(userName)
  81. if err != nil {
  82. ctx.HTML(200, "user/signin")
  83. return
  84. }
  85. secret := base.EncodeMd5(user.Rands + user.Passwd)
  86. value, _ := ctx.GetSecureCookie(secret, base.CookieRememberName)
  87. if value != user.Name {
  88. ctx.HTML(200, "user/signin")
  89. return
  90. }
  91. isSucceed = true
  92. ctx.Session.Set("userId", user.Id)
  93. ctx.Session.Set("userName", user.Name)
  94. ctx.Redirect("/")
  95. return
  96. }
  97. if hasErr, ok := ctx.Data["HasError"]; ok && hasErr.(bool) {
  98. ctx.HTML(200, "user/signin")
  99. return
  100. }
  101. user, err := models.LoginUserPlain(form.UserName, form.Password)
  102. if err != nil {
  103. if err == models.ErrUserNotExist {
  104. log.Trace("%s Log in failed: %s/%s", ctx.Req.RequestURI, form.UserName, form.Password)
  105. ctx.RenderWithErr("Username or password is not correct", "user/signin", &form)
  106. return
  107. }
  108. ctx.Handle(200, "user.SignIn", err)
  109. return
  110. }
  111. if form.Remember == "on" {
  112. secret := base.EncodeMd5(user.Rands + user.Passwd)
  113. days := 86400 * base.LogInRememberDays
  114. ctx.SetCookie(base.CookieUserName, user.Name, days)
  115. ctx.SetSecureCookie(secret, base.CookieRememberName, user.Name, days)
  116. }
  117. ctx.Session.Set("userId", user.Id)
  118. ctx.Session.Set("userName", user.Name)
  119. ctx.Redirect("/")
  120. }
  121. func SignOut(ctx *middleware.Context) {
  122. ctx.Session.Delete("userId")
  123. ctx.Session.Delete("userName")
  124. ctx.Redirect("/")
  125. }
  126. func SignUp(ctx *middleware.Context, form auth.RegisterForm) {
  127. ctx.Data["Title"] = "Sign Up"
  128. ctx.Data["PageIsSignUp"] = true
  129. if base.Service.DisenableRegisteration {
  130. ctx.Data["DisenableRegisteration"] = true
  131. ctx.HTML(200, "user/signup")
  132. return
  133. }
  134. if ctx.Req.Method == "GET" {
  135. ctx.HTML(200, "user/signup")
  136. return
  137. }
  138. if form.Password != form.RetypePasswd {
  139. ctx.Data["HasError"] = true
  140. ctx.Data["Err_Password"] = true
  141. ctx.Data["Err_RetypePasswd"] = true
  142. ctx.Data["ErrorMsg"] = "Password and re-type password are not same"
  143. auth.AssignForm(form, ctx.Data)
  144. }
  145. if ctx.HasError() {
  146. ctx.HTML(200, "user/signup")
  147. return
  148. }
  149. u := &models.User{
  150. Name: form.UserName,
  151. Email: form.Email,
  152. Passwd: form.Password,
  153. IsActive: !base.Service.RegisterEmailConfirm,
  154. }
  155. var err error
  156. if u, err = models.RegisterUser(u); err != nil {
  157. switch err {
  158. case models.ErrUserAlreadyExist:
  159. ctx.RenderWithErr("Username has been already taken", "user/signup", &form)
  160. case models.ErrEmailAlreadyUsed:
  161. ctx.RenderWithErr("E-mail address has been already used", "user/signup", &form)
  162. case models.ErrUserNameIllegal:
  163. ctx.RenderWithErr(models.ErrRepoNameIllegal.Error(), "user/signup", &form)
  164. default:
  165. ctx.Handle(200, "user.SignUp", err)
  166. }
  167. return
  168. }
  169. log.Trace("%s User created: %s", ctx.Req.RequestURI, strings.ToLower(form.UserName))
  170. // Send confirmation e-mail.
  171. if base.Service.RegisterEmailConfirm && u.Id > 1 {
  172. mailer.SendRegisterMail(ctx.Render, u)
  173. ctx.Data["IsSendRegisterMail"] = true
  174. ctx.Data["Email"] = u.Email
  175. ctx.Data["Hours"] = base.Service.ActiveCodeLives / 60
  176. ctx.HTML(200, "user/active")
  177. if err = ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
  178. log.Error("Set cache(MailResendLimit) fail: %v", err)
  179. }
  180. return
  181. }
  182. ctx.Redirect("/user/login")
  183. }
  184. func Delete(ctx *middleware.Context) {
  185. ctx.Data["Title"] = "Delete Account"
  186. ctx.Data["PageIsUserSetting"] = true
  187. ctx.Data["IsUserPageSettingDelete"] = true
  188. if ctx.Req.Method == "GET" {
  189. ctx.HTML(200, "user/delete")
  190. return
  191. }
  192. tmpUser := models.User{Passwd: ctx.Query("password")}
  193. tmpUser.EncodePasswd()
  194. if len(tmpUser.Passwd) == 0 || tmpUser.Passwd != ctx.User.Passwd {
  195. ctx.Data["HasError"] = true
  196. ctx.Data["ErrorMsg"] = "Password is not correct. Make sure you are owner of this account."
  197. } else {
  198. if err := models.DeleteUser(ctx.User); err != nil {
  199. ctx.Data["HasError"] = true
  200. switch err {
  201. case models.ErrUserOwnRepos:
  202. ctx.Data["ErrorMsg"] = "Your account still have ownership of repository, you have to delete or transfer them first."
  203. default:
  204. ctx.Handle(200, "user.Delete", err)
  205. return
  206. }
  207. } else {
  208. ctx.Redirect("/")
  209. return
  210. }
  211. }
  212. ctx.HTML(200, "user/delete")
  213. }
  214. const (
  215. TPL_FEED = `<i class="icon fa fa-%s"></i>
  216. <div class="info"><span class="meta">%s</span><br>%s</div>`
  217. )
  218. func Feeds(ctx *middleware.Context, form auth.FeedsForm) {
  219. actions, err := models.GetFeeds(form.UserId, form.Page*20, false)
  220. if err != nil {
  221. ctx.JSON(500, err)
  222. }
  223. feeds := make([]string, len(actions))
  224. for i := range actions {
  225. feeds[i] = fmt.Sprintf(TPL_FEED, base.ActionIcon(actions[i].OpType),
  226. base.TimeSince(actions[i].Created), base.ActionDesc(actions[i], ctx.User.AvatarLink()))
  227. }
  228. ctx.JSON(200, &feeds)
  229. }
  230. func Issues(ctx *middleware.Context) {
  231. ctx.HTML(200, "user/issues")
  232. }
  233. func Pulls(ctx *middleware.Context) {
  234. ctx.HTML(200, "user/pulls")
  235. }
  236. func Stars(ctx *middleware.Context) {
  237. ctx.HTML(200, "user/stars")
  238. }
  239. func Activate(ctx *middleware.Context) {
  240. code := ctx.Query("code")
  241. if len(code) == 0 {
  242. ctx.Data["IsActivatePage"] = true
  243. if ctx.User.IsActive {
  244. ctx.Error(404)
  245. return
  246. }
  247. // Resend confirmation e-mail.
  248. if base.Service.RegisterEmailConfirm {
  249. if ctx.Cache.IsExist("MailResendLimit_" + ctx.User.LowerName) {
  250. ctx.Data["ResendLimited"] = true
  251. } else {
  252. ctx.Data["Hours"] = base.Service.ActiveCodeLives / 60
  253. mailer.SendActiveMail(ctx.Render, ctx.User)
  254. }
  255. } else {
  256. ctx.Data["ServiceNotEnabled"] = true
  257. }
  258. ctx.HTML(200, "user/active")
  259. return
  260. }
  261. // Verify code.
  262. if user := models.VerifyUserActiveCode(code); user != nil {
  263. user.IsActive = true
  264. user.Rands = models.GetUserSalt()
  265. models.UpdateUser(user)
  266. log.Trace("%s User activated: %s", ctx.Req.RequestURI, user.LowerName)
  267. ctx.Session.Set("userId", user.Id)
  268. ctx.Session.Set("userName", user.Name)
  269. ctx.Redirect("/", 302)
  270. return
  271. }
  272. ctx.Data["IsActivateFailed"] = true
  273. ctx.HTML(200, "user/active")
  274. }