2021-03-15 00:57:19 +13:00
|
|
|
package object
|
|
|
|
|
|
|
|
import (
|
2022-07-14 10:58:22 +12:00
|
|
|
"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,
|
2023-01-13 12:39:30 +13:00
|
|
|
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
|
2023-01-13 12:39:30 +13:00
|
|
|
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 {
|
2021-03-15 01:11:26 +13:00
|
|
|
var err error
|
|
|
|
|
2023-01-13 12:39:30 +13:00
|
|
|
for _, entry := range provider.config.Users {
|
|
|
|
_, err = provider.Insert(entry.Username, entry.Password, entry.Profile)
|
2021-03-15 01:11:26 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2023-01-13 12:39:30 +13:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-01-13 12:39:30 +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
|
|
|
}
|
|
|
|
|
2023-01-13 12:39:30 +13:00
|
|
|
provider.entries[id] = &memberEntry{
|
2021-03-15 02:44:03 +13:00
|
|
|
// TODO: Use hash function.
|
2023-01-13 12:39:30 +13:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-01-13 12:39:30 +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.
|
2023-01-13 12:39:30 +13:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-01-13 12:39:30 +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) {
|
2023-01-13 12:39:30 +13:00
|
|
|
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
|
|
|
|
}
|