mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-06-01 04:12:10 +00:00
update session
This commit is contained in:
parent
0d1872ebe3
commit
f9c07c4186
7 changed files with 79 additions and 21 deletions
|
@ -9,7 +9,8 @@ import (
|
|||
"reflect"
|
||||
|
||||
"github.com/codegangsta/martini"
|
||||
"github.com/martini-contrib/sessions"
|
||||
|
||||
"github.com/gogits/session"
|
||||
|
||||
"github.com/gogits/binding"
|
||||
|
||||
|
@ -19,7 +20,7 @@ import (
|
|||
)
|
||||
|
||||
// SignedInId returns the id of signed in user.
|
||||
func SignedInId(session sessions.Session) int64 {
|
||||
func SignedInId(session session.SessionStore) int64 {
|
||||
userId := session.Get("userId")
|
||||
if userId == nil {
|
||||
return 0
|
||||
|
@ -34,7 +35,7 @@ func SignedInId(session sessions.Session) int64 {
|
|||
}
|
||||
|
||||
// SignedInName returns the name of signed in user.
|
||||
func SignedInName(session sessions.Session) string {
|
||||
func SignedInName(session session.SessionStore) string {
|
||||
userName := session.Get("userName")
|
||||
if userName == nil {
|
||||
return ""
|
||||
|
@ -46,7 +47,7 @@ func SignedInName(session sessions.Session) string {
|
|||
}
|
||||
|
||||
// SignedInUser returns the user object of signed user.
|
||||
func SignedInUser(session sessions.Session) *models.User {
|
||||
func SignedInUser(session session.SessionStore) *models.User {
|
||||
id := SignedInId(session)
|
||||
if id <= 0 {
|
||||
return nil
|
||||
|
@ -61,7 +62,7 @@ func SignedInUser(session sessions.Session) *models.User {
|
|||
}
|
||||
|
||||
// IsSignedIn check if any user has signed in.
|
||||
func IsSignedIn(session sessions.Session) bool {
|
||||
func IsSignedIn(session session.SessionStore) bool {
|
||||
return SignedInId(session) > 0
|
||||
}
|
||||
|
||||
|
|
|
@ -16,6 +16,7 @@ import (
|
|||
"github.com/Unknwon/goconfig"
|
||||
|
||||
"github.com/gogits/cache"
|
||||
"github.com/gogits/session"
|
||||
|
||||
"github.com/gogits/gogs/modules/log"
|
||||
)
|
||||
|
@ -49,6 +50,10 @@ var (
|
|||
|
||||
LogMode string
|
||||
LogConfig string
|
||||
|
||||
SessionProvider string
|
||||
SessionConfig *session.Config
|
||||
SessionManager *session.Manager
|
||||
)
|
||||
|
||||
var Service struct {
|
||||
|
@ -164,6 +169,30 @@ func newCacheService() {
|
|||
log.Info("Cache Service Enabled")
|
||||
}
|
||||
|
||||
func newSessionService() {
|
||||
SessionProvider = Cfg.MustValue("session", "PROVIDER", "memory")
|
||||
|
||||
SessionConfig = new(session.Config)
|
||||
SessionConfig.ProviderConfig = Cfg.MustValue("session", "PROVIDER_CONFIG")
|
||||
SessionConfig.CookieName = Cfg.MustValue("session", "COOKIE_NAME", "i_like_gogits")
|
||||
SessionConfig.CookieSecure = Cfg.MustBool("session", "COOKIE_SECURE")
|
||||
SessionConfig.EnableSetCookie = Cfg.MustBool("session", "ENABLE_SET_COOKIE", true)
|
||||
SessionConfig.GcIntervalTime = Cfg.MustInt64("session", "GC_INTERVAL_TIME", 86400)
|
||||
SessionConfig.SessionLifeTime = Cfg.MustInt64("session", "SESSION_LIFE_TIME", 86400)
|
||||
SessionConfig.SessionIDHashFunc = Cfg.MustValue("session", "SESSION_ID_HASHFUNC", "sha1")
|
||||
SessionConfig.SessionIDHashKey = Cfg.MustValue("session", "SESSION_ID_HASHKEY")
|
||||
|
||||
var err error
|
||||
SessionManager, err = session.NewManager(SessionProvider, *SessionConfig)
|
||||
if err != nil {
|
||||
fmt.Printf("Init session system failed, provider: %s, %v\n",
|
||||
SessionProvider, err)
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
log.Info("Session Service Enabled")
|
||||
}
|
||||
|
||||
func newMailService() {
|
||||
// Check mailer setting.
|
||||
if Cfg.MustBool("mailer", "ENABLED") {
|
||||
|
@ -234,6 +263,7 @@ func NewServices() {
|
|||
newService()
|
||||
newLogService()
|
||||
newCacheService()
|
||||
newSessionService()
|
||||
newMailService()
|
||||
newRegisterMailService()
|
||||
}
|
||||
|
|
|
@ -10,9 +10,9 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/codegangsta/martini"
|
||||
"github.com/martini-contrib/sessions"
|
||||
|
||||
"github.com/gogits/cache"
|
||||
"github.com/gogits/session"
|
||||
|
||||
"github.com/gogits/gogs/models"
|
||||
"github.com/gogits/gogs/modules/auth"
|
||||
|
@ -27,7 +27,7 @@ type Context struct {
|
|||
p martini.Params
|
||||
Req *http.Request
|
||||
Res http.ResponseWriter
|
||||
Session sessions.Session
|
||||
Session session.SessionStore
|
||||
Cache cache.Cache
|
||||
User *models.User
|
||||
IsSigned bool
|
||||
|
@ -92,21 +92,25 @@ func (ctx *Context) Handle(status int, title string, err error) {
|
|||
|
||||
// InitContext initializes a classic context for a request.
|
||||
func InitContext() martini.Handler {
|
||||
return func(res http.ResponseWriter, r *http.Request, c martini.Context,
|
||||
session sessions.Session, rd *Render) {
|
||||
return func(res http.ResponseWriter, r *http.Request, c martini.Context, rd *Render) {
|
||||
|
||||
ctx := &Context{
|
||||
c: c,
|
||||
// p: p,
|
||||
Req: r,
|
||||
Res: res,
|
||||
Session: session,
|
||||
Cache: base.Cache,
|
||||
Render: rd,
|
||||
Req: r,
|
||||
Res: res,
|
||||
Cache: base.Cache,
|
||||
Render: rd,
|
||||
}
|
||||
|
||||
// start session
|
||||
ctx.Session = base.SessionManager.SessionStart(res, r)
|
||||
defer func() {
|
||||
ctx.Session.SessionRelease(res)
|
||||
}()
|
||||
|
||||
// Get user from session if logined.
|
||||
user := auth.SignedInUser(session)
|
||||
user := auth.SignedInUser(ctx.Session)
|
||||
ctx.User = user
|
||||
ctx.IsSigned = user != nil
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue