@ -74,57 +74,63 @@ func Profile(ctx *middleware.Context, params martini.Params) {
ctx . HTML ( 200 , "user/profile" )
ctx . HTML ( 200 , "user/profile" )
}
}
func SignIn ( ctx * middleware . Context , form auth . LogInForm ) {
func SignIn ( ctx * middleware . Context ) {
ctx . Data [ "Title" ] = "Log In"
ctx . Data [ "Title" ] = "Log In"
if ctx . Req . Method == "GET" {
if base . OauthService != nil {
if base . OauthService != nil {
ctx . Data [ "OauthEnabled" ] = true
ctx . Data [ "OauthEnabled" ] = true
ctx . Data [ "OauthGitHubEnabled" ] = base . OauthService . GitHub . Enabled
ctx . Data [ "OauthGitHubEnabled" ] = base . OauthService . GitHub . Enabled
}
}
// Check auto-login.
userName := ctx . GetCookie ( base . CookieUserName )
if len ( userName ) == 0 {
ctx . HTML ( 200 , "user/signin" )
return
}
isSucceed := false
// Check auto-login.
defer func ( ) {
userName := ctx . GetCookie ( base . CookieUserName )
if ! isSucceed {
if len ( userName ) == 0 {
log . Trace ( "%s auto-login cookie cleared: %s" , ctx . Req . RequestURI , userName )
ctx . HTML ( 200 , "user/signin" )
ctx . SetCookie ( base . CookieUserName , "" , - 1 )
return
ctx . SetCookie ( base . CookieRememberName , "" , - 1 )
}
}
} ( )
user , err := models . GetUserByName ( userName )
isSucceed := false
if err != nil {
defer func ( ) {
ctx . HTML ( 200 , "user/signin" )
if ! isSucceed {
return
log . Trace ( "%s auto-login cookie cleared: %s" , ctx . Req . RequestURI , userName )
ctx . SetCookie ( base . CookieUserName , "" , - 1 )
ctx . SetCookie ( base . CookieRememberName , "" , - 1 )
}
}
} ( )
secret := base . EncodeMd5 ( user . Rands + user . Passwd )
user , err := models . GetUserByName ( userName )
value , _ := ctx . GetSecureCookie ( secret , base . CookieRememberName )
if err != nil {
if value != user . Name {
ctx . HTML ( 200 , "user/signin" )
ctx . HTML ( 200 , "user/signin" )
return
return
}
}
isSucceed = true
secret := base . EncodeMd5 ( user . Rands + user . Passwd )
ctx . Session . Set ( "userId" , user . Id )
value , _ := ctx . GetSecureCookie ( secret , base . CookieRememberName )
ctx . Session . Set ( "userName" , user . Name )
if value != user . Name {
redirectTo , _ := url . QueryUnescape ( ctx . GetCookie ( "redirect_to" ) )
ctx . HTML ( 200 , "user/signin" )
if len ( redirectTo ) > 0 {
ctx . SetCookie ( "redirect_to" , "" , - 1 )
ctx . Redirect ( redirectTo )
} else {
ctx . Redirect ( "/" )
}
return
return
}
}
isSucceed = true
ctx . Session . Set ( "userId" , user . Id )
ctx . Session . Set ( "userName" , user . Name )
if redirectTo , _ := url . QueryUnescape ( ctx . GetCookie ( "redirect_to" ) ) ; len ( redirectTo ) > 0 {
ctx . SetCookie ( "redirect_to" , "" , - 1 )
ctx . Redirect ( redirectTo )
return
}
ctx . Redirect ( "/" )
}
func SignInPost ( ctx * middleware . Context , form auth . LogInForm ) {
ctx . Data [ "Title" ] = "Log In"
if base . OauthService != nil {
ctx . Data [ "OauthEnabled" ] = true
ctx . Data [ "OauthGitHubEnabled" ] = base . OauthService . GitHub . Enabled
}
if ctx . HasError ( ) {
if ctx . HasError ( ) {
ctx . HTML ( 200 , "user/signin" )
ctx . HTML ( 200 , "user/signin" )
return
return
@ -138,7 +144,7 @@ func SignIn(ctx *middleware.Context, form auth.LogInForm) {
return
return
}
}
ctx . Handle ( 2 00, "user.SignIn" , err )
ctx . Handle ( 5 00, "user.SignIn" , err )
return
return
}
}
@ -151,13 +157,13 @@ func SignIn(ctx *middleware.Context, form auth.LogInForm) {
ctx . Session . Set ( "userId" , user . Id )
ctx . Session . Set ( "userId" , user . Id )
ctx . Session . Set ( "userName" , user . Name )
ctx . Session . Set ( "userName" , user . Name )
redirectTo , _ := url . QueryUnescape ( ctx . GetCookie ( "redirect_to" ) )
if redirectTo , _ := url . QueryUnescape ( ctx . GetCookie ( "redirect_to" ) ) ; len ( redirectTo ) > 0 {
if len ( redirectTo ) > 0 {
ctx . SetCookie ( "redirect_to" , "" , - 1 )
ctx . SetCookie ( "redirect_to" , "" , - 1 )
ctx . Redirect ( redirectTo )
ctx . Redirect ( redirectTo )
} else {
return
ctx . Redirect ( "/" )
}
}
ctx . Redirect ( "/" )
}
}
func SignOut ( ctx * middleware . Context ) {
func SignOut ( ctx * middleware . Context ) {
@ -168,7 +174,7 @@ func SignOut(ctx *middleware.Context) {
ctx . Redirect ( "/" )
ctx . Redirect ( "/" )
}
}
func SignUp ( ctx * middleware . Context , form auth . RegisterForm ) {
func SignUp ( ctx * middleware . Context ) {
ctx . Data [ "Title" ] = "Sign Up"
ctx . Data [ "Title" ] = "Sign Up"
ctx . Data [ "PageIsSignUp" ] = true
ctx . Data [ "PageIsSignUp" ] = true
@ -178,8 +184,15 @@ func SignUp(ctx *middleware.Context, form auth.RegisterForm) {
return
return
}
}
if ctx . Req . Method == "GET" {
ctx . HTML ( 200 , "user/signup" )
ctx . HTML ( 200 , "user/signup" )
}
func SignUpPost ( ctx * middleware . Context , form auth . RegisterForm ) {
ctx . Data [ "Title" ] = "Sign Up"
ctx . Data [ "PageIsSignUp" ] = true
if base . Service . DisenableRegisteration {
ctx . Handle ( 403 , "user.SignUpPost" , nil )
return
return
}
}
@ -213,7 +226,7 @@ func SignUp(ctx *middleware.Context, form auth.RegisterForm) {
case models . ErrUserNameIllegal :
case models . ErrUserNameIllegal :
ctx . RenderWithErr ( models . ErrRepoNameIllegal . Error ( ) , "user/signup" , & form )
ctx . RenderWithErr ( models . ErrRepoNameIllegal . Error ( ) , "user/signup" , & form )
default :
default :
ctx . Handle ( 2 00, "user.SignUp" , err )
ctx . Handle ( 5 00, "user.SignUp" , err )
}
}
return
return
}
}
@ -240,25 +253,28 @@ func Delete(ctx *middleware.Context) {
ctx . Data [ "Title" ] = "Delete Account"
ctx . Data [ "Title" ] = "Delete Account"
ctx . Data [ "PageIsUserSetting" ] = true
ctx . Data [ "PageIsUserSetting" ] = true
ctx . Data [ "IsUserPageSettingDelete" ] = true
ctx . Data [ "IsUserPageSettingDelete" ] = true
ctx . HTML ( 200 , "user/delete" )
}
if ctx . Req . Method == "GET" {
func DeletePost ( ctx * middleware . Context ) {
ctx . HTML ( 200 , "user/delete" )
ctx . Data [ "Title" ] = "Delete Account"
return
ctx . Data [ "PageIsUserSetting" ] = true
}
ctx . Data [ "IsUserPageSettingDelete" ] = true
tmpUser := models . User { Passwd : ctx . Query ( "password" ) }
tmpUser := models . User {
Passwd : ctx . Query ( "password" ) ,
Salt : ctx . User . Salt ,
}
tmpUser . EncodePasswd ( )
tmpUser . EncodePasswd ( )
if len ( tmpUser . Passwd ) == 0 || tmpUser . Passwd != ctx . User . Passwd {
if tmpUser . Passwd != ctx . User . Passwd {
ctx . Data [ "HasError" ] = true
ctx . Flash . Error ( "Password is not correct. Make sure you are owner of this account." )
ctx . Data [ "ErrorMsg" ] = "Password is not correct. Make sure you are owner of this account."
} else {
} else {
if err := models . DeleteUser ( ctx . User ) ; err != nil {
if err := models . DeleteUser ( ctx . User ) ; err != nil {
ctx . Data [ "HasError" ] = true
switch err {
switch err {
case models . ErrUserOwnRepos :
case models . ErrUserOwnRepos :
ctx . Data [ "ErrorMsg" ] = "Your account still have ownership of repository, you have to delete or transfer them first."
ctx . Flash . Error ( "Your account still have ownership of repository, you have to delete or transfer them first." )
default :
default :
ctx . Handle ( 2 00, "user.Delete" , err )
ctx . Handle ( 5 00, "user.Delete" , err )
return
return
}
}
} else {
} else {
@ -267,7 +283,7 @@ func Delete(ctx *middleware.Context) {
}
}
}
}
ctx . HTML ( 200 , " user/delete")
ctx . Redirect ( "/ user/delete")
}
}
const (
const (
@ -439,10 +455,17 @@ func ForgotPasswd(ctx *middleware.Context) {
}
}
ctx . Data [ "IsResetRequest" ] = true
ctx . Data [ "IsResetRequest" ] = true
if ctx . Req . Method == "GET" {
ctx . HTML ( 200 , "user/forgot_passwd" )
ctx . HTML ( 200 , "user/forgot_passwd" )
}
func ForgotPasswdPost ( ctx * middleware . Context ) {
ctx . Data [ "Title" ] = "Forgot Password"
if base . MailService == nil {
ctx . Handle ( 403 , "user.ForgotPasswdPost" , nil )
return
return
}
}
ctx . Data [ "IsResetRequest" ] = true
email := ctx . Query ( "email" )
email := ctx . Query ( "email" )
u , err := models . GetUserByEmail ( email )
u , err := models . GetUserByEmail ( email )
@ -450,7 +473,7 @@ func ForgotPasswd(ctx *middleware.Context) {
if err == models . ErrUserNotExist {
if err == models . ErrUserNotExist {
ctx . RenderWithErr ( "This e-mail address does not associate to any account." , "user/forgot_passwd" , nil )
ctx . RenderWithErr ( "This e-mail address does not associate to any account." , "user/forgot_passwd" , nil )
} else {
} else {
ctx . Handle ( 404 , "user.ResetPasswd(check existence)" , err )
ctx . Handle ( 500 , "user.ResetPasswd(check existence)" , err )
}
}
return
return
}
}
@ -473,6 +496,8 @@ func ForgotPasswd(ctx *middleware.Context) {
}
}
func ResetPasswd ( ctx * middleware . Context ) {
func ResetPasswd ( ctx * middleware . Context ) {
ctx . Data [ "Title" ] = "Reset Password"
code := ctx . Query ( "code" )
code := ctx . Query ( "code" )
if len ( code ) == 0 {
if len ( code ) == 0 {
ctx . Error ( 404 )
ctx . Error ( 404 )
@ -480,11 +505,19 @@ func ResetPasswd(ctx *middleware.Context) {
}
}
ctx . Data [ "Code" ] = code
ctx . Data [ "Code" ] = code
if ctx . Req . Method == "GET" {
ctx . Data [ "IsResetForm" ] = true
ctx . Data [ "IsResetForm" ] = true
ctx . HTML ( 200 , "user/reset_passwd" )
ctx . HTML ( 200 , "user/reset_passwd" )
}
func ResetPasswdPost ( ctx * middleware . Context ) {
ctx . Data [ "Title" ] = "Reset Password"
code := ctx . Query ( "code" )
if len ( code ) == 0 {
ctx . Error ( 404 )
return
return
}
}
ctx . Data [ "Code" ] = code
if u := models . VerifyUserActiveCode ( code ) ; u != nil {
if u := models . VerifyUserActiveCode ( code ) ; u != nil {
// Validate password length.
// Validate password length.
@ -500,7 +533,7 @@ func ResetPasswd(ctx *middleware.Context) {
u . Salt = models . GetUserSalt ( )
u . Salt = models . GetUserSalt ( )
u . EncodePasswd ( )
u . EncodePasswd ( )
if err := models . UpdateUser ( u ) ; err != nil {
if err := models . UpdateUser ( u ) ; err != nil {
ctx . Handle ( 404 , "user.ResetPasswd(UpdateUser)" , err )
ctx . Handle ( 500 , "user.ResetPasswd(UpdateUser)" , err )
return
return
}
}