diff options
author | Unknwon <joe2010xtmf@163.com> | 2014-08-09 21:02:00 -0700 |
---|---|---|
committer | Unknwon <joe2010xtmf@163.com> | 2014-08-09 21:02:00 -0700 |
commit | 7af7584d25f48309a70083c30805ebb8454611f0 (patch) | |
tree | f1f20d5fafe8f023392ce35931221ba2cee2fe35 /routers | |
parent | 5605af973b440add79b1c32c911bee03fa821a4e (diff) | |
download | gitea-7af7584d25f48309a70083c30805ebb8454611f0.tar.gz gitea-7af7584d25f48309a70083c30805ebb8454611f0.zip |
Finish new reset password, etc.
Diffstat (limited to 'routers')
-rw-r--r-- | routers/home.go | 7 | ||||
-rw-r--r-- | routers/user/auth.go | 241 |
2 files changed, 129 insertions, 119 deletions
diff --git a/routers/home.go b/routers/home.go index 4f7abea7a3..5ea3e2a027 100644 --- a/routers/home.go +++ b/routers/home.go @@ -17,7 +17,12 @@ const ( func Home(ctx *middleware.Context) { if ctx.IsSigned { - user.Dashboard(ctx) + if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm { + ctx.Data["Title"] = ctx.Tr("auth.active_your_account") + ctx.HTML(200, user.ACTIVATE) + } else { + user.Dashboard(ctx) + } return } diff --git a/routers/user/auth.go b/routers/user/auth.go index 191da0a219..404b22f24d 100644 --- a/routers/user/auth.go +++ b/routers/user/auth.go @@ -106,10 +106,9 @@ func SignInPost(ctx *middleware.Context, form auth.SignInForm) { if err != nil { if err == models.ErrUserNotExist { ctx.RenderWithErr(ctx.Tr("form.username_password_incorrect"), SIGNIN, &form) - return + } else { + ctx.Handle(500, "UserSignIn", err) } - - ctx.Handle(500, "UserSignIn", err) return } @@ -294,55 +293,59 @@ func SignUpPost(ctx *middleware.Context, cpt *captcha.Captcha, form auth.Registe } func Activate(ctx *middleware.Context) { - // code := ctx.Query("code") - // if len(code) == 0 { - // ctx.Data["IsActivatePage"] = true - // if ctx.User.IsActive { - // ctx.Handle(404, "user.Activate", nil) - // return - // } - // // Resend confirmation e-mail. - // if setting.Service.RegisterEmailConfirm { - // if ctx.Cache.IsExist("MailResendLimit_" + ctx.User.LowerName) { - // ctx.Data["ResendLimited"] = true - // } else { - // ctx.Data["Hours"] = setting.Service.ActiveCodeLives / 60 - // mailer.SendActiveMail(ctx.Render, ctx.User) - - // if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil { - // log.Error("Set cache(MailResendLimit) fail: %v", err) - // } - // } - // } else { - // ctx.Data["ServiceNotEnabled"] = true - // } - // ctx.HTML(200, ACTIVATE) - // return - // } - - // // Verify code. - // if user := models.VerifyUserActiveCode(code); user != nil { - // user.IsActive = true - // user.Rands = models.GetUserSalt() - // if err := models.UpdateUser(user); err != nil { - // ctx.Handle(404, "user.Activate", err) - // return - // } - - // log.Trace("%s User activated: %s", ctx.Req.RequestURI, user.Name) - - // ctx.Session.Set("userId", user.Id) - // ctx.Session.Set("userName", user.Name) - // ctx.Redirect("/") - // return - // } - - // ctx.Data["IsActivateFailed"] = true - // ctx.HTML(200, ACTIVATE) + code := ctx.Query("code") + if len(code) == 0 { + ctx.Data["IsActivatePage"] = true + if ctx.User.IsActive { + ctx.Error(404) + return + } + // Resend confirmation e-mail. + if setting.Service.RegisterEmailConfirm { + if ctx.Cache.IsExist("MailResendLimit_" + ctx.User.LowerName) { + ctx.Data["ResendLimited"] = true + } else { + ctx.Data["Hours"] = setting.Service.ActiveCodeLives / 60 + mailer.SendActiveMail(ctx.Render, ctx.User) + + if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil { + log.Error(4, "Set cache(MailResendLimit) fail: %v", err) + } + } + } else { + ctx.Data["ServiceNotEnabled"] = true + } + ctx.HTML(200, ACTIVATE) + return + } + + // Verify code. + if user := models.VerifyUserActiveCode(code); user != nil { + user.IsActive = true + user.Rands = models.GetUserSalt() + if err := models.UpdateUser(user); err != nil { + if err == models.ErrUserNotExist { + ctx.Error(404) + } else { + ctx.Handle(500, "UpdateUser", err) + } + return + } + + log.Trace("User activated: %s", user.Name) + + ctx.Session.Set("uid", user.Id) + ctx.Session.Set("uname", user.Name) + ctx.Redirect("/") + return + } + + ctx.Data["IsActivateFailed"] = true + ctx.HTML(200, ACTIVATE) } func ForgotPasswd(ctx *middleware.Context) { - ctx.Data["Title"] = "Forgot Password" + ctx.Data["Title"] = ctx.Tr("auth.forgot_password") if setting.MailService == nil { ctx.Data["IsResetDisable"] = true @@ -355,44 +358,45 @@ func ForgotPasswd(ctx *middleware.Context) { } func ForgotPasswdPost(ctx *middleware.Context) { - // ctx.Data["Title"] = "Forgot Password" - - // if setting.MailService == nil { - // ctx.Handle(403, "user.ForgotPasswdPost", nil) - // return - // } - // ctx.Data["IsResetRequest"] = true - - // email := ctx.Query("email") - // u, err := models.GetUserByEmail(email) - // if err != nil { - // if err == models.ErrUserNotExist { - // ctx.RenderWithErr("This e-mail address does not associate to any account.", "user/forgot_passwd", nil) - // } else { - // ctx.Handle(500, "user.ResetPasswd(check existence)", err) - // } - // return - // } - - // if ctx.Cache.IsExist("MailResendLimit_" + u.LowerName) { - // ctx.Data["ResendLimited"] = true - // ctx.HTML(200, FORGOT_PASSWORD) - // return - // } - - // mailer.SendResetPasswdMail(ctx.Render, u) - // if err = ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil { - // log.Error("Set cache(MailResendLimit) fail: %v", err) - // } - - // ctx.Data["Email"] = email - // ctx.Data["Hours"] = setting.Service.ActiveCodeLives / 60 - // ctx.Data["IsResetSent"] = true - // ctx.HTML(200, FORGOT_PASSWORD) + ctx.Data["Title"] = ctx.Tr("auth.forgot_password") + + if setting.MailService == nil { + ctx.Handle(403, "user.ForgotPasswdPost", nil) + return + } + ctx.Data["IsResetRequest"] = true + + email := ctx.Query("email") + u, err := models.GetUserByEmail(email) + if err != nil { + if err == models.ErrUserNotExist { + ctx.Data["Err_Email"] = true + ctx.RenderWithErr(ctx.Tr("auth.email_not_associate"), FORGOT_PASSWORD, nil) + } else { + ctx.Handle(500, "user.ResetPasswd(check existence)", err) + } + return + } + + if ctx.Cache.IsExist("MailResendLimit_" + u.LowerName) { + ctx.Data["ResendLimited"] = true + ctx.HTML(200, FORGOT_PASSWORD) + return + } + + mailer.SendResetPasswdMail(ctx.Render, u) + if err = ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil { + log.Error(4, "Set cache(MailResendLimit) fail: %v", err) + } + + ctx.Data["Email"] = email + ctx.Data["Hours"] = setting.Service.ActiveCodeLives / 60 + ctx.Data["IsResetSent"] = true + ctx.HTML(200, FORGOT_PASSWORD) } func ResetPasswd(ctx *middleware.Context) { - ctx.Data["Title"] = "Reset Password" + ctx.Data["Title"] = ctx.Tr("auth.reset_password") code := ctx.Query("code") if len(code) == 0 { @@ -405,38 +409,39 @@ func ResetPasswd(ctx *middleware.Context) { } func ResetPasswdPost(ctx *middleware.Context) { - // ctx.Data["Title"] = "Reset Password" - - // code := ctx.Query("code") - // if len(code) == 0 { - // ctx.Error(404) - // return - // } - // ctx.Data["Code"] = code - - // if u := models.VerifyUserActiveCode(code); u != nil { - // // Validate password length. - // passwd := ctx.Query("passwd") - // if len(passwd) < 6 || len(passwd) > 30 { - // ctx.Data["IsResetForm"] = true - // ctx.RenderWithErr("Password length should be in 6 and 30.", "user/reset_passwd", nil) - // return - // } - - // u.Passwd = passwd - // u.Rands = models.GetUserSalt() - // u.Salt = models.GetUserSalt() - // u.EncodePasswd() - // if err := models.UpdateUser(u); err != nil { - // ctx.Handle(500, "user.ResetPasswd(UpdateUser)", err) - // return - // } - - // log.Trace("%s User password reset: %s", ctx.Req.RequestURI, u.Name) - // ctx.Redirect("/user/login") - // return - // } - - // ctx.Data["IsResetFailed"] = true - // ctx.HTML(200, RESET_PASSWORD) + ctx.Data["Title"] = ctx.Tr("auth.reset_password") + + code := ctx.Query("code") + if len(code) == 0 { + ctx.Error(404) + return + } + ctx.Data["Code"] = code + + if u := models.VerifyUserActiveCode(code); u != nil { + // Validate password length. + passwd := ctx.Query("password") + if len(passwd) < 6 { + ctx.Data["IsResetForm"] = true + ctx.Data["Err_Password"] = true + ctx.RenderWithErr(ctx.Tr("auth.password_too_short"), RESET_PASSWORD, nil) + return + } + + u.Passwd = passwd + u.Rands = models.GetUserSalt() + u.Salt = models.GetUserSalt() + u.EncodePasswd() + if err := models.UpdateUser(u); err != nil { + ctx.Handle(500, "UpdateUser", err) + return + } + + log.Trace("User password reset: %s", u.Name) + ctx.Redirect("/user/login") + return + } + + ctx.Data["IsResetFailed"] = true + ctx.HTML(200, RESET_PASSWORD) } |