mirror of
https://github.com/kataras/iris.git
synced 2025-02-09 02:34:55 +01:00
add access to the session manager through new 'Session.Man' field
relative to: https://github.com/kataras/iris/issues/1485 Former-commit-id: c4ced38b74af42bfcd17abe6b439b35db6837bbf
This commit is contained in:
parent
486e57b9ae
commit
a1e6d81b49
|
@ -36,7 +36,7 @@ func (p *provider) RegisterDatabase(db Database) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// newSession returns a new session from sessionid
|
// newSession returns a new session from sessionid
|
||||||
func (p *provider) newSession(sid string, expires time.Duration) *Session {
|
func (p *provider) newSession(man *Sessions, sid string, expires time.Duration) *Session {
|
||||||
onExpire := func() {
|
onExpire := func() {
|
||||||
p.Destroy(sid)
|
p.Destroy(sid)
|
||||||
}
|
}
|
||||||
|
@ -60,6 +60,7 @@ func (p *provider) newSession(sid string, expires time.Duration) *Session {
|
||||||
|
|
||||||
sess := &Session{
|
sess := &Session{
|
||||||
sid: sid,
|
sid: sid,
|
||||||
|
Man: man,
|
||||||
provider: p,
|
provider: p,
|
||||||
flashes: make(map[string]*flashMessage),
|
flashes: make(map[string]*flashMessage),
|
||||||
Lifetime: lifetime,
|
Lifetime: lifetime,
|
||||||
|
@ -69,8 +70,8 @@ func (p *provider) newSession(sid string, expires time.Duration) *Session {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init creates the session and returns it
|
// Init creates the session and returns it
|
||||||
func (p *provider) Init(sid string, expires time.Duration) *Session {
|
func (p *provider) Init(man *Sessions, sid string, expires time.Duration) *Session {
|
||||||
newSession := p.newSession(sid, expires)
|
newSession := p.newSession(man, sid, expires)
|
||||||
p.mu.Lock()
|
p.mu.Lock()
|
||||||
p.sessions[sid] = newSession
|
p.sessions[sid] = newSession
|
||||||
p.mu.Unlock()
|
p.mu.Unlock()
|
||||||
|
@ -110,7 +111,7 @@ func (p *provider) UpdateExpiration(sid string, expires time.Duration) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read returns the store which sid parameter belongs
|
// Read returns the store which sid parameter belongs
|
||||||
func (p *provider) Read(sid string, expires time.Duration) *Session {
|
func (p *provider) Read(man *Sessions, sid string, expires time.Duration) *Session {
|
||||||
p.mu.Lock()
|
p.mu.Lock()
|
||||||
if sess, found := p.sessions[sid]; found {
|
if sess, found := p.sessions[sid]; found {
|
||||||
sess.runFlashGC() // run the flash messages GC, new request here of existing session
|
sess.runFlashGC() // run the flash messages GC, new request here of existing session
|
||||||
|
@ -120,7 +121,7 @@ func (p *provider) Read(sid string, expires time.Duration) *Session {
|
||||||
}
|
}
|
||||||
p.mu.Unlock()
|
p.mu.Unlock()
|
||||||
|
|
||||||
return p.Init(sid, expires) // if not found create new
|
return p.Init(man, sid, expires) // if not found create new
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *provider) registerDestroyListener(ln DestroyListener) {
|
func (p *provider) registerDestroyListener(ln DestroyListener) {
|
||||||
|
|
|
@ -15,11 +15,16 @@ type (
|
||||||
//
|
//
|
||||||
// This is what will be returned when sess := sessions.Start().
|
// This is what will be returned when sess := sessions.Start().
|
||||||
Session struct {
|
Session struct {
|
||||||
sid string
|
sid string
|
||||||
isNew bool
|
isNew bool
|
||||||
flashes map[string]*flashMessage
|
flashes map[string]*flashMessage
|
||||||
mu sync.RWMutex // for flashes.
|
mu sync.RWMutex // for flashes.
|
||||||
|
// Lifetime it contains the expiration data, use it for read-only information.
|
||||||
|
// See `Sessions.UpdateExpiration` too.
|
||||||
Lifetime LifeTime
|
Lifetime LifeTime
|
||||||
|
// Man is the sessions manager that this session created of.
|
||||||
|
Man *Sessions
|
||||||
|
|
||||||
provider *provider
|
provider *provider
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,7 +42,7 @@ type (
|
||||||
// Note that this method does NOT remove the client's cookie, although
|
// Note that this method does NOT remove the client's cookie, although
|
||||||
// it should be reseted if new session is attached to that (client).
|
// it should be reseted if new session is attached to that (client).
|
||||||
//
|
//
|
||||||
// Use the session's manager `Destroy(ctx)` in order to remove the cookie as well.
|
// Use the session's manager `Destroy(ctx)` in order to remove the cookie instead.
|
||||||
func (s *Session) Destroy() {
|
func (s *Session) Destroy() {
|
||||||
s.provider.deleteSession(s)
|
s.provider.deleteSession(s)
|
||||||
}
|
}
|
||||||
|
|
|
@ -84,7 +84,7 @@ func (s *Sessions) Start(ctx context.Context, cookieOptions ...context.CookieOpt
|
||||||
if cookieValue == "" { // cookie doesn't exist, let's generate a session and set a cookie.
|
if cookieValue == "" { // cookie doesn't exist, let's generate a session and set a cookie.
|
||||||
sid := s.config.SessionIDGenerator(ctx)
|
sid := s.config.SessionIDGenerator(ctx)
|
||||||
|
|
||||||
sess := s.provider.Init(sid, s.config.Expires)
|
sess := s.provider.Init(s, sid, s.config.Expires)
|
||||||
sess.isNew = s.provider.db.Len(sid) == 0
|
sess.isNew = s.provider.db.Len(sid) == 0
|
||||||
|
|
||||||
s.updateCookie(ctx, sid, s.config.Expires, cookieOptions...)
|
s.updateCookie(ctx, sid, s.config.Expires, cookieOptions...)
|
||||||
|
@ -92,7 +92,7 @@ func (s *Sessions) Start(ctx context.Context, cookieOptions ...context.CookieOpt
|
||||||
return sess
|
return sess
|
||||||
}
|
}
|
||||||
|
|
||||||
return s.provider.Read(cookieValue, s.config.Expires)
|
return s.provider.Read(s, cookieValue, s.config.Expires)
|
||||||
}
|
}
|
||||||
|
|
||||||
const contextSessionKey = "iris.session"
|
const contextSessionKey = "iris.session"
|
||||||
|
|
|
@ -237,12 +237,12 @@ func TestSessionsUpdateExpiration(t *testing.T) {
|
||||||
app.Post("/remember_me", func(ctx iris.Context) {
|
app.Post("/remember_me", func(ctx iris.Context) {
|
||||||
// re-sends the cookie with the new Expires and MaxAge fields,
|
// re-sends the cookie with the new Expires and MaxAge fields,
|
||||||
// test checks that on same session id too.
|
// test checks that on same session id too.
|
||||||
sess.UpdateExpiration(ctx, 24*time.Hour)
|
sessions.Get(ctx).Man.UpdateExpiration(ctx, 24*time.Hour)
|
||||||
writeResponse(ctx)
|
writeResponse(ctx)
|
||||||
})
|
})
|
||||||
|
|
||||||
app.Get("/destroy", func(ctx iris.Context) {
|
app.Get("/destroy", func(ctx iris.Context) {
|
||||||
sess.Destroy(ctx) // this will delete the cookie too.
|
sessions.Get(ctx).Man.Destroy(ctx) // this will delete the cookie too.
|
||||||
})
|
})
|
||||||
|
|
||||||
e := httptest.New(t, app, httptest.URL("http://example.com"))
|
e := httptest.New(t, app, httptest.URL("http://example.com"))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user