neko/internal/member/object/manager.go

173 lines
3.6 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 02:44:03 +13:00
entries: make(map[string]*MemberEntry),
2021-03-15 00:57:19 +13:00
mu: sync.Mutex{},
}
}
type MemberManagerCtx struct {
2021-03-15 01:07:03 +13:00
config Config
2021-03-15 02:44:03 +13:00
entries map[string]*MemberEntry
2021-03-15 00:57:19 +13:00
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
2021-03-15 02:44:03 +13:00
_, ok := manager.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
manager.entries[id] = &MemberEntry{
// TODO: Use hash function.
Password: password,
Profile: profile,
}
2021-03-15 00:57:19 +13:00
return id, nil
}
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()
entry, ok := manager.entries[id]
if !ok {
return fmt.Errorf("Member ID does not exist.")
}
entry.Profile = profile
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
return nil
}
func (manager *MemberManagerCtx) Select(id string) (types.MemberProfile, error) {
manager.mu.Lock()
defer manager.mu.Unlock()
entry, ok := manager.entries[id]
2021-03-15 02:44:03 +13:00
if !ok {
return types.MemberProfile{}, fmt.Errorf("Member ID does not exist.")
2021-03-15 00:57:19 +13:00
}
return entry.Profile, nil
}
func (manager *MemberManagerCtx) SelectAll(limit int, offset int) (map[string]types.MemberProfile, error) {
manager.mu.Lock()
defer manager.mu.Unlock()
2021-03-15 02:44:03 +13:00
profiles := make(map[string]types.MemberProfile)
2021-03-15 00:57:19 +13:00
i := 0
for id, entry := range manager.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()
_, ok := manager.entries[id]
if !ok {
return fmt.Errorf("Member ID does not exist.")
}
delete(manager.entries, id)
return nil
}