2016-11-16 13:47:49 +00:00
|
|
|
package inmem
|
2016-08-28 03:59:17 +00:00
|
|
|
|
2016-10-13 18:21:47 +00:00
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
|
2016-11-16 13:47:49 +00:00
|
|
|
"github.com/kolide/kolide-ose/server/errors"
|
2016-10-13 18:21:47 +00:00
|
|
|
"github.com/kolide/kolide-ose/server/kolide"
|
|
|
|
)
|
2016-08-28 03:59:17 +00:00
|
|
|
|
2016-11-16 13:47:49 +00:00
|
|
|
func (orm *Datastore) NewUser(user *kolide.User) (*kolide.User, error) {
|
2016-08-28 03:59:17 +00:00
|
|
|
orm.mtx.Lock()
|
|
|
|
defer orm.mtx.Unlock()
|
|
|
|
|
|
|
|
for _, in := range orm.users {
|
|
|
|
if in.Username == user.Username {
|
2016-11-16 13:47:49 +00:00
|
|
|
return nil, errors.ErrExists
|
2016-08-28 03:59:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-14 16:15:04 +00:00
|
|
|
user.ID = orm.nextID(user)
|
2016-08-28 03:59:17 +00:00
|
|
|
orm.users[user.ID] = user
|
|
|
|
|
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
2016-11-16 13:47:49 +00:00
|
|
|
func (orm *Datastore) User(username string) (*kolide.User, error) {
|
2016-08-28 03:59:17 +00:00
|
|
|
orm.mtx.Lock()
|
|
|
|
defer orm.mtx.Unlock()
|
|
|
|
|
|
|
|
for _, user := range orm.users {
|
|
|
|
if user.Username == username {
|
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-16 13:47:49 +00:00
|
|
|
return nil, errors.ErrNotFound
|
2016-08-28 03:59:17 +00:00
|
|
|
}
|
|
|
|
|
2016-11-16 13:47:49 +00:00
|
|
|
func (orm *Datastore) ListUsers(opt kolide.ListOptions) ([]*kolide.User, error) {
|
2016-09-15 14:52:17 +00:00
|
|
|
orm.mtx.Lock()
|
|
|
|
defer orm.mtx.Unlock()
|
|
|
|
|
2016-10-13 18:21:47 +00:00
|
|
|
// We need to sort by keys to provide reliable ordering
|
|
|
|
keys := []int{}
|
|
|
|
for k, _ := range orm.users {
|
|
|
|
keys = append(keys, int(k))
|
2016-09-15 14:52:17 +00:00
|
|
|
}
|
2016-10-13 18:21:47 +00:00
|
|
|
sort.Ints(keys)
|
|
|
|
|
|
|
|
users := []*kolide.User{}
|
|
|
|
for _, k := range keys {
|
|
|
|
users = append(users, orm.users[uint(k)])
|
|
|
|
}
|
|
|
|
|
2016-10-17 14:01:14 +00:00
|
|
|
// Apply ordering
|
|
|
|
if opt.OrderKey != "" {
|
|
|
|
var fields = map[string]string{
|
|
|
|
"id": "ID",
|
|
|
|
"created_at": "CreatedAt",
|
|
|
|
"updated_at": "UpdatedAt",
|
|
|
|
"username": "Username",
|
|
|
|
"name": "Name",
|
|
|
|
"email": "Email",
|
|
|
|
"admin": "Admin",
|
|
|
|
"enabled": "Enabled",
|
|
|
|
"position": "Position",
|
|
|
|
}
|
|
|
|
if err := sortResults(users, opt, fields); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-13 18:21:47 +00:00
|
|
|
// Apply limit/offset
|
|
|
|
low, high := orm.getLimitOffsetSliceBounds(opt, len(users))
|
|
|
|
users = users[low:high]
|
2016-09-15 14:52:17 +00:00
|
|
|
|
|
|
|
return users, nil
|
|
|
|
}
|
|
|
|
|
2016-11-16 13:47:49 +00:00
|
|
|
func (orm *Datastore) UserByEmail(email string) (*kolide.User, error) {
|
2016-09-15 14:52:17 +00:00
|
|
|
orm.mtx.Lock()
|
|
|
|
defer orm.mtx.Unlock()
|
|
|
|
|
|
|
|
for _, user := range orm.users {
|
|
|
|
if user.Email == email {
|
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-16 13:47:49 +00:00
|
|
|
return nil, errors.ErrNotFound
|
2016-09-15 14:52:17 +00:00
|
|
|
}
|
|
|
|
|
2016-11-16 13:47:49 +00:00
|
|
|
func (orm *Datastore) UserByID(id uint) (*kolide.User, error) {
|
2016-08-28 03:59:17 +00:00
|
|
|
orm.mtx.Lock()
|
|
|
|
defer orm.mtx.Unlock()
|
|
|
|
|
|
|
|
if user, ok := orm.users[id]; ok {
|
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
2016-11-16 13:47:49 +00:00
|
|
|
return nil, errors.ErrNotFound
|
2016-08-28 03:59:17 +00:00
|
|
|
}
|
|
|
|
|
2016-11-16 13:47:49 +00:00
|
|
|
func (orm *Datastore) SaveUser(user *kolide.User) error {
|
2016-08-28 03:59:17 +00:00
|
|
|
orm.mtx.Lock()
|
|
|
|
defer orm.mtx.Unlock()
|
|
|
|
|
|
|
|
if _, ok := orm.users[user.ID]; !ok {
|
2016-11-16 13:47:49 +00:00
|
|
|
return errors.ErrNotFound
|
2016-08-28 03:59:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
orm.users[user.ID] = user
|
|
|
|
return nil
|
|
|
|
}
|