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 12 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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. "net/url"
  7. "strings"
  8. "github.com/gogits/gogs/models"
  9. "github.com/gogits/gogs/modules/auth"
  10. "github.com/gogits/gogs/modules/base"
  11. "github.com/gogits/gogs/modules/log"
  12. "github.com/gogits/gogs/modules/mailer"
  13. "github.com/gogits/gogs/modules/middleware"
  14. )
  15. func SignIn(ctx *middleware.Context) {
  16. ctx.Data["Title"] = "Log In"
  17. if _, ok := ctx.Session.Get("socialId").(int64); ok {
  18. ctx.Data["IsSocialLogin"] = true
  19. ctx.HTML(200, "user/signin")
  20. return
  21. }
  22. if base.OauthService != nil {
  23. ctx.Data["OauthEnabled"] = true
  24. ctx.Data["OauthService"] = base.OauthService
  25. }
  26. // Check auto-login.
  27. userName := ctx.GetCookie(base.CookieUserName)
  28. if len(userName) == 0 {
  29. ctx.HTML(200, "user/signin")
  30. return
  31. }
  32. isSucceed := false
  33. defer func() {
  34. if !isSucceed {
  35. log.Trace("user.SignIn(auto-login cookie cleared): %s", userName)
  36. ctx.SetCookie(base.CookieUserName, "", -1)
  37. ctx.SetCookie(base.CookieRememberName, "", -1)
  38. return
  39. }
  40. }()
  41. user, err := models.GetUserByName(userName)
  42. if err != nil {
  43. ctx.HTML(500, "user/signin")
  44. return
  45. }
  46. secret := base.EncodeMd5(user.Rands + user.Passwd)
  47. value, _ := ctx.GetSecureCookie(secret, base.CookieRememberName)
  48. if value != user.Name {
  49. ctx.HTML(500, "user/signin")
  50. return
  51. }
  52. isSucceed = true
  53. ctx.Session.Set("userId", user.Id)
  54. ctx.Session.Set("userName", user.Name)
  55. if redirectTo, _ := url.QueryUnescape(ctx.GetCookie("redirect_to")); len(redirectTo) > 0 {
  56. ctx.SetCookie("redirect_to", "", -1)
  57. ctx.Redirect(redirectTo)
  58. return
  59. }
  60. ctx.Redirect("/")
  61. }
  62. func SignInPost(ctx *middleware.Context, form auth.LogInForm) {
  63. ctx.Data["Title"] = "Log In"
  64. sid, isOauth := ctx.Session.Get("socialId").(int64)
  65. if isOauth {
  66. ctx.Data["IsSocialLogin"] = true
  67. } else if base.OauthService != nil {
  68. ctx.Data["OauthEnabled"] = true
  69. ctx.Data["OauthService"] = base.OauthService
  70. }
  71. if ctx.HasError() {
  72. ctx.HTML(200, "user/signin")
  73. return
  74. }
  75. var user *models.User
  76. var err error
  77. if base.Service.LdapAuth {
  78. user, err = models.LoginUserLdap(form.UserName, form.Password)
  79. if err != nil {
  80. log.Error("Fail to login through LDAP: %v", err)
  81. }
  82. }
  83. // try local if not LDAP or it's failed
  84. if !base.Service.LdapAuth || err != nil {
  85. user, err = models.LoginUserPlain(form.UserName, form.Password)
  86. }
  87. if err != nil {
  88. if err == models.ErrUserNotExist {
  89. log.Trace("%s Log in failed: %s/%s", ctx.Req.RequestURI, form.UserName, form.Password)
  90. ctx.RenderWithErr("Username or password is not correct", "user/signin", &form)
  91. return
  92. }
  93. ctx.Handle(500, "user.SignIn", err)
  94. return
  95. }
  96. if form.Remember == "on" {
  97. secret := base.EncodeMd5(user.Rands + user.Passwd)
  98. days := 86400 * base.LogInRememberDays
  99. ctx.SetCookie(base.CookieUserName, user.Name, days)
  100. ctx.SetSecureCookie(secret, base.CookieRememberName, user.Name, days)
  101. }
  102. // Bind with social account.
  103. if isOauth {
  104. if err = models.BindUserOauth2(user.Id, sid); err != nil {
  105. if err == models.ErrOauth2RecordNotExist {
  106. ctx.Handle(404, "user.SignInPost(GetOauth2ById)", err)
  107. } else {
  108. ctx.Handle(500, "user.SignInPost(GetOauth2ById)", err)
  109. }
  110. return
  111. }
  112. ctx.Session.Delete("socialId")
  113. log.Trace("%s OAuth binded: %s -> %d", ctx.Req.RequestURI, form.UserName, sid)
  114. }
  115. ctx.Session.Set("userId", user.Id)
  116. ctx.Session.Set("userName", user.Name)
  117. if redirectTo, _ := url.QueryUnescape(ctx.GetCookie("redirect_to")); len(redirectTo) > 0 {
  118. ctx.SetCookie("redirect_to", "", -1)
  119. ctx.Redirect(redirectTo)
  120. return
  121. }
  122. ctx.Redirect("/")
  123. }
  124. func oauthSignInPost(ctx *middleware.Context, sid int64) {
  125. ctx.Data["Title"] = "OAuth Sign Up"
  126. ctx.Data["PageIsSignUp"] = true
  127. if _, err := models.GetOauth2ById(sid); err != nil {
  128. if err == models.ErrOauth2RecordNotExist {
  129. ctx.Handle(404, "user.oauthSignUp(GetOauth2ById)", err)
  130. } else {
  131. ctx.Handle(500, "user.oauthSignUp(GetOauth2ById)", err)
  132. }
  133. return
  134. }
  135. ctx.Data["IsSocialLogin"] = true
  136. ctx.Data["username"] = ctx.Session.Get("socialName")
  137. ctx.Data["email"] = ctx.Session.Get("socialEmail")
  138. log.Trace("user.oauthSignUp(social ID): %v", ctx.Session.Get("socialId"))
  139. ctx.HTML(200, "user/signup")
  140. }
  141. func SignOut(ctx *middleware.Context) {
  142. ctx.Session.Delete("userId")
  143. ctx.Session.Delete("userName")
  144. ctx.Session.Delete("socialId")
  145. ctx.Session.Delete("socialName")
  146. ctx.Session.Delete("socialEmail")
  147. ctx.SetCookie(base.CookieUserName, "", -1)
  148. ctx.SetCookie(base.CookieRememberName, "", -1)
  149. ctx.Redirect("/")
  150. }
  151. func SignUp(ctx *middleware.Context) {
  152. ctx.Data["Title"] = "Sign Up"
  153. ctx.Data["PageIsSignUp"] = true
  154. if base.Service.DisableRegistration {
  155. ctx.Data["DisableRegistration"] = true
  156. ctx.HTML(200, "user/signup")
  157. return
  158. }
  159. if sid, ok := ctx.Session.Get("socialId").(int64); ok {
  160. oauthSignUp(ctx, sid)
  161. return
  162. }
  163. ctx.HTML(200, "user/signup")
  164. }
  165. func oauthSignUp(ctx *middleware.Context, sid int64) {
  166. ctx.Data["Title"] = "OAuth Sign Up"
  167. ctx.Data["PageIsSignUp"] = true
  168. if _, err := models.GetOauth2ById(sid); err != nil {
  169. if err == models.ErrOauth2RecordNotExist {
  170. ctx.Handle(404, "user.oauthSignUp(GetOauth2ById)", err)
  171. } else {
  172. ctx.Handle(500, "user.oauthSignUp(GetOauth2ById)", err)
  173. }
  174. return
  175. }
  176. ctx.Data["IsSocialLogin"] = true
  177. ctx.Data["username"] = strings.Replace(ctx.Session.Get("socialName").(string), " ", "", -1)
  178. ctx.Data["email"] = ctx.Session.Get("socialEmail")
  179. log.Trace("user.oauthSignUp(social ID): %v", ctx.Session.Get("socialId"))
  180. ctx.HTML(200, "user/signup")
  181. }
  182. func SignUpPost(ctx *middleware.Context, form auth.RegisterForm) {
  183. ctx.Data["Title"] = "Sign Up"
  184. ctx.Data["PageIsSignUp"] = true
  185. if base.Service.DisableRegistration {
  186. ctx.Handle(403, "user.SignUpPost", nil)
  187. return
  188. }
  189. sid, isOauth := ctx.Session.Get("socialId").(int64)
  190. if isOauth {
  191. ctx.Data["IsSocialLogin"] = true
  192. }
  193. if form.Password != form.RetypePasswd {
  194. ctx.Data["HasError"] = true
  195. ctx.Data["Err_Password"] = true
  196. ctx.Data["Err_RetypePasswd"] = true
  197. ctx.Data["ErrorMsg"] = "Password and re-type password are not same"
  198. auth.AssignForm(form, ctx.Data)
  199. }
  200. if ctx.HasError() {
  201. ctx.HTML(200, "user/signup")
  202. return
  203. }
  204. u := &models.User{
  205. Name: form.UserName,
  206. Email: form.Email,
  207. Passwd: form.Password,
  208. IsActive: !base.Service.RegisterEmailConfirm || isOauth,
  209. }
  210. var err error
  211. if u, err = models.RegisterUser(u); err != nil {
  212. switch err {
  213. case models.ErrUserAlreadyExist:
  214. ctx.RenderWithErr("Username has been already taken", "user/signup", &form)
  215. case models.ErrEmailAlreadyUsed:
  216. ctx.RenderWithErr("E-mail address has been already used", "user/signup", &form)
  217. case models.ErrUserNameIllegal:
  218. ctx.RenderWithErr(models.ErrRepoNameIllegal.Error(), "user/signup", &form)
  219. default:
  220. ctx.Handle(500, "user.SignUp(RegisterUser)", err)
  221. }
  222. return
  223. }
  224. log.Trace("%s User created: %s", ctx.Req.RequestURI, form.UserName)
  225. // Bind social account.
  226. if isOauth {
  227. if err = models.BindUserOauth2(u.Id, sid); err != nil {
  228. ctx.Handle(500, "user.SignUp(BindUserOauth2)", err)
  229. return
  230. }
  231. ctx.Session.Delete("socialId")
  232. log.Trace("%s OAuth binded: %s -> %d", ctx.Req.RequestURI, form.UserName, sid)
  233. }
  234. // Send confirmation e-mail, no need for social account.
  235. if !isOauth && base.Service.RegisterEmailConfirm && u.Id > 1 {
  236. mailer.SendRegisterMail(ctx.Render, u)
  237. ctx.Data["IsSendRegisterMail"] = true
  238. ctx.Data["Email"] = u.Email
  239. ctx.Data["Hours"] = base.Service.ActiveCodeLives / 60
  240. ctx.HTML(200, "user/activate")
  241. if err = ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
  242. log.Error("Set cache(MailResendLimit) fail: %v", err)
  243. }
  244. return
  245. }
  246. ctx.Redirect("/user/login")
  247. }
  248. func Delete(ctx *middleware.Context) {
  249. ctx.Data["Title"] = "Delete Account"
  250. ctx.Data["PageIsUserSetting"] = true
  251. ctx.Data["IsUserPageSettingDelete"] = true
  252. ctx.HTML(200, "user/delete")
  253. }
  254. func DeletePost(ctx *middleware.Context) {
  255. ctx.Data["Title"] = "Delete Account"
  256. ctx.Data["PageIsUserSetting"] = true
  257. ctx.Data["IsUserPageSettingDelete"] = true
  258. tmpUser := models.User{
  259. Passwd: ctx.Query("password"),
  260. Salt: ctx.User.Salt,
  261. }
  262. tmpUser.EncodePasswd()
  263. if tmpUser.Passwd != ctx.User.Passwd {
  264. ctx.Flash.Error("Password is not correct. Make sure you are owner of this account.")
  265. } else {
  266. if err := models.DeleteUser(ctx.User); err != nil {
  267. switch err {
  268. case models.ErrUserOwnRepos:
  269. ctx.Flash.Error("Your account still have ownership of repository, you have to delete or transfer them first.")
  270. default:
  271. ctx.Handle(500, "user.Delete", err)
  272. return
  273. }
  274. } else {
  275. ctx.Redirect("/")
  276. return
  277. }
  278. }
  279. ctx.Redirect("/user/delete")
  280. }
  281. func Activate(ctx *middleware.Context) {
  282. code := ctx.Query("code")
  283. if len(code) == 0 {
  284. ctx.Data["IsActivatePage"] = true
  285. if ctx.User.IsActive {
  286. ctx.Handle(404, "user.Activate", nil)
  287. return
  288. }
  289. // Resend confirmation e-mail.
  290. if base.Service.RegisterEmailConfirm {
  291. if ctx.Cache.IsExist("MailResendLimit_" + ctx.User.LowerName) {
  292. ctx.Data["ResendLimited"] = true
  293. } else {
  294. ctx.Data["Hours"] = base.Service.ActiveCodeLives / 60
  295. mailer.SendActiveMail(ctx.Render, ctx.User)
  296. if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil {
  297. log.Error("Set cache(MailResendLimit) fail: %v", err)
  298. }
  299. }
  300. } else {
  301. ctx.Data["ServiceNotEnabled"] = true
  302. }
  303. ctx.HTML(200, "user/activate")
  304. return
  305. }
  306. // Verify code.
  307. if user := models.VerifyUserActiveCode(code); user != nil {
  308. user.IsActive = true
  309. user.Rands = models.GetUserSalt()
  310. if err := models.UpdateUser(user); err != nil {
  311. ctx.Handle(404, "user.Activate", err)
  312. return
  313. }
  314. log.Trace("%s User activated: %s", ctx.Req.RequestURI, user.Name)
  315. ctx.Session.Set("userId", user.Id)
  316. ctx.Session.Set("userName", user.Name)
  317. ctx.Redirect("/")
  318. return
  319. }
  320. ctx.Data["IsActivateFailed"] = true
  321. ctx.HTML(200, "user/activate")
  322. }
  323. func ForgotPasswd(ctx *middleware.Context) {
  324. ctx.Data["Title"] = "Forgot Password"
  325. if base.MailService == nil {
  326. ctx.Data["IsResetDisable"] = true
  327. ctx.HTML(200, "user/forgot_passwd")
  328. return
  329. }
  330. ctx.Data["IsResetRequest"] = true
  331. ctx.HTML(200, "user/forgot_passwd")
  332. }
  333. func ForgotPasswdPost(ctx *middleware.Context) {
  334. ctx.Data["Title"] = "Forgot Password"
  335. if base.MailService == nil {
  336. ctx.Handle(403, "user.ForgotPasswdPost", nil)
  337. return
  338. }
  339. ctx.Data["IsResetRequest"] = true
  340. email := ctx.Query("email")
  341. u, err := models.GetUserByEmail(email)
  342. if err != nil {
  343. if err == models.ErrUserNotExist {
  344. ctx.RenderWithErr("This e-mail address does not associate to any account.", "user/forgot_passwd", nil)
  345. } else {
  346. ctx.Handle(500, "user.ResetPasswd(check existence)", err)
  347. }
  348. return
  349. }
  350. if ctx.Cache.IsExist("MailResendLimit_" + u.LowerName) {
  351. ctx.Data["ResendLimited"] = true
  352. ctx.HTML(200, "user/forgot_passwd")
  353. return
  354. }
  355. mailer.SendResetPasswdMail(ctx.Render, u)
  356. if err = ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
  357. log.Error("Set cache(MailResendLimit) fail: %v", err)
  358. }
  359. ctx.Data["Email"] = email
  360. ctx.Data["Hours"] = base.Service.ActiveCodeLives / 60
  361. ctx.Data["IsResetSent"] = true
  362. ctx.HTML(200, "user/forgot_passwd")
  363. }
  364. func ResetPasswd(ctx *middleware.Context) {
  365. ctx.Data["Title"] = "Reset Password"
  366. code := ctx.Query("code")
  367. if len(code) == 0 {
  368. ctx.Error(404)
  369. return
  370. }
  371. ctx.Data["Code"] = code
  372. ctx.Data["IsResetForm"] = true
  373. ctx.HTML(200, "user/reset_passwd")
  374. }
  375. func ResetPasswdPost(ctx *middleware.Context) {
  376. ctx.Data["Title"] = "Reset Password"
  377. code := ctx.Query("code")
  378. if len(code) == 0 {
  379. ctx.Error(404)
  380. return
  381. }
  382. ctx.Data["Code"] = code
  383. if u := models.VerifyUserActiveCode(code); u != nil {
  384. // Validate password length.
  385. passwd := ctx.Query("passwd")
  386. if len(passwd) < 6 || len(passwd) > 30 {
  387. ctx.Data["IsResetForm"] = true
  388. ctx.RenderWithErr("Password length should be in 6 and 30.", "user/reset_passwd", nil)
  389. return
  390. }
  391. u.Passwd = passwd
  392. u.Rands = models.GetUserSalt()
  393. u.Salt = models.GetUserSalt()
  394. u.EncodePasswd()
  395. if err := models.UpdateUser(u); err != nil {
  396. ctx.Handle(500, "user.ResetPasswd(UpdateUser)", err)
  397. return
  398. }
  399. log.Trace("%s User password reset: %s", ctx.Req.RequestURI, u.Name)
  400. ctx.Redirect("/user/login")
  401. return
  402. }
  403. ctx.Data["IsResetFailed"] = true
  404. ctx.HTML(200, "user/reset_passwd")
  405. }