mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 02:31:04 +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
|
||||
func (p *provider) newSession(sid string, expires time.Duration) *Session {
|
||||
func (p *provider) newSession(man *Sessions, sid string, expires time.Duration) *Session {
|
||||
onExpire := func() {
|
||||
p.Destroy(sid)
|
||||
}
|
||||
|
@ -60,6 +60,7 @@ func (p *provider) newSession(sid string, expires time.Duration) *Session {
|
|||
|
||||
sess := &Session{
|
||||
sid: sid,
|
||||
Man: man,
|
||||
provider: p,
|
||||
flashes: make(map[string]*flashMessage),
|
||||
Lifetime: lifetime,
|
||||
|
@ -69,8 +70,8 @@ func (p *provider) newSession(sid string, expires time.Duration) *Session {
|
|||
}
|
||||
|
||||
// Init creates the session and returns it
|
||||
func (p *provider) Init(sid string, expires time.Duration) *Session {
|
||||
newSession := p.newSession(sid, expires)
|
||||
func (p *provider) Init(man *Sessions, sid string, expires time.Duration) *Session {
|
||||
newSession := p.newSession(man, sid, expires)
|
||||
p.mu.Lock()
|
||||
p.sessions[sid] = newSession
|
||||
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
|
||||
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()
|
||||
if sess, found := p.sessions[sid]; found {
|
||||
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()
|
||||
|
||||
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) {
|
||||
|
|
|
@ -15,11 +15,16 @@ type (
|
|||
//
|
||||
// This is what will be returned when sess := sessions.Start().
|
||||
Session struct {
|
||||
sid string
|
||||
isNew bool
|
||||
flashes map[string]*flashMessage
|
||||
mu sync.RWMutex // for flashes.
|
||||
sid string
|
||||
isNew bool
|
||||
flashes map[string]*flashMessage
|
||||
mu sync.RWMutex // for flashes.
|
||||
// Lifetime it contains the expiration data, use it for read-only information.
|
||||
// See `Sessions.UpdateExpiration` too.
|
||||
Lifetime LifeTime
|
||||
// Man is the sessions manager that this session created of.
|
||||
Man *Sessions
|
||||
|
||||
provider *provider
|
||||
}
|
||||
|
||||
|
@ -37,7 +42,7 @@ type (
|
|||
// Note that this method does NOT remove the client's cookie, although
|
||||
// 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() {
|
||||
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.
|
||||
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
|
||||
|
||||
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 s.provider.Read(cookieValue, s.config.Expires)
|
||||
return s.provider.Read(s, cookieValue, s.config.Expires)
|
||||
}
|
||||
|
||||
const contextSessionKey = "iris.session"
|
||||
|
|
|
@ -237,12 +237,12 @@ func TestSessionsUpdateExpiration(t *testing.T) {
|
|||
app.Post("/remember_me", func(ctx iris.Context) {
|
||||
// re-sends the cookie with the new Expires and MaxAge fields,
|
||||
// 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)
|
||||
})
|
||||
|
||||
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"))
|
||||
|
|
Loading…
Reference in New Issue
Block a user