Add user list

This commit is contained in:
Torjus Håkestad 2022-01-21 14:04:41 +01:00
parent ed4a10c966
commit 20cb97f90f
4 changed files with 64 additions and 1 deletions

View File

@ -1,6 +1,10 @@
package users package users
import "golang.org/x/crypto/bcrypt" import (
"fmt"
"golang.org/x/crypto/bcrypt"
)
type Role string type Role string
@ -16,10 +20,13 @@ type User struct {
Role Role `json:"role"` Role Role `json:"role"`
} }
var ErrNoSuchUser = fmt.Errorf("no such user")
type UserStore interface { type UserStore interface {
Get(username string) (*User, error) Get(username string) (*User, error)
Store(user *User) error Store(user *User) error
Delete(username string) error Delete(username string) error
List() ([]string, error)
} }
func (u *User) ValidatePassword(password string) error { func (u *User) ValidatePassword(password string) error {

View File

@ -6,6 +6,8 @@ import (
"go.etcd.io/bbolt" "go.etcd.io/bbolt"
) )
var _ UserStore = &BoltUserStore{}
var keyUsers = []byte("users") var keyUsers = []byte("users")
type BoltUserStore struct { type BoltUserStore struct {
@ -67,3 +69,22 @@ func (s *BoltUserStore) Delete(username string) error {
return bkt.Delete([]byte(username)) return bkt.Delete([]byte(username))
}) })
} }
func (s *BoltUserStore) List() ([]string, error) {
var ids []string
err := s.db.View(func(tx *bbolt.Tx) error {
bkt := tx.Bucket(keyUsers)
c := bkt.Cursor()
for k, _ := c.First(); k != nil; k, _ = c.Next() {
ids = append(ids, string(k))
}
return nil
})
if err != nil {
return nil, err
}
return ids, nil
}

View File

@ -5,6 +5,8 @@ import (
"sync" "sync"
) )
var _ UserStore = &MemoryUserStore{}
type MemoryUserStore struct { type MemoryUserStore struct {
users map[string]*User users map[string]*User
lock sync.Mutex lock sync.Mutex
@ -37,3 +39,14 @@ func (s *MemoryUserStore) Delete(username string) error {
delete(s.users, username) delete(s.users, username)
return nil return nil
} }
func (s *MemoryUserStore) List() ([]string, error) {
s.lock.Lock()
defer s.lock.Unlock()
var ids []string
for k := range s.users {
ids = append(ids, k)
}
return ids, nil
}

View File

@ -47,5 +47,27 @@ func RunUserStoreTest(newFunc func() (func(), users.UserStore), t *testing.T) {
t.Errorf("User mismatch: %s", cmp.Diff(user, userMap[k])) t.Errorf("User mismatch: %s", cmp.Diff(user, userMap[k]))
} }
} }
l, err := s.List()
if err != nil {
t.Fatalf("Error listing users: %s", err)
}
if len(l) != len(userMap) {
t.Errorf("List wrong amount of users.")
}
for _, username := range l {
if err := s.Delete(username); err != nil {
t.Fatalf("Error deleting user: %s", err)
}
}
l, err = s.List()
if err != nil {
t.Fatalf("Error listing after delete: %s", err)
}
if len(l) != 0 {
t.Fatalf("List is not empty after deleting all")
}
}) })
} }