neko/internal/member/file/manager.go

219 lines
4.2 KiB
Go
Raw Normal View History

2021-03-15 00:57:19 +13:00
package file
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"sync"
"demodesk/neko/internal/types"
)
2021-03-15 01:07:03 +13:00
func New(config Config) types.MemberManager {
2021-03-15 00:57:19 +13:00
return &MemberManagerCtx{
2021-03-15 01:07:03 +13:00
config: config,
mu: sync.Mutex{},
2021-03-15 00:57:19 +13:00
}
}
type MemberManagerCtx struct {
2021-03-15 01:07:03 +13:00
config Config
mu sync.Mutex
2021-03-15 00:57:19 +13:00
}
func (manager *MemberManagerCtx) Connect() error {
return nil
}
func (manager *MemberManagerCtx) Disconnect() error {
return nil
}
func (manager *MemberManagerCtx) Authenticate(username string, password string) (string, types.MemberProfile, error) {
manager.mu.Lock()
defer manager.mu.Unlock()
// id will be also username
id := username
entry, err := manager.getEntry(id)
if err != nil {
return "", types.MemberProfile{}, err
}
// TODO: Use hash function.
if entry.Password != password {
return "", types.MemberProfile{}, fmt.Errorf("Invalid password.")
}
return id, entry.Profile, nil
}
func (manager *MemberManagerCtx) Insert(username string, password string, profile types.MemberProfile) (string, error) {
manager.mu.Lock()
defer manager.mu.Unlock()
// id will be also username
id := username
entries, err := manager.deserialize()
if err != nil {
return "", err
}
2021-03-15 02:44:03 +13:00
_, ok := entries[id]
2021-03-15 00:57:19 +13:00
if ok {
return "", fmt.Errorf("Member ID already exists.")
}
2021-03-15 02:44:03 +13:00
entries[id] = MemberEntry{
// TODO: Use hash function.
Password: password,
Profile: profile,
}
2021-03-15 00:57:19 +13:00
return id, manager.serialize(entries)
}
2021-03-15 02:44:03 +13:00
func (manager *MemberManagerCtx) UpdateProfile(id string, profile types.MemberProfile) error {
2021-03-15 00:57:19 +13:00
manager.mu.Lock()
defer manager.mu.Unlock()
entries, err := manager.deserialize()
if err != nil {
return err
}
entry, ok := entries[id]
if !ok {
return fmt.Errorf("Member ID does not exist.")
}
entry.Profile = profile
entries[id] = entry
return manager.serialize(entries)
}
func (manager *MemberManagerCtx) UpdatePassword(id string, password string) error {
manager.mu.Lock()
defer manager.mu.Unlock()
entries, err := manager.deserialize()
if err != nil {
return err
}
entry, ok := entries[id]
if !ok {
return fmt.Errorf("Member ID does not exist.")
}
// TODO: Use hash function.
entry.Password = password
entries[id] = entry
return manager.serialize(entries)
}
func (manager *MemberManagerCtx) Select(id string) (types.MemberProfile, error) {
manager.mu.Lock()
defer manager.mu.Unlock()
entry, err := manager.getEntry(id)
if err != nil {
return types.MemberProfile{}, err
}
return entry.Profile, nil
}
func (manager *MemberManagerCtx) SelectAll(limit int, offset int) (map[string]types.MemberProfile, error) {
manager.mu.Lock()
defer manager.mu.Unlock()
profiles := map[string]types.MemberProfile{}
entries, err := manager.deserialize()
if err != nil {
return profiles, err
}
i := 0
for id, entry := range entries {
2021-03-15 02:44:03 +13:00
if i >= offset && (limit == 0 || i < offset+limit) {
profiles[id] = entry.Profile
2021-03-15 00:57:19 +13:00
}
i = i + 1
}
return profiles, nil
}
func (manager *MemberManagerCtx) Delete(id string) error {
manager.mu.Lock()
defer manager.mu.Unlock()
entries, err := manager.deserialize()
if err != nil {
return err
}
_, ok := entries[id]
if !ok {
return fmt.Errorf("Member ID does not exist.")
}
delete(entries, id)
return manager.serialize(entries)
}
func (manager *MemberManagerCtx) deserialize() (map[string]MemberEntry, error) {
2021-03-15 01:07:03 +13:00
file, err := os.OpenFile(manager.config.File, os.O_RDONLY|os.O_CREATE, os.ModePerm)
2021-03-15 00:57:19 +13:00
if err != nil {
return nil, err
}
raw, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}
if len(raw) == 0 {
return map[string]MemberEntry{}, nil
}
var entries map[string]MemberEntry
if err := json.Unmarshal([]byte(raw), &entries); err != nil {
return nil, err
}
return entries, nil
}
func (manager *MemberManagerCtx) getEntry(id string) (MemberEntry, error) {
entries, err := manager.deserialize()
if err != nil {
return MemberEntry{}, err
}
entry, ok := entries[id]
if !ok {
return MemberEntry{}, fmt.Errorf("Member ID does not exist.")
}
return entry, nil
}
func (manager *MemberManagerCtx) serialize(data map[string]MemberEntry) error {
raw, err := json.Marshal(data)
if err != nil {
return err
}
2021-03-15 01:07:03 +13:00
return ioutil.WriteFile(manager.config.File, raw, os.ModePerm)
2021-03-15 00:57:19 +13:00
}