|
@ -22,11 +22,28 @@ type Session struct { |
|
|
ID string `json:"id"` |
|
|
ID string `json:"id"` |
|
|
UserID string `json:"user"` |
|
|
UserID string `json:"user"` |
|
|
Time time.Time `json:"time"` |
|
|
Time time.Time `json:"time"` |
|
|
|
|
|
data map[string]string |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// SetData sets session data
|
|
|
|
|
|
func (sess *Session) SetData(key, value string) { |
|
|
|
|
|
sess.data[key] = value |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// GetData gets session data for the following key
|
|
|
|
|
|
func (sess *Session) GetData(key string) string { |
|
|
|
|
|
return sess.data[key] |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// HasData checks if the session data contains the key
|
|
|
|
|
|
func (sess *Session) HasData(key string) bool { |
|
|
|
|
|
_, ok := sess.data[key] |
|
|
|
|
|
return ok |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// OpenSession creates a new session from the supplied user's ID
|
|
|
// OpenSession creates a new session from the supplied user's ID
|
|
|
func OpenSession(user *User) *Session { |
|
|
func OpenSession(user *User) *Session { |
|
|
session := &Session{generate.SessionID(), user.FullID(), time.Now()} |
|
|
|
|
|
|
|
|
session := &Session{generate.SessionID(), user.FullID(), time.Now(), make(map[string]string)} |
|
|
|
|
|
|
|
|
sessionMutex.Lock() |
|
|
sessionMutex.Lock() |
|
|
sessions[session.ID] = session |
|
|
sessions[session.ID] = session |
|
@ -38,6 +55,8 @@ func OpenSession(user *User) *Session { |
|
|
go cleanup() |
|
|
go cleanup() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
user.Session = session |
|
|
|
|
|
|
|
|
return session |
|
|
return session |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|