neko/server/internal/member/object/provider.go

125 lines
2.6 KiB
Go
Raw Normal View History

2021-03-15 00:57:19 +13:00
package object
import (
"github.com/demodesk/neko/pkg/types"
2021-03-15 00:57:19 +13:00
)
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,
entries: make(map[string]*memberEntry),
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
entries map[string]*memberEntry
2021-03-15 00:57:19 +13:00
}
2021-03-15 04:58:18 +13:00
func (provider *MemberProviderCtx) Connect() error {
var err error
for _, entry := range provider.config.Users {
_, err = provider.Insert(entry.Username, entry.Password, entry.Profile)
}
return err
2021-03-15 00:57:19 +13:00
}
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, ok := provider.entries[id]
2021-03-15 00:57:19 +13:00
if !ok {
2021-08-30 03:09:13 +12:00
return "", types.MemberProfile{}, types.ErrMemberDoesNotExist
2021-03-15 00:57:19 +13:00
}
// TODO: Use hash function.
if !entry.CheckPassword(password) {
2021-08-30 03:09:13 +12:00
return "", types.MemberProfile{}, types.ErrMemberInvalidPassword
2021-03-15 00:57:19 +13:00
}
return id, entry.profile, nil
2021-03-15 00:57:19 +13:00
}
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
_, ok := provider.entries[id]
2021-03-15 00:57:19 +13:00
if ok {
2021-08-30 03:09:13 +12:00
return "", types.ErrMemberAlreadyExists
2021-03-15 00:57:19 +13:00
}
provider.entries[id] = &memberEntry{
2021-03-15 02:44:03 +13:00
// TODO: Use hash function.
password: password,
profile: profile,
2021-03-15 02:44:03 +13:00
}
2021-03-15 00:57:19 +13:00
return id, nil
}
2021-03-15 04:58:18 +13:00
func (provider *MemberProviderCtx) UpdateProfile(id string, profile types.MemberProfile) error {
entry, ok := provider.entries[id]
2021-03-15 00:57:19 +13:00
if !ok {
2021-08-30 03:09:13 +12:00
return types.ErrMemberDoesNotExist
2021-03-15 00:57:19 +13:00
}
entry.profile = profile
2021-03-15 00:57:19 +13:00
return nil
}
2021-03-15 04:58:18 +13:00
func (provider *MemberProviderCtx) UpdatePassword(id string, password string) error {
entry, ok := provider.entries[id]
2021-03-15 00:57:19 +13:00
if !ok {
2021-08-30 03:09:13 +12:00
return types.ErrMemberDoesNotExist
2021-03-15 00:57:19 +13:00
}
// TODO: Use hash function.
entry.password = password
2021-03-15 00:57:19 +13:00
return nil
}
2021-03-15 04:58:18 +13:00
func (provider *MemberProviderCtx) Select(id string) (types.MemberProfile, error) {
entry, ok := provider.entries[id]
2021-03-15 02:44:03 +13:00
if !ok {
2021-08-30 03:09:13 +12:00
return types.MemberProfile{}, types.ErrMemberDoesNotExist
2021-03-15 00:57:19 +13:00
}
return entry.profile, nil
2021-03-15 00:57:19 +13:00
}
2021-03-15 04:58:18 +13:00
func (provider *MemberProviderCtx) SelectAll(limit int, offset int) (map[string]types.MemberProfile, error) {
2021-03-15 02:44:03 +13:00
profiles := make(map[string]types.MemberProfile)
2021-03-15 00:57:19 +13:00
i := 0
2021-03-15 04:58:18 +13:00
for id, entry := range provider.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 {
_, ok := provider.entries[id]
2021-03-15 00:57:19 +13:00
if !ok {
2021-08-30 03:09:13 +12:00
return types.ErrMemberDoesNotExist
2021-03-15 00:57:19 +13:00
}
2021-03-15 04:58:18 +13:00
delete(provider.entries, id)
2021-03-15 00:57:19 +13:00
return nil
}