diff options
author | Unknwon <u@gogs.io> | 2015-08-14 02:43:40 +0800 |
---|---|---|
committer | Unknwon <u@gogs.io> | 2015-08-14 02:43:40 +0800 |
commit | 817b48ed1e083b5291afd339bda573cba14bba8d (patch) | |
tree | 52a38a2c63c49afb45d45ac3fc06b3c730aa9cca /modules/middleware/auth.go | |
parent | 1fb53067f478414302096d5d976a3db85df9e2eb (diff) | |
download | gitea-817b48ed1e083b5291afd339bda573cba14bba8d.tar.gz gitea-817b48ed1e083b5291afd339bda573cba14bba8d.zip |
Show owner/poster tags of comments and fix #1312
Diffstat (limited to 'modules/middleware/auth.go')
-rw-r--r-- | modules/middleware/auth.go | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/modules/middleware/auth.go b/modules/middleware/auth.go index 2a02d27607..db643ccf28 100644 --- a/modules/middleware/auth.go +++ b/modules/middleware/auth.go @@ -5,12 +5,16 @@ package middleware import ( + "fmt" "net/url" "github.com/Unknwon/macaron" "github.com/macaron-contrib/csrf" + "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/auth" + "github.com/gogits/gogs/modules/base" + "github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/setting" ) @@ -21,6 +25,41 @@ type ToggleOptions struct { DisableCsrf bool } +// AutoSignIn reads cookie and try to auto-login. +func AutoSignIn(ctx *Context) (bool, error) { + uname := ctx.GetCookie(setting.CookieUserName) + if len(uname) == 0 { + return false, nil + } + + isSucceed := false + defer func() { + if !isSucceed { + log.Trace("auto-login cookie cleared: %s", uname) + ctx.SetCookie(setting.CookieUserName, "", -1, setting.AppSubUrl) + ctx.SetCookie(setting.CookieRememberName, "", -1, setting.AppSubUrl) + } + }() + + u, err := models.GetUserByName(uname) + if err != nil { + if !models.IsErrUserNotExist(err) { + return false, fmt.Errorf("GetUserByName: %v", err) + } + return false, nil + } + + if val, _ := ctx.GetSuperSecureCookie( + base.EncodeMd5(u.Rands+u.Passwd), setting.CookieRememberName); val != u.Name { + return false, nil + } + + isSucceed = true + ctx.Session.Set("uid", u.Id) + ctx.Session.Set("uname", u.Name) + return true, nil +} + func Toggle(options *ToggleOptions) macaron.Handler { return func(ctx *Context) { // Cannot view any page before installation. |