mirror of https://github.com/gogits/gogs.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
535 lines
13 KiB
535 lines
13 KiB
11 years ago
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
||
|
// Use of this source code is governed by a MIT-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
|
package user
|
||
|
|
||
|
import (
|
||
9 years ago
|
"fmt"
|
||
11 years ago
|
"net/url"
|
||
|
|
||
9 years ago
|
"github.com/go-macaron/captcha"
|
||
8 years ago
|
log "gopkg.in/clog.v1"
|
||
11 years ago
|
|
||
11 years ago
|
"github.com/gogits/gogs/models"
|
||
8 years ago
|
"github.com/gogits/gogs/models/errors"
|
||
8 years ago
|
"github.com/gogits/gogs/pkg/context"
|
||
|
"github.com/gogits/gogs/pkg/form"
|
||
|
"github.com/gogits/gogs/pkg/mailer"
|
||
|
"github.com/gogits/gogs/pkg/setting"
|
||
11 years ago
|
)
|
||
|
|
||
|
const (
|
||
8 years ago
|
LOGIN = "user/auth/login"
|
||
|
TWO_FACTOR = "user/auth/two_factor"
|
||
|
TWO_FACTOR_RECOVERY_CODE = "user/auth/two_factor_recovery_code"
|
||
|
SIGNUP = "user/auth/signup"
|
||
|
ACTIVATE = "user/auth/activate"
|
||
|
FORGOT_PASSWORD = "user/auth/forgot_passwd"
|
||
|
RESET_PASSWORD = "user/auth/reset_passwd"
|
||
11 years ago
|
)
|
||
|
|
||
8 years ago
|
// AutoLogin reads cookie and try to auto-login.
|
||
|
func AutoLogin(c *context.Context) (bool, error) {
|
||
9 years ago
|
if !models.HasEngine {
|
||
|
return false, nil
|
||
|
}
|
||
|
|
||
8 years ago
|
uname := c.GetCookie(setting.CookieUserName)
|
||
9 years ago
|
if len(uname) == 0 {
|
||
|
return false, nil
|
||
|
}
|
||
|
|
||
|
isSucceed := false
|
||
|
defer func() {
|
||
|
if !isSucceed {
|
||
|
log.Trace("auto-login cookie cleared: %s", uname)
|
||
8 years ago
|
c.SetCookie(setting.CookieUserName, "", -1, setting.AppSubURL)
|
||
|
c.SetCookie(setting.CookieRememberName, "", -1, setting.AppSubURL)
|
||
|
c.SetCookie(setting.LoginStatusCookieName, "", -1, setting.AppSubURL)
|
||
9 years ago
|
}
|
||
|
}()
|
||
|
|
||
|
u, err := models.GetUserByName(uname)
|
||
|
if err != nil {
|
||
8 years ago
|
if !errors.IsUserNotExist(err) {
|
||
9 years ago
|
return false, fmt.Errorf("GetUserByName: %v", err)
|
||
|
}
|
||
|
return false, nil
|
||
|
}
|
||
|
|
||
8 years ago
|
if val, ok := c.GetSuperSecureCookie(u.Rands+u.Passwd, setting.CookieRememberName); !ok || val != u.Name {
|
||
9 years ago
|
return false, nil
|
||
|
}
|
||
|
|
||
|
isSucceed = true
|
||
8 years ago
|
c.Session.Set("uid", u.ID)
|
||
|
c.Session.Set("uname", u.Name)
|
||
8 years ago
|
c.SetCookie(setting.CSRFCookieName, "", -1, setting.AppSubURL)
|
||
8 years ago
|
if setting.EnableLoginStatusCookie {
|
||
8 years ago
|
c.SetCookie(setting.LoginStatusCookieName, "true", 0, setting.AppSubURL)
|
||
8 years ago
|
}
|
||
9 years ago
|
return true, nil
|
||
|
}
|
||
|
|
||
8 years ago
|
// isValidRedirect returns false if the URL does not redirect to same site.
|
||
|
// False: //url, http://url
|
||
|
// True: /url
|
||
|
func isValidRedirect(url string) bool {
|
||
|
return len(url) >= 2 && url[0] == '/' && url[1] != '/'
|
||
|
}
|
||
|
|
||
8 years ago
|
func Login(c *context.Context) {
|
||
|
c.Data["Title"] = c.Tr("sign_in")
|
||
11 years ago
|
|
||
|
// Check auto-login.
|
||
8 years ago
|
isSucceed, err := AutoLogin(c)
|
||
11 years ago
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "AutoLogin", err)
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
redirectTo := c.Query("redirect_to")
|
||
8 years ago
|
if len(redirectTo) > 0 {
|
||
8 years ago
|
c.SetCookie("redirect_to", redirectTo, 0, setting.AppSubURL)
|
||
8 years ago
|
} else {
|
||
8 years ago
|
redirectTo, _ = url.QueryUnescape(c.GetCookie("redirect_to"))
|
||
8 years ago
|
}
|
||
8 years ago
|
c.SetCookie("redirect_to", "", -1, setting.AppSubURL)
|
||
8 years ago
|
|
||
10 years ago
|
if isSucceed {
|
||
8 years ago
|
if isValidRedirect(redirectTo) {
|
||
8 years ago
|
c.Redirect(redirectTo)
|
||
9 years ago
|
} else {
|
||
8 years ago
|
c.Redirect(setting.AppSubURL + "/")
|
||
10 years ago
|
}
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
c.HTML(200, LOGIN)
|
||
|
}
|
||
|
|
||
|
func afterLogin(c *context.Context, u *models.User, remember bool) {
|
||
|
if remember {
|
||
|
days := 86400 * setting.LoginRememberDays
|
||
8 years ago
|
c.SetCookie(setting.CookieUserName, u.Name, days, setting.AppSubURL, "", setting.CookieSecure, true)
|
||
|
c.SetSuperSecureCookie(u.Rands+u.Passwd, setting.CookieRememberName, u.Name, days, setting.AppSubURL, "", setting.CookieSecure, true)
|
||
8 years ago
|
}
|
||
|
|
||
|
c.Session.Set("uid", u.ID)
|
||
|
c.Session.Set("uname", u.Name)
|
||
|
c.Session.Delete("twoFactorRemember")
|
||
|
c.Session.Delete("twoFactorUserID")
|
||
|
|
||
|
// Clear whatever CSRF has right now, force to generate a new one
|
||
8 years ago
|
c.SetCookie(setting.CSRFCookieName, "", -1, setting.AppSubURL)
|
||
8 years ago
|
if setting.EnableLoginStatusCookie {
|
||
8 years ago
|
c.SetCookie(setting.LoginStatusCookieName, "true", 0, setting.AppSubURL)
|
||
8 years ago
|
}
|
||
|
|
||
|
redirectTo, _ := url.QueryUnescape(c.GetCookie("redirect_to"))
|
||
8 years ago
|
c.SetCookie("redirect_to", "", -1, setting.AppSubURL)
|
||
8 years ago
|
if isValidRedirect(redirectTo) {
|
||
|
c.Redirect(redirectTo)
|
||
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
c.Redirect(setting.AppSubURL + "/")
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
func LoginPost(c *context.Context, f form.SignIn) {
|
||
|
c.Data["Title"] = c.Tr("sign_in")
|
||
11 years ago
|
|
||
8 years ago
|
if c.HasError() {
|
||
|
c.Success(LOGIN)
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
u, err := models.UserSignIn(f.UserName, f.Password)
|
||
11 years ago
|
if err != nil {
|
||
8 years ago
|
if errors.IsUserNotExist(err) {
|
||
8 years ago
|
c.RenderWithErr(c.Tr("form.username_password_incorrect"), LOGIN, &f)
|
||
11 years ago
|
} else {
|
||
8 years ago
|
c.ServerError("UserSignIn", err)
|
||
11 years ago
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
if !u.IsEnabledTwoFactor() {
|
||
|
afterLogin(c, u, f.Remember)
|
||
|
return
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
c.Session.Set("twoFactorRemember", f.Remember)
|
||
|
c.Session.Set("twoFactorUserID", u.ID)
|
||
8 years ago
|
c.Redirect(setting.AppSubURL + "/user/login/two_factor")
|
||
8 years ago
|
}
|
||
9 years ago
|
|
||
8 years ago
|
func LoginTwoFactor(c *context.Context) {
|
||
|
_, ok := c.Session.Get("twoFactorUserID").(int64)
|
||
|
if !ok {
|
||
|
c.NotFound()
|
||
|
return
|
||
8 years ago
|
}
|
||
9 years ago
|
|
||
8 years ago
|
c.Success(TWO_FACTOR)
|
||
|
}
|
||
|
|
||
|
func LoginTwoFactorPost(c *context.Context) {
|
||
|
userID, ok := c.Session.Get("twoFactorUserID").(int64)
|
||
|
if !ok {
|
||
|
c.NotFound()
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
t, err := models.GetTwoFactorByUserID(userID)
|
||
|
if err != nil {
|
||
|
c.ServerError("GetTwoFactorByUserID", err)
|
||
|
return
|
||
|
}
|
||
|
valid, err := t.ValidateTOTP(c.Query("passcode"))
|
||
|
if err != nil {
|
||
|
c.ServerError("ValidateTOTP", err)
|
||
|
return
|
||
|
} else if !valid {
|
||
|
c.Flash.Error(c.Tr("settings.two_factor_invalid_passcode"))
|
||
8 years ago
|
c.Redirect(setting.AppSubURL + "/user/login/two_factor")
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
|
u, err := models.GetUserByID(userID)
|
||
|
if err != nil {
|
||
|
c.ServerError("GetUserByID", err)
|
||
|
return
|
||
|
}
|
||
|
afterLogin(c, u, c.Session.Get("twoFactorRemember").(bool))
|
||
|
}
|
||
|
|
||
|
func LoginTwoFactorRecoveryCode(c *context.Context) {
|
||
|
_, ok := c.Session.Get("twoFactorUserID").(int64)
|
||
|
if !ok {
|
||
|
c.NotFound()
|
||
|
return
|
||
|
}
|
||
|
|
||
|
c.Success(TWO_FACTOR_RECOVERY_CODE)
|
||
|
}
|
||
|
|
||
|
func LoginTwoFactorRecoveryCodePost(c *context.Context) {
|
||
|
userID, ok := c.Session.Get("twoFactorUserID").(int64)
|
||
|
if !ok {
|
||
|
c.NotFound()
|
||
|
return
|
||
|
}
|
||
|
|
||
|
if err := models.UseRecoveryCode(userID, c.Query("recovery_code")); err != nil {
|
||
|
if errors.IsTwoFactorRecoveryCodeNotFound(err) {
|
||
|
c.Flash.Error(c.Tr("auth.login_two_factor_invalid_recovery_code"))
|
||
8 years ago
|
c.Redirect(setting.AppSubURL + "/user/login/two_factor_recovery_code")
|
||
8 years ago
|
} else {
|
||
|
c.ServerError("UseRecoveryCode", err)
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
|
u, err := models.GetUserByID(userID)
|
||
|
if err != nil {
|
||
|
c.ServerError("GetUserByID", err)
|
||
|
return
|
||
|
}
|
||
|
afterLogin(c, u, c.Session.Get("twoFactorRemember").(bool))
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
func SignOut(c *context.Context) {
|
||
|
c.Session.Delete("uid")
|
||
|
c.Session.Delete("uname")
|
||
|
c.SetCookie(setting.CookieUserName, "", -1, setting.AppSubURL)
|
||
|
c.SetCookie(setting.CookieRememberName, "", -1, setting.AppSubURL)
|
||
|
c.SetCookie(setting.CSRFCookieName, "", -1, setting.AppSubURL)
|
||
|
c.Redirect(setting.AppSubURL + "/")
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
func SignUp(c *context.Context) {
|
||
|
c.Data["Title"] = c.Tr("sign_up")
|
||
11 years ago
|
|
||
8 years ago
|
c.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
|
||
9 years ago
|
|
||
11 years ago
|
if setting.Service.DisableRegistration {
|
||
8 years ago
|
c.Data["DisableRegistration"] = true
|
||
|
c.HTML(200, SIGNUP)
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
c.HTML(200, SIGNUP)
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
func SignUpPost(c *context.Context, cpt *captcha.Captcha, f form.Register) {
|
||
|
c.Data["Title"] = c.Tr("sign_up")
|
||
11 years ago
|
|
||
8 years ago
|
c.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
|
||
9 years ago
|
|
||
11 years ago
|
if setting.Service.DisableRegistration {
|
||
8 years ago
|
c.Error(403)
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
if c.HasError() {
|
||
|
c.HTML(200, SIGNUP)
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
if setting.Service.EnableCaptcha && !cpt.VerifyReq(c.Req) {
|
||
|
c.Data["Err_Captcha"] = true
|
||
|
c.RenderWithErr(c.Tr("form.captcha_incorrect"), SIGNUP, &f)
|
||
11 years ago
|
return
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
if f.Password != f.Retype {
|
||
8 years ago
|
c.Data["Err_Password"] = true
|
||
|
c.RenderWithErr(c.Tr("form.password_not_match"), SIGNUP, &f)
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
|
u := &models.User{
|
||
8 years ago
|
Name: f.UserName,
|
||
|
Email: f.Email,
|
||
|
Passwd: f.Password,
|
||
9 years ago
|
IsActive: !setting.Service.RegisterEmailConfirm,
|
||
11 years ago
|
}
|
||
|
if err := models.CreateUser(u); err != nil {
|
||
10 years ago
|
switch {
|
||
|
case models.IsErrUserAlreadyExist(err):
|
||
8 years ago
|
c.Data["Err_UserName"] = true
|
||
|
c.RenderWithErr(c.Tr("form.username_been_taken"), SIGNUP, &f)
|
||
10 years ago
|
case models.IsErrEmailAlreadyUsed(err):
|
||
8 years ago
|
c.Data["Err_Email"] = true
|
||
|
c.RenderWithErr(c.Tr("form.email_been_used"), SIGNUP, &f)
|
||
10 years ago
|
case models.IsErrNameReserved(err):
|
||
8 years ago
|
c.Data["Err_UserName"] = true
|
||
|
c.RenderWithErr(c.Tr("user.form.name_reserved", err.(models.ErrNameReserved).Name), SIGNUP, &f)
|
||
10 years ago
|
case models.IsErrNamePatternNotAllowed(err):
|
||
8 years ago
|
c.Data["Err_UserName"] = true
|
||
|
c.RenderWithErr(c.Tr("user.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), SIGNUP, &f)
|
||
11 years ago
|
default:
|
||
8 years ago
|
c.Handle(500, "CreateUser", err)
|
||
11 years ago
|
}
|
||
|
return
|
||
|
}
|
||
|
log.Trace("Account created: %s", u.Name)
|
||
|
|
||
9 years ago
|
// Auto-set admin for the only user.
|
||
|
if models.CountUsers() == 1 {
|
||
|
u.IsAdmin = true
|
||
|
u.IsActive = true
|
||
|
if err := models.UpdateUser(u); err != nil {
|
||
8 years ago
|
c.Handle(500, "UpdateUser", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
// Send confirmation email, no need for social account.
|
||
9 years ago
|
if setting.Service.RegisterEmailConfirm && u.ID > 1 {
|
||
8 years ago
|
mailer.SendActivateAccountMail(c.Context, models.NewMailerUser(u))
|
||
|
c.Data["IsSendRegisterMail"] = true
|
||
|
c.Data["Email"] = u.Email
|
||
|
c.Data["Hours"] = setting.Service.ActiveCodeLives / 60
|
||
|
c.HTML(200, ACTIVATE)
|
||
11 years ago
|
|
||
8 years ago
|
if err := c.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
|
||
11 years ago
|
log.Error(4, "Set cache(MailResendLimit) fail: %v", err)
|
||
|
}
|
||
|
return
|
||
|
}
|
||
11 years ago
|
|
||
8 years ago
|
c.Redirect(setting.AppSubURL + "/user/login")
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
func Activate(c *context.Context) {
|
||
|
code := c.Query("code")
|
||
11 years ago
|
if len(code) == 0 {
|
||
8 years ago
|
c.Data["IsActivatePage"] = true
|
||
|
if c.User.IsActive {
|
||
|
c.Error(404)
|
||
11 years ago
|
return
|
||
|
}
|
||
9 years ago
|
// Resend confirmation email.
|
||
11 years ago
|
if setting.Service.RegisterEmailConfirm {
|
||
8 years ago
|
if c.Cache.IsExist("MailResendLimit_" + c.User.LowerName) {
|
||
|
c.Data["ResendLimited"] = true
|
||
11 years ago
|
} else {
|
||
8 years ago
|
c.Data["Hours"] = setting.Service.ActiveCodeLives / 60
|
||
|
mailer.SendActivateAccountMail(c.Context, models.NewMailerUser(c.User))
|
||
11 years ago
|
|
||
8 years ago
|
keyName := "MailResendLimit_" + c.User.LowerName
|
||
|
if err := c.Cache.Put(keyName, c.User.LowerName, 180); err != nil {
|
||
8 years ago
|
log.Error(2, "Set cache '%s' fail: %v", keyName, err)
|
||
11 years ago
|
}
|
||
|
}
|
||
|
} else {
|
||
8 years ago
|
c.Data["ServiceNotEnabled"] = true
|
||
11 years ago
|
}
|
||
8 years ago
|
c.HTML(200, ACTIVATE)
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
|
// Verify code.
|
||
|
if user := models.VerifyUserActiveCode(code); user != nil {
|
||
|
user.IsActive = true
|
||
8 years ago
|
var err error
|
||
|
if user.Rands, err = models.GetUserSalt(); err != nil {
|
||
8 years ago
|
c.Handle(500, "UpdateUser", err)
|
||
8 years ago
|
return
|
||
|
}
|
||
11 years ago
|
if err := models.UpdateUser(user); err != nil {
|
||
8 years ago
|
c.Handle(500, "UpdateUser", err)
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
|
log.Trace("User activated: %s", user.Name)
|
||
|
|
||
8 years ago
|
c.Session.Set("uid", user.ID)
|
||
|
c.Session.Set("uname", user.Name)
|
||
|
c.Redirect(setting.AppSubURL + "/")
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
c.Data["IsActivateFailed"] = true
|
||
|
c.HTML(200, ACTIVATE)
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
func ActivateEmail(c *context.Context) {
|
||
|
code := c.Query("code")
|
||
|
email_string := c.Query("email")
|
||
10 years ago
|
|
||
|
// Verify code.
|
||
|
if email := models.VerifyActiveEmailCode(code, email_string); email != nil {
|
||
10 years ago
|
if err := email.Activate(); err != nil {
|
||
8 years ago
|
c.Handle(500, "ActivateEmail", err)
|
||
10 years ago
|
}
|
||
|
|
||
|
log.Trace("Email activated: %s", email.Email)
|
||
8 years ago
|
c.Flash.Success(c.Tr("settings.add_email_success"))
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
c.Redirect(setting.AppSubURL + "/user/settings/email")
|
||
10 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
func ForgotPasswd(c *context.Context) {
|
||
|
c.Data["Title"] = c.Tr("auth.forgot_password")
|
||
11 years ago
|
|
||
|
if setting.MailService == nil {
|
||
8 years ago
|
c.Data["IsResetDisable"] = true
|
||
|
c.HTML(200, FORGOT_PASSWORD)
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
c.Data["IsResetRequest"] = true
|
||
|
c.HTML(200, FORGOT_PASSWORD)
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
func ForgotPasswdPost(c *context.Context) {
|
||
|
c.Data["Title"] = c.Tr("auth.forgot_password")
|
||
11 years ago
|
|
||
|
if setting.MailService == nil {
|
||
8 years ago
|
c.Handle(403, "ForgotPasswdPost", nil)
|
||
11 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.Data["IsResetRequest"] = true
|
||
11 years ago
|
|
||
8 years ago
|
email := c.Query("email")
|
||
|
c.Data["Email"] = email
|
||
9 years ago
|
|
||
11 years ago
|
u, err := models.GetUserByEmail(email)
|
||
|
if err != nil {
|
||
8 years ago
|
if errors.IsUserNotExist(err) {
|
||
8 years ago
|
c.Data["Hours"] = setting.Service.ActiveCodeLives / 60
|
||
|
c.Data["IsResetSent"] = true
|
||
|
c.HTML(200, FORGOT_PASSWORD)
|
||
8 years ago
|
return
|
||
11 years ago
|
} else {
|
||
8 years ago
|
c.Handle(500, "user.ResetPasswd(check existence)", err)
|
||
11 years ago
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
9 years ago
|
if !u.IsLocal() {
|
||
8 years ago
|
c.Data["Err_Email"] = true
|
||
|
c.RenderWithErr(c.Tr("auth.non_local_account"), FORGOT_PASSWORD, nil)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
if c.Cache.IsExist("MailResendLimit_" + u.LowerName) {
|
||
|
c.Data["ResendLimited"] = true
|
||
|
c.HTML(200, FORGOT_PASSWORD)
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
mailer.SendResetPasswordMail(c.Context, models.NewMailerUser(u))
|
||
|
if err = c.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
|
||
11 years ago
|
log.Error(4, "Set cache(MailResendLimit) fail: %v", err)
|
||
|
}
|
||
|
|
||
8 years ago
|
c.Data["Hours"] = setting.Service.ActiveCodeLives / 60
|
||
|
c.Data["IsResetSent"] = true
|
||
|
c.HTML(200, FORGOT_PASSWORD)
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
func ResetPasswd(c *context.Context) {
|
||
|
c.Data["Title"] = c.Tr("auth.reset_password")
|
||
11 years ago
|
|
||
8 years ago
|
code := c.Query("code")
|
||
11 years ago
|
if len(code) == 0 {
|
||
8 years ago
|
c.Error(404)
|
||
11 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.Data["Code"] = code
|
||
|
c.Data["IsResetForm"] = true
|
||
|
c.HTML(200, RESET_PASSWORD)
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
func ResetPasswdPost(c *context.Context) {
|
||
|
c.Data["Title"] = c.Tr("auth.reset_password")
|
||
11 years ago
|
|
||
8 years ago
|
code := c.Query("code")
|
||
11 years ago
|
if len(code) == 0 {
|
||
8 years ago
|
c.Error(404)
|
||
11 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.Data["Code"] = code
|
||
11 years ago
|
|
||
|
if u := models.VerifyUserActiveCode(code); u != nil {
|
||
|
// Validate password length.
|
||
8 years ago
|
passwd := c.Query("password")
|
||
11 years ago
|
if len(passwd) < 6 {
|
||
8 years ago
|
c.Data["IsResetForm"] = true
|
||
|
c.Data["Err_Password"] = true
|
||
|
c.RenderWithErr(c.Tr("auth.password_too_short"), RESET_PASSWORD, nil)
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
|
u.Passwd = passwd
|
||
8 years ago
|
var err error
|
||
|
if u.Rands, err = models.GetUserSalt(); err != nil {
|
||
8 years ago
|
c.Handle(500, "UpdateUser", err)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
if u.Salt, err = models.GetUserSalt(); err != nil {
|
||
8 years ago
|
c.Handle(500, "UpdateUser", err)
|
||
8 years ago
|
return
|
||
|
}
|
||
11 years ago
|
u.EncodePasswd()
|
||
|
if err := models.UpdateUser(u); err != nil {
|
||
8 years ago
|
c.Handle(500, "UpdateUser", err)
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
|
log.Trace("User password reset: %s", u.Name)
|
||
8 years ago
|
c.Redirect(setting.AppSubURL + "/user/login")
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
c.Data["IsResetFailed"] = true
|
||
|
c.HTML(200, RESET_PASSWORD)
|
||
11 years ago
|
}
|