neko/internal/member/object/manager.go

175 lines
3.7 KiB
Go
Raw Normal View History

2021-03-15 00:57:19 +13:00
package object
import (
"fmt"
"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,
2021-03-15 00:57:19 +13:00
entries: make(map[string]MemberEntry),
mu: sync.Mutex{},
}
}
type MemberManagerCtx struct {
2021-03-15 01:07:03 +13:00
config Config
2021-03-15 00:57:19 +13:00
entries map[string]MemberEntry
mu sync.Mutex
}
func (manager *MemberManagerCtx) Connect() error {
var err error
if manager.config.AdminPassword != "" {
// create default admin account at startup
_, err = manager.Insert("admin", manager.config.AdminPassword, types.MemberProfile{
Name: "Administrator",
IsAdmin: true,
CanLogin: true,
CanConnect: true,
CanWatch: true,
CanHost: true,
CanAccessClipboard: true,
})
}
if manager.config.UserPassword != "" {
// create default user account at startup
_, err = manager.Insert("user", manager.config.UserPassword, types.MemberProfile{
Name: "User",
IsAdmin: false,
CanLogin: true,
CanConnect: true,
CanWatch: true,
CanHost: true,
CanAccessClipboard: true,
})
}
return err
2021-03-15 00:57:19 +13:00
}
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, ok := manager.entries[id]
if !ok {
return "", types.MemberProfile{}, fmt.Errorf("Member ID does not exist.")
}
// 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
entry, ok := manager.entries[id]
if ok {
return "", fmt.Errorf("Member ID already exists.")
}
// TODO: Use hash function.
entry.Password = password
entry.Profile = profile
manager.entries[id] = entry
return id, nil
}
func (manager *MemberManagerCtx) Update(id string, profile types.MemberProfile) error {
manager.mu.Lock()
defer manager.mu.Unlock()
entry, ok := manager.entries[id]
if !ok {
return fmt.Errorf("Member ID does not exist.")
}
entry.Profile = profile
manager.entries[id] = entry
return nil
}
func (manager *MemberManagerCtx) UpdatePassword(id string, password string) error {
manager.mu.Lock()
defer manager.mu.Unlock()
entry, ok := manager.entries[id]
if !ok {
return fmt.Errorf("Member ID does not exist.")
}
// TODO: Use hash function.
entry.Password = password
manager.entries[id] = entry
return nil
}
func (manager *MemberManagerCtx) Select(id string) (types.MemberProfile, error) {
manager.mu.Lock()
defer manager.mu.Unlock()
entry, ok := manager.entries[id]
if ok {
return types.MemberProfile{}, fmt.Errorf("Member ID already exists.")
}
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{}
i := 0
for id, entry := range manager.entries {
2021-03-15 01:07:03 +13:00
if i < offset || i > offset+limit {
2021-03-15 00:57:19 +13:00
continue
}
profiles[id] = entry.Profile
i = i + 1
}
return profiles, nil
}
func (manager *MemberManagerCtx) Delete(id string) error {
manager.mu.Lock()
defer manager.mu.Unlock()
_, ok := manager.entries[id]
if !ok {
return fmt.Errorf("Member ID does not exist.")
}
delete(manager.entries, id)
return nil
}