From 20cb97f90fdccc1e015e10b81118aee8f4c9e6ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torjus=20H=C3=A5kestad?= Date: Fri, 21 Jan 2022 14:04:41 +0100 Subject: [PATCH] Add user list --- users/user.go | 9 ++++++++- users/userstore_bolt.go | 21 +++++++++++++++++++++ users/userstore_memory.go | 13 +++++++++++++ users/userstore_test.go | 22 ++++++++++++++++++++++ 4 files changed, 64 insertions(+), 1 deletion(-) diff --git a/users/user.go b/users/user.go index 762264c..b2c2a27 100644 --- a/users/user.go +++ b/users/user.go @@ -1,6 +1,10 @@ package users -import "golang.org/x/crypto/bcrypt" +import ( + "fmt" + + "golang.org/x/crypto/bcrypt" +) type Role string @@ -16,10 +20,13 @@ type User struct { Role Role `json:"role"` } +var ErrNoSuchUser = fmt.Errorf("no such user") + type UserStore interface { Get(username string) (*User, error) Store(user *User) error Delete(username string) error + List() ([]string, error) } func (u *User) ValidatePassword(password string) error { diff --git a/users/userstore_bolt.go b/users/userstore_bolt.go index 3ceff7c..908ac94 100644 --- a/users/userstore_bolt.go +++ b/users/userstore_bolt.go @@ -6,6 +6,8 @@ import ( "go.etcd.io/bbolt" ) +var _ UserStore = &BoltUserStore{} + var keyUsers = []byte("users") type BoltUserStore struct { @@ -67,3 +69,22 @@ func (s *BoltUserStore) Delete(username string) error { 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 +} diff --git a/users/userstore_memory.go b/users/userstore_memory.go index 5da183d..8980afd 100644 --- a/users/userstore_memory.go +++ b/users/userstore_memory.go @@ -5,6 +5,8 @@ import ( "sync" ) +var _ UserStore = &MemoryUserStore{} + type MemoryUserStore struct { users map[string]*User lock sync.Mutex @@ -37,3 +39,14 @@ func (s *MemoryUserStore) Delete(username string) error { delete(s.users, username) 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 +} diff --git a/users/userstore_test.go b/users/userstore_test.go index 7808633..bd15a73 100644 --- a/users/userstore_test.go +++ b/users/userstore_test.go @@ -47,5 +47,27 @@ func RunUserStoreTest(newFunc func() (func(), users.UserStore), t *testing.T) { 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") + } }) }