fleet/server/datastore/inmem/sessions.go
John Murphy 6a825c11e3 Datastore refactor (#439)
Removed Gorm, replaced it with Sqlx

* Added SQL bundling command to Makfile

* Using go-kit logger

* Added soft delete capability

* Changed SearchLabel to accept a variadic param for optional omit list
instead of array

* Gorm removed

* Refactor table structures to use CURRENT_TIMESTAMP mysql function

* Moved Inmem datastore into it's own package

* Updated README

* Implemented code review suggestions from @zwass

* Removed reference to Gorm from glide.yaml
2016-11-16 21:47:49 +08:00

89 lines
1.9 KiB
Go

package inmem
import (
"time"
"github.com/kolide/kolide-ose/server/errors"
"github.com/kolide/kolide-ose/server/kolide"
)
func (orm *Datastore) SessionByKey(key string) (*kolide.Session, error) {
orm.mtx.Lock()
defer orm.mtx.Unlock()
for _, session := range orm.sessions {
if session.Key == key {
return session, nil
}
}
return nil, errors.ErrNotFound
}
func (orm *Datastore) SessionByID(id uint) (*kolide.Session, error) {
orm.mtx.Lock()
defer orm.mtx.Unlock()
if session, ok := orm.sessions[id]; ok {
return session, nil
}
return nil, errors.ErrNotFound
}
func (orm *Datastore) ListSessionsForUser(id uint) ([]*kolide.Session, error) {
orm.mtx.Lock()
defer orm.mtx.Unlock()
var sessions []*kolide.Session
for _, session := range orm.sessions {
if session.UserID == id {
sessions = append(sessions, session)
}
}
if len(sessions) == 0 {
return nil, errors.ErrNotFound
}
return sessions, nil
}
func (orm *Datastore) NewSession(session *kolide.Session) (*kolide.Session, error) {
orm.mtx.Lock()
defer orm.mtx.Unlock()
session.ID = orm.nextID(session)
orm.sessions[session.ID] = session
if err := orm.MarkSessionAccessed(session); err != nil {
return nil, err
}
return session, nil
}
func (orm *Datastore) DestroySession(session *kolide.Session) error {
if _, ok := orm.sessions[session.ID]; !ok {
return errors.ErrNotFound
}
delete(orm.sessions, session.ID)
return nil
}
func (orm *Datastore) DestroyAllSessionsForUser(id uint) error {
for _, session := range orm.sessions {
if session.UserID == id {
delete(orm.sessions, session.ID)
}
}
return nil
}
func (orm *Datastore) MarkSessionAccessed(session *kolide.Session) error {
session.AccessedAt = time.Now().UTC()
if _, ok := orm.sessions[session.ID]; !ok {
return errors.ErrNotFound
}
orm.sessions[session.ID] = session
return nil
}
// TODO test session validation(expiration)