gpaste/users/userstore_memory.go

61 lines
966 B
Go
Raw Permalink Normal View History

2022-01-20 02:35:55 +00:00
package users
2022-01-19 20:45:53 +00:00
import (
"fmt"
"sync"
)
2022-01-21 13:04:41 +00:00
var _ UserStore = &MemoryUserStore{}
2022-01-19 20:45:53 +00:00
type MemoryUserStore struct {
users map[string]*User
lock sync.Mutex
}
func NewMemoryUserStore() *MemoryUserStore {
return &MemoryUserStore{users: make(map[string]*User)}
}
2022-01-27 10:36:16 +00:00
2022-01-19 20:45:53 +00:00
func (s *MemoryUserStore) Get(username string) (*User, error) {
s.lock.Lock()
defer s.lock.Unlock()
2022-01-24 19:25:52 +00:00
2022-01-19 20:45:53 +00:00
user, ok := s.users[username]
2022-01-24 19:25:52 +00:00
2022-01-19 20:45:53 +00:00
if !ok {
return nil, fmt.Errorf("no such user: %s", username)
}
return user, nil
}
func (s *MemoryUserStore) Store(user *User) error {
s.lock.Lock()
defer s.lock.Unlock()
2022-01-24 19:25:52 +00:00
2022-01-19 20:45:53 +00:00
s.users[user.Username] = user
2022-01-24 19:25:52 +00:00
2022-01-19 20:45:53 +00:00
return nil
}
func (s *MemoryUserStore) Delete(username string) error {
s.lock.Lock()
defer s.lock.Unlock()
2022-01-24 19:25:52 +00:00
2022-01-19 20:45:53 +00:00
delete(s.users, username)
2022-01-24 19:25:52 +00:00
2022-01-19 20:45:53 +00:00
return nil
}
2022-01-27 10:36:16 +00:00
2022-01-21 13:04:41 +00:00
func (s *MemoryUserStore) List() ([]string, error) {
s.lock.Lock()
defer s.lock.Unlock()
2022-01-24 19:25:52 +00:00
ids := make([]string, 0, len(s.users))
2022-01-21 13:04:41 +00:00
for k := range s.users {
ids = append(ids, k)
}
return ids, nil
}