neko/internal/member/file/provider.go

195 lines
3.8 KiB
Go
Raw Normal View History

2021-03-15 00:57:19 +13:00
package file
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"demodesk/neko/internal/types"
)
2021-03-15 04:58:18 +13:00
func New(config Config) types.MemberProvider {
return &MemberProviderCtx{
2021-03-15 01:07:03 +13:00
config: config,
2021-03-15 00:57:19 +13:00
}
}
2021-03-15 04:58:18 +13:00
type MemberProviderCtx struct {
2021-03-15 01:07:03 +13:00
config Config
2021-03-15 00:57:19 +13:00
}
2021-03-15 04:58:18 +13:00
func (provider *MemberProviderCtx) Connect() error {
2021-03-15 00:57:19 +13:00
return nil
}
2021-03-15 04:58:18 +13:00
func (provider *MemberProviderCtx) Disconnect() error {
2021-03-15 00:57:19 +13:00
return nil
}
2021-03-15 04:58:18 +13:00
func (provider *MemberProviderCtx) Authenticate(username string, password string) (string, types.MemberProfile, error) {
2021-03-15 00:57:19 +13:00
// id will be also username
id := username
2021-03-15 04:58:18 +13:00
entry, err := provider.getEntry(id)
2021-03-15 00:57:19 +13:00
if err != nil {
return "", types.MemberProfile{}, err
}
// TODO: Use hash function.
if entry.Password != password {
2021-04-25 07:37:43 +12:00
return "", types.MemberProfile{}, fmt.Errorf("invalid password")
2021-03-15 00:57:19 +13:00
}
return id, entry.Profile, nil
}
2021-03-15 04:58:18 +13:00
func (provider *MemberProviderCtx) Insert(username string, password string, profile types.MemberProfile) (string, error) {
2021-03-15 00:57:19 +13:00
// id will be also username
id := username
2021-03-15 04:58:18 +13:00
entries, err := provider.deserialize()
2021-03-15 00:57:19 +13:00
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 {
2021-04-25 07:37:43 +12:00
return "", fmt.Errorf("member ID already exists")
2021-03-15 00:57:19 +13:00
}
2021-03-15 02:44:03 +13:00
entries[id] = MemberEntry{
// TODO: Use hash function.
Password: password,
2021-03-15 08:26:58 +13:00
Profile: profile,
2021-03-15 02:44:03 +13:00
}
2021-03-15 00:57:19 +13:00
2021-03-15 04:58:18 +13:00
return id, provider.serialize(entries)
2021-03-15 00:57:19 +13:00
}
2021-03-15 04:58:18 +13:00
func (provider *MemberProviderCtx) UpdateProfile(id string, profile types.MemberProfile) error {
entries, err := provider.deserialize()
2021-03-15 00:57:19 +13:00
if err != nil {
return err
}
entry, ok := entries[id]
if !ok {
2021-04-25 07:37:43 +12:00
return fmt.Errorf("member ID does not exist")
2021-03-15 00:57:19 +13:00
}
entry.Profile = profile
entries[id] = entry
2021-03-15 04:58:18 +13:00
return provider.serialize(entries)
2021-03-15 00:57:19 +13:00
}
2021-03-15 04:58:18 +13:00
func (provider *MemberProviderCtx) UpdatePassword(id string, password string) error {
entries, err := provider.deserialize()
2021-03-15 00:57:19 +13:00
if err != nil {
return err
}
entry, ok := entries[id]
if !ok {
2021-04-25 07:37:43 +12:00
return fmt.Errorf("member ID does not exist")
2021-03-15 00:57:19 +13:00
}
// TODO: Use hash function.
entry.Password = password
entries[id] = entry
2021-03-15 04:58:18 +13:00
return provider.serialize(entries)
2021-03-15 00:57:19 +13:00
}
2021-03-15 04:58:18 +13:00
func (provider *MemberProviderCtx) Select(id string) (types.MemberProfile, error) {
entry, err := provider.getEntry(id)
2021-03-15 00:57:19 +13:00
if err != nil {
return types.MemberProfile{}, err
}
return entry.Profile, nil
}
2021-03-15 04:58:18 +13:00
func (provider *MemberProviderCtx) SelectAll(limit int, offset int) (map[string]types.MemberProfile, error) {
2021-03-15 00:57:19 +13:00
profiles := map[string]types.MemberProfile{}
2021-03-15 04:58:18 +13:00
entries, err := provider.deserialize()
2021-03-15 00:57:19 +13:00
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
}
2021-03-15 04:58:18 +13:00
func (provider *MemberProviderCtx) Delete(id string) error {
entries, err := provider.deserialize()
2021-03-15 00:57:19 +13:00
if err != nil {
return err
}
_, ok := entries[id]
if !ok {
2021-04-25 07:37:43 +12:00
return fmt.Errorf("member ID does not exist")
2021-03-15 00:57:19 +13:00
}
delete(entries, id)
2021-03-15 04:58:18 +13:00
return provider.serialize(entries)
2021-03-15 00:57:19 +13:00
}
2021-03-15 04:58:18 +13:00
func (provider *MemberProviderCtx) deserialize() (map[string]MemberEntry, error) {
2021-03-17 03:28:40 +13:00
file, err := os.OpenFile(provider.config.Path, 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
}
2021-03-15 04:58:18 +13:00
func (provider *MemberProviderCtx) getEntry(id string) (MemberEntry, error) {
entries, err := provider.deserialize()
2021-03-15 00:57:19 +13:00
if err != nil {
return MemberEntry{}, err
}
entry, ok := entries[id]
if !ok {
2021-04-25 07:37:43 +12:00
return MemberEntry{}, fmt.Errorf("member ID does not exist")
2021-03-15 00:57:19 +13:00
}
return entry, nil
}
2021-03-15 04:58:18 +13:00
func (provider *MemberProviderCtx) serialize(data map[string]MemberEntry) error {
2021-03-15 00:57:19 +13:00
raw, err := json.Marshal(data)
if err != nil {
return err
}
2021-03-17 03:28:40 +13:00
return ioutil.WriteFile(provider.config.Path, raw, os.ModePerm)
2021-03-15 00:57:19 +13:00
}