From 45462662e9bdb001f1cf3d4ca0e4d679757c7642 Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 10 Apr 2014 16:36:50 -0400 Subject: [PATCH] Add flash --- gogs.go | 2 +- models/git.go | 8 +- modules/log/log.go | 1 + modules/middleware/context.go | 14 ++- routers/install.go | 8 +- routers/user/setting.go | 24 ++-- routers/user/social.go | 7 +- routers/user/user.go | 179 ++++++++++++++++++------------ templates/base/alert.tmpl | 3 +- templates/status/500.tmpl | 4 +- templates/user/delete.tmpl | 5 +- templates/user/forgot_passwd.tmpl | 2 +- templates/user/reset_passwd.tmpl | 2 +- templates/user/setting.tmpl | 2 +- templates/user/signin.tmpl | 2 +- templates/user/signup.tmpl | 2 +- web.go | 43 +++---- 17 files changed, 179 insertions(+), 129 deletions(-) diff --git a/gogs.go b/gogs.go index 228fe89f40..72c506aff2 100644 --- a/gogs.go +++ b/gogs.go @@ -19,7 +19,7 @@ import ( // Test that go1.2 tag above is included in builds. main.go refers to this definition. const go12tag = true -const APP_VER = "0.2.3.0410 Alpha" +const APP_VER = "0.2.4.0410 Alpha" func init() { base.AppVer = APP_VER diff --git a/models/git.go b/models/git.go index 77b7ef2d7e..68e139056a 100644 --- a/models/git.go +++ b/models/git.go @@ -14,6 +14,8 @@ import ( "path" "strings" + "github.com/Unknwon/com" + "github.com/gogits/git" "github.com/gogits/gogs/modules/base" @@ -163,13 +165,11 @@ func getReposFiles(userName, repoName, commitId string, rpath string) ([]*RepoFi return 0 } - cmd := exec.Command("git", "log", "-1", "--pretty=format:%H", commitId, "--", path.Join(dirname, entry.Name)) - cmd.Dir = repopath - out, err := cmd.Output() + stdout, _, err := com.ExecCmdDir(repopath, "git", "log", "-1", "--pretty=format:%H", commitId, "--", path.Join(dirname, entry.Name)) if err != nil { return 0 } - filecm, err := repo.GetCommit(string(out)) + filecm, err := repo.GetCommit(string(stdout)) if err != nil { return 0 } diff --git a/modules/log/log.go b/modules/log/log.go index f21897b901..636ea787ca 100644 --- a/modules/log/log.go +++ b/modules/log/log.go @@ -21,6 +21,7 @@ func init() { func NewLogger(bufLen int64, mode, config string) { Mode, Config = mode, config logger = logs.NewLogger(bufLen) + logger.SetLogFuncCallDepth(3) logger.SetLogger(mode, config) } diff --git a/modules/middleware/context.go b/modules/middleware/context.go index 272af33052..6ee94b960b 100644 --- a/modules/middleware/context.go +++ b/modules/middleware/context.go @@ -91,10 +91,11 @@ func (ctx *Context) HTML(status int, name string, htmlOpt ...HTMLOptions) { // RenderWithErr used for page has form validation but need to prompt error to users. func (ctx *Context) RenderWithErr(msg, tpl string, form auth.Form) { - ctx.Flash.Error(msg) if form != nil { auth.AssignForm(form, ctx.Data) } + ctx.Flash.ErrorMsg = msg + ctx.Data["Flash"] = ctx.Flash ctx.HTML(200, tpl) } @@ -274,22 +275,25 @@ func InitContext() martini.Handler { // start session ctx.Session = base.SessionManager.SessionStart(res, r) - ctx.Flash = &Flash{} // Get flash. values, err := url.ParseQuery(ctx.GetCookie("gogs_flash")) if err != nil { log.Error("InitContext.ParseQuery(flash): %v", err) - } else { - ctx.Flash.Values = values + } else if len(values) > 0 { + ctx.Flash = &Flash{Values: values} + ctx.Flash.ErrorMsg = ctx.Flash.Get("error") + ctx.Flash.SuccessMsg = ctx.Flash.Get("success") ctx.Data["Flash"] = ctx.Flash + ctx.SetCookie("gogs_flash", "", -1) } + ctx.Flash = &Flash{Values: url.Values{}} rw := res.(martini.ResponseWriter) rw.Before(func(martini.ResponseWriter) { ctx.Session.SessionRelease(res) if flash := ctx.Flash.Encode(); len(flash) > 0 { - ctx.SetCookie("gogs_flash", ctx.Flash.Encode(), -1) + ctx.SetCookie("gogs_flash", ctx.Flash.Encode(), 0) } }) diff --git a/routers/install.go b/routers/install.go index d3686053b1..78ba383dee 100644 --- a/routers/install.go +++ b/routers/install.go @@ -23,10 +23,6 @@ import ( "github.com/gogits/gogs/modules/middleware" ) -type installRouter int - -var InstallRouter installRouter = 1 - // Check run mode(Default of martini is Dev). func checkRunMode() { switch base.Cfg.MustValue("", "RUN_MODE") { @@ -58,7 +54,7 @@ func GlobalInit() { checkRunMode() } -func (r installRouter) Get(ctx *middleware.Context, form auth.InstallForm) { +func Install(ctx *middleware.Context, form auth.InstallForm) { if base.InstallLock { ctx.Handle(404, "install.Install", errors.New("Installation is prohibited")) return @@ -101,7 +97,7 @@ func (r installRouter) Get(ctx *middleware.Context, form auth.InstallForm) { ctx.HTML(200, "install") } -func (r installRouter) Post(ctx *middleware.Context, form auth.InstallForm) { +func InstallPost(ctx *middleware.Context, form auth.InstallForm) { if base.InstallLock { ctx.Handle(404, "install.Install", errors.New("Installation is prohibited")) return diff --git a/routers/user/setting.go b/routers/user/setting.go index ea779e8549..03da04b9e3 100644 --- a/routers/user/setting.go +++ b/routers/user/setting.go @@ -14,8 +14,16 @@ import ( "github.com/gogits/gogs/modules/middleware" ) +func Setting(ctx *middleware.Context) { + ctx.Data["Title"] = "Setting" + ctx.Data["PageIsUserSetting"] = true + ctx.Data["IsUserPageSetting"] = true + ctx.Data["Owner"] = ctx.User + ctx.HTML(200, "user/setting") +} + // Render user setting page (email, website modify) -func Setting(ctx *middleware.Context, form auth.UpdateProfileForm) { +func SettingPost(ctx *middleware.Context, form auth.UpdateProfileForm) { ctx.Data["Title"] = "Setting" ctx.Data["PageIsUserSetting"] = true // For navbar arrow. ctx.Data["IsUserPageSetting"] = true // For setting nav highlight. @@ -23,7 +31,7 @@ func Setting(ctx *middleware.Context, form auth.UpdateProfileForm) { user := ctx.User ctx.Data["Owner"] = user - if ctx.Req.Method == "GET" || ctx.HasError() { + if ctx.HasError() { ctx.HTML(200, "user/setting") return } @@ -32,13 +40,13 @@ func Setting(ctx *middleware.Context, form auth.UpdateProfileForm) { if user.Name != form.UserName { isExist, err := models.IsUserExist(form.UserName) if err != nil { - ctx.Handle(404, "user.Setting(update: check existence)", err) + ctx.Handle(500, "user.Setting(update: check existence)", err) return } else if isExist { ctx.RenderWithErr("User name has been taken.", "user/setting", &form) return } else if err = models.ChangeUserName(user, form.UserName); err != nil { - ctx.Handle(404, "user.Setting(change user name)", err) + ctx.Handle(500, "user.Setting(change user name)", err) return } log.Trace("%s User name changed: %s -> %s", ctx.Req.RequestURI, user.Name, form.UserName) @@ -52,13 +60,13 @@ func Setting(ctx *middleware.Context, form auth.UpdateProfileForm) { user.Avatar = base.EncodeMd5(form.Avatar) user.AvatarEmail = form.Avatar if err := models.UpdateUser(user); err != nil { - ctx.Handle(200, "setting.Setting", err) + ctx.Handle(500, "setting.Setting", err) return } - - ctx.Data["IsSuccess"] = true - ctx.HTML(200, "user/setting") log.Trace("%s User setting updated: %s", ctx.Req.RequestURI, ctx.User.LowerName) + + ctx.Flash.Success("Your profile has been successfully updated.") + ctx.Redirect("/user/setting") } func SettingPassword(ctx *middleware.Context, form auth.UpdatePasswdForm) { diff --git a/routers/user/social.go b/routers/user/social.go index b87c313f5d..2b60ab9ffd 100644 --- a/routers/user/social.go +++ b/routers/user/social.go @@ -93,11 +93,10 @@ func SocialSignIn(ctx *middleware.Context, tokens oauth2.Tokens) { log.Info("login soc id: %v", socid) return } + config := &oauth.Config{ - //ClientId: base.OauthService.Github.ClientId, - //ClientSecret: base.OauthService.Github.ClientSecret, // FIXME: I don't know why compile error here - ClientId: "09383403ff2dc16daaa1", - ClientSecret: "0e4aa0c3630df396cdcea01a9d45cacf79925fea", + ClientId: base.OauthService.GitHub.ClientId, + ClientSecret: base.OauthService.GitHub.ClientSecret, RedirectURL: strings.TrimSuffix(base.AppUrl, "/") + ctx.Req.URL.RequestURI(), Scope: base.OauthService.GitHub.Scopes, AuthURL: "https://github.com/login/oauth/authorize", diff --git a/routers/user/user.go b/routers/user/user.go index 084d0bbde2..37c6baa9f2 100644 --- a/routers/user/user.go +++ b/routers/user/user.go @@ -74,57 +74,63 @@ func Profile(ctx *middleware.Context, params martini.Params) { ctx.HTML(200, "user/profile") } -func SignIn(ctx *middleware.Context, form auth.LogInForm) { +func SignIn(ctx *middleware.Context) { ctx.Data["Title"] = "Log In" - if ctx.Req.Method == "GET" { - if base.OauthService != nil { - ctx.Data["OauthEnabled"] = true - ctx.Data["OauthGitHubEnabled"] = base.OauthService.GitHub.Enabled - } + if base.OauthService != nil { + ctx.Data["OauthEnabled"] = true + 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 - defer func() { - if !isSucceed { - log.Trace("%s auto-login cookie cleared: %s", ctx.Req.RequestURI, userName) - ctx.SetCookie(base.CookieUserName, "", -1) - ctx.SetCookie(base.CookieRememberName, "", -1) - } - }() - - user, err := models.GetUserByName(userName) - if err != nil { - ctx.HTML(200, "user/signin") - return - } - - secret := base.EncodeMd5(user.Rands + user.Passwd) - value, _ := ctx.GetSecureCookie(secret, base.CookieRememberName) - if value != user.Name { - ctx.HTML(200, "user/signin") - return - } - - isSucceed = true - ctx.Session.Set("userId", user.Id) - ctx.Session.Set("userName", user.Name) - redirectTo, _ := url.QueryUnescape(ctx.GetCookie("redirect_to")) - if len(redirectTo) > 0 { - ctx.SetCookie("redirect_to", "", -1) - ctx.Redirect(redirectTo) - } else { - ctx.Redirect("/") - } + // Check auto-login. + userName := ctx.GetCookie(base.CookieUserName) + if len(userName) == 0 { + ctx.HTML(200, "user/signin") return } + isSucceed := false + defer func() { + if !isSucceed { + log.Trace("%s auto-login cookie cleared: %s", ctx.Req.RequestURI, userName) + ctx.SetCookie(base.CookieUserName, "", -1) + ctx.SetCookie(base.CookieRememberName, "", -1) + } + }() + + user, err := models.GetUserByName(userName) + if err != nil { + ctx.HTML(200, "user/signin") + return + } + + secret := base.EncodeMd5(user.Rands + user.Passwd) + value, _ := ctx.GetSecureCookie(secret, base.CookieRememberName) + if value != user.Name { + ctx.HTML(200, "user/signin") + 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() { ctx.HTML(200, "user/signin") return @@ -138,7 +144,7 @@ func SignIn(ctx *middleware.Context, form auth.LogInForm) { return } - ctx.Handle(200, "user.SignIn", err) + ctx.Handle(500, "user.SignIn", err) return } @@ -151,13 +157,13 @@ func SignIn(ctx *middleware.Context, form auth.LogInForm) { ctx.Session.Set("userId", user.Id) ctx.Session.Set("userName", user.Name) - redirectTo, _ := url.QueryUnescape(ctx.GetCookie("redirect_to")) - if len(redirectTo) > 0 { + if redirectTo, _ := url.QueryUnescape(ctx.GetCookie("redirect_to")); len(redirectTo) > 0 { ctx.SetCookie("redirect_to", "", -1) ctx.Redirect(redirectTo) - } else { - ctx.Redirect("/") + return } + + ctx.Redirect("/") } func SignOut(ctx *middleware.Context) { @@ -168,7 +174,7 @@ func SignOut(ctx *middleware.Context) { ctx.Redirect("/") } -func SignUp(ctx *middleware.Context, form auth.RegisterForm) { +func SignUp(ctx *middleware.Context) { ctx.Data["Title"] = "Sign Up" ctx.Data["PageIsSignUp"] = true @@ -178,8 +184,15 @@ func SignUp(ctx *middleware.Context, form auth.RegisterForm) { 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 } @@ -213,7 +226,7 @@ func SignUp(ctx *middleware.Context, form auth.RegisterForm) { case models.ErrUserNameIllegal: ctx.RenderWithErr(models.ErrRepoNameIllegal.Error(), "user/signup", &form) default: - ctx.Handle(200, "user.SignUp", err) + ctx.Handle(500, "user.SignUp", err) } return } @@ -240,25 +253,28 @@ func Delete(ctx *middleware.Context) { ctx.Data["Title"] = "Delete Account" ctx.Data["PageIsUserSetting"] = true ctx.Data["IsUserPageSettingDelete"] = true + ctx.HTML(200, "user/delete") +} - if ctx.Req.Method == "GET" { - ctx.HTML(200, "user/delete") - return +func DeletePost(ctx *middleware.Context) { + ctx.Data["Title"] = "Delete Account" + ctx.Data["PageIsUserSetting"] = true + ctx.Data["IsUserPageSettingDelete"] = true + + tmpUser := models.User{ + Passwd: ctx.Query("password"), + Salt: ctx.User.Salt, } - - tmpUser := models.User{Passwd: ctx.Query("password")} tmpUser.EncodePasswd() - if len(tmpUser.Passwd) == 0 || tmpUser.Passwd != ctx.User.Passwd { - ctx.Data["HasError"] = true - ctx.Data["ErrorMsg"] = "Password is not correct. Make sure you are owner of this account." + if tmpUser.Passwd != ctx.User.Passwd { + ctx.Flash.Error("Password is not correct. Make sure you are owner of this account.") } else { if err := models.DeleteUser(ctx.User); err != nil { - ctx.Data["HasError"] = true switch err { 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: - ctx.Handle(200, "user.Delete", err) + ctx.Handle(500, "user.Delete", err) return } } else { @@ -267,7 +283,7 @@ func Delete(ctx *middleware.Context) { } } - ctx.HTML(200, "user/delete") + ctx.Redirect("/user/delete") } const ( @@ -439,10 +455,17 @@ func ForgotPasswd(ctx *middleware.Context) { } 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 } + ctx.Data["IsResetRequest"] = true email := ctx.Query("email") u, err := models.GetUserByEmail(email) @@ -450,7 +473,7 @@ func ForgotPasswd(ctx *middleware.Context) { if err == models.ErrUserNotExist { ctx.RenderWithErr("This e-mail address does not associate to any account.", "user/forgot_passwd", nil) } else { - ctx.Handle(404, "user.ResetPasswd(check existence)", err) + ctx.Handle(500, "user.ResetPasswd(check existence)", err) } return } @@ -473,6 +496,8 @@ func ForgotPasswd(ctx *middleware.Context) { } func ResetPasswd(ctx *middleware.Context) { + ctx.Data["Title"] = "Reset Password" + code := ctx.Query("code") if len(code) == 0 { ctx.Error(404) @@ -480,11 +505,19 @@ func ResetPasswd(ctx *middleware.Context) { } ctx.Data["Code"] = code - if ctx.Req.Method == "GET" { - ctx.Data["IsResetForm"] = true - ctx.HTML(200, "user/reset_passwd") + ctx.Data["IsResetForm"] = true + 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 } + ctx.Data["Code"] = code if u := models.VerifyUserActiveCode(code); u != nil { // Validate password length. @@ -500,7 +533,7 @@ func ResetPasswd(ctx *middleware.Context) { u.Salt = models.GetUserSalt() u.EncodePasswd() if err := models.UpdateUser(u); err != nil { - ctx.Handle(404, "user.ResetPasswd(UpdateUser)", err) + ctx.Handle(500, "user.ResetPasswd(UpdateUser)", err) return } diff --git a/templates/base/alert.tmpl b/templates/base/alert.tmpl index 699314ace7..bb1eb6aab1 100644 --- a/templates/base/alert.tmpl +++ b/templates/base/alert.tmpl @@ -1 +1,2 @@ -{{if .Flash.ErrorMsg}}
{{.Flash.ErrorMsg}}
{{end}} \ No newline at end of file +{{if .Flash.ErrorMsg}}
{{.Flash.ErrorMsg}}
{{end}} +{{if .Flash.SuccessMsg}}
{{.Flash.SuccessMsg}}
{{end}} \ No newline at end of file diff --git a/templates/status/500.tmpl b/templates/status/500.tmpl index dd7358115d..07edd3620a 100644 --- a/templates/status/500.tmpl +++ b/templates/status/500.tmpl @@ -2,8 +2,8 @@ {{template "base/navbar" .}}

404

-
-

An error is occurred : {{.ErrorMsg}}

+ {{if .ErrorMsg}}
+

An error is occurred : {{.ErrorMsg}}

{{end}}

Application Version: {{AppVer}}

diff --git a/templates/user/delete.tmpl b/templates/user/delete.tmpl index 17c9ea8925..39949ee2b6 100644 --- a/templates/user/delete.tmpl +++ b/templates/user/delete.tmpl @@ -12,13 +12,16 @@
  • Delete Account
  • +

    Delete Account

    -

    {{if not .HasError}}The operation will delete your account permanently. Sorry to see you go, but we know you'll back soon.{{else}}{{.ErrorMsg}}{{end}}

    + {{template "base/alert" .}} + {{if not .Flash.ErrorMsg}}

    The operation will delete your account permanently. Sorry to see you go, but we know you'll back soon.

    {{end}}
    +