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