split member manager and provider.

This commit is contained in:
Miroslav Šedivý 2021-03-14 16:58:18 +01:00
parent 19e732f3ea
commit 4207775fb3
6 changed files with 161 additions and 103 deletions

View File

@ -6,21 +6,21 @@ import (
"demodesk/neko/internal/types" "demodesk/neko/internal/types"
) )
func New() types.MemberManager { func New() types.MemberProvider {
return &MemberManagerCtx{} return &MemberProviderCtx{}
} }
type MemberManagerCtx struct{} type MemberProviderCtx struct{}
func (manager *MemberManagerCtx) Connect() error { func (provider *MemberProviderCtx) Connect() error {
return nil return nil
} }
func (manager *MemberManagerCtx) Disconnect() error { func (provider *MemberProviderCtx) Disconnect() error {
return nil return nil
} }
func (manager *MemberManagerCtx) Authenticate(username string, password string) (string, types.MemberProfile, error) { func (provider *MemberProviderCtx) Authenticate(username string, password string) (string, types.MemberProfile, error) {
return username, types.MemberProfile{ return username, types.MemberProfile{
Name: username, Name: username,
IsAdmin: true, IsAdmin: true,
@ -32,26 +32,26 @@ func (manager *MemberManagerCtx) Authenticate(username string, password string)
}, nil }, nil
} }
func (manager *MemberManagerCtx) Insert(username string, password string, profile types.MemberProfile) (string, error) { func (provider *MemberProviderCtx) Insert(username string, password string, profile types.MemberProfile) (string, error) {
return "", fmt.Errorf("Not implemented.") return "", fmt.Errorf("Not implemented.")
} }
func (manager *MemberManagerCtx) Select(id string) (types.MemberProfile, error) { func (provider *MemberProviderCtx) Select(id string) (types.MemberProfile, error) {
return types.MemberProfile{}, fmt.Errorf("Not implemented.") return types.MemberProfile{}, fmt.Errorf("Not implemented.")
} }
func (manager *MemberManagerCtx) SelectAll(limit int, offset int) (map[string]types.MemberProfile, error) { func (provider *MemberProviderCtx) SelectAll(limit int, offset int) (map[string]types.MemberProfile, error) {
return map[string]types.MemberProfile{}, fmt.Errorf("Not implemented.") return map[string]types.MemberProfile{}, fmt.Errorf("Not implemented.")
} }
func (manager *MemberManagerCtx) UpdateProfile(id string, profile types.MemberProfile) error { func (provider *MemberProviderCtx) UpdateProfile(id string, profile types.MemberProfile) error {
return fmt.Errorf("Not implemented.") return fmt.Errorf("Not implemented.")
} }
func (manager *MemberManagerCtx) UpdatePassword(id string, password string) error { func (provider *MemberProviderCtx) UpdatePassword(id string, password string) error {
return fmt.Errorf("Not implemented.") return fmt.Errorf("Not implemented.")
} }
func (manager *MemberManagerCtx) Delete(id string) error { func (provider *MemberProviderCtx) Delete(id string) error {
return fmt.Errorf("Not implemented.") return fmt.Errorf("Not implemented.")
} }

View File

@ -10,34 +10,34 @@ import (
"demodesk/neko/internal/types" "demodesk/neko/internal/types"
) )
func New(config Config) types.MemberManager { func New(config Config) types.MemberProvider {
return &MemberManagerCtx{ return &MemberProviderCtx{
config: config, config: config,
mu: sync.Mutex{}, mu: sync.Mutex{},
} }
} }
type MemberManagerCtx struct { type MemberProviderCtx struct {
config Config config Config
mu sync.Mutex mu sync.Mutex
} }
func (manager *MemberManagerCtx) Connect() error { func (provider *MemberProviderCtx) Connect() error {
return nil return nil
} }
func (manager *MemberManagerCtx) Disconnect() error { func (provider *MemberProviderCtx) Disconnect() error {
return nil return nil
} }
func (manager *MemberManagerCtx) Authenticate(username string, password string) (string, types.MemberProfile, error) { func (provider *MemberProviderCtx) Authenticate(username string, password string) (string, types.MemberProfile, error) {
manager.mu.Lock() provider.mu.Lock()
defer manager.mu.Unlock() defer provider.mu.Unlock()
// id will be also username // id will be also username
id := username id := username
entry, err := manager.getEntry(id) entry, err := provider.getEntry(id)
if err != nil { if err != nil {
return "", types.MemberProfile{}, err return "", types.MemberProfile{}, err
} }
@ -50,14 +50,14 @@ func (manager *MemberManagerCtx) Authenticate(username string, password string)
return id, entry.Profile, nil return id, entry.Profile, nil
} }
func (manager *MemberManagerCtx) Insert(username string, password string, profile types.MemberProfile) (string, error) { func (provider *MemberProviderCtx) Insert(username string, password string, profile types.MemberProfile) (string, error) {
manager.mu.Lock() provider.mu.Lock()
defer manager.mu.Unlock() defer provider.mu.Unlock()
// id will be also username // id will be also username
id := username id := username
entries, err := manager.deserialize() entries, err := provider.deserialize()
if err != nil { if err != nil {
return "", err return "", err
} }
@ -73,14 +73,14 @@ func (manager *MemberManagerCtx) Insert(username string, password string, profil
Profile: profile, Profile: profile,
} }
return id, manager.serialize(entries) return id, provider.serialize(entries)
} }
func (manager *MemberManagerCtx) UpdateProfile(id string, profile types.MemberProfile) error { func (provider *MemberProviderCtx) UpdateProfile(id string, profile types.MemberProfile) error {
manager.mu.Lock() provider.mu.Lock()
defer manager.mu.Unlock() defer provider.mu.Unlock()
entries, err := manager.deserialize() entries, err := provider.deserialize()
if err != nil { if err != nil {
return err return err
} }
@ -93,14 +93,14 @@ func (manager *MemberManagerCtx) UpdateProfile(id string, profile types.MemberPr
entry.Profile = profile entry.Profile = profile
entries[id] = entry entries[id] = entry
return manager.serialize(entries) return provider.serialize(entries)
} }
func (manager *MemberManagerCtx) UpdatePassword(id string, password string) error { func (provider *MemberProviderCtx) UpdatePassword(id string, password string) error {
manager.mu.Lock() provider.mu.Lock()
defer manager.mu.Unlock() defer provider.mu.Unlock()
entries, err := manager.deserialize() entries, err := provider.deserialize()
if err != nil { if err != nil {
return err return err
} }
@ -114,14 +114,14 @@ func (manager *MemberManagerCtx) UpdatePassword(id string, password string) erro
entry.Password = password entry.Password = password
entries[id] = entry entries[id] = entry
return manager.serialize(entries) return provider.serialize(entries)
} }
func (manager *MemberManagerCtx) Select(id string) (types.MemberProfile, error) { func (provider *MemberProviderCtx) Select(id string) (types.MemberProfile, error) {
manager.mu.Lock() provider.mu.Lock()
defer manager.mu.Unlock() defer provider.mu.Unlock()
entry, err := manager.getEntry(id) entry, err := provider.getEntry(id)
if err != nil { if err != nil {
return types.MemberProfile{}, err return types.MemberProfile{}, err
} }
@ -129,13 +129,13 @@ func (manager *MemberManagerCtx) Select(id string) (types.MemberProfile, error)
return entry.Profile, nil return entry.Profile, nil
} }
func (manager *MemberManagerCtx) SelectAll(limit int, offset int) (map[string]types.MemberProfile, error) { func (provider *MemberProviderCtx) SelectAll(limit int, offset int) (map[string]types.MemberProfile, error) {
manager.mu.Lock() provider.mu.Lock()
defer manager.mu.Unlock() defer provider.mu.Unlock()
profiles := map[string]types.MemberProfile{} profiles := map[string]types.MemberProfile{}
entries, err := manager.deserialize() entries, err := provider.deserialize()
if err != nil { if err != nil {
return profiles, err return profiles, err
} }
@ -152,11 +152,11 @@ func (manager *MemberManagerCtx) SelectAll(limit int, offset int) (map[string]ty
return profiles, nil return profiles, nil
} }
func (manager *MemberManagerCtx) Delete(id string) error { func (provider *MemberProviderCtx) Delete(id string) error {
manager.mu.Lock() provider.mu.Lock()
defer manager.mu.Unlock() defer provider.mu.Unlock()
entries, err := manager.deserialize() entries, err := provider.deserialize()
if err != nil { if err != nil {
return err return err
} }
@ -168,11 +168,11 @@ func (manager *MemberManagerCtx) Delete(id string) error {
delete(entries, id) delete(entries, id)
return manager.serialize(entries) return provider.serialize(entries)
} }
func (manager *MemberManagerCtx) deserialize() (map[string]MemberEntry, error) { func (provider *MemberProviderCtx) deserialize() (map[string]MemberEntry, error) {
file, err := os.OpenFile(manager.config.File, os.O_RDONLY|os.O_CREATE, os.ModePerm) file, err := os.OpenFile(provider.config.File, os.O_RDONLY|os.O_CREATE, os.ModePerm)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -194,8 +194,8 @@ func (manager *MemberManagerCtx) deserialize() (map[string]MemberEntry, error) {
return entries, nil return entries, nil
} }
func (manager *MemberManagerCtx) getEntry(id string) (MemberEntry, error) { func (provider *MemberProviderCtx) getEntry(id string) (MemberEntry, error) {
entries, err := manager.deserialize() entries, err := provider.deserialize()
if err != nil { if err != nil {
return MemberEntry{}, err return MemberEntry{}, err
} }
@ -208,11 +208,11 @@ func (manager *MemberManagerCtx) getEntry(id string) (MemberEntry, error) {
return entry, nil return entry, nil
} }
func (manager *MemberManagerCtx) serialize(data map[string]MemberEntry) error { func (provider *MemberProviderCtx) serialize(data map[string]MemberEntry) error {
raw, err := json.Marshal(data) raw, err := json.Marshal(data)
if err != nil { if err != nil {
return err return err
} }
return ioutil.WriteFile(manager.config.File, raw, os.ModePerm) return ioutil.WriteFile(provider.config.File, raw, os.ModePerm)
} }

View File

@ -1,6 +1,11 @@
package member package member
import ( import (
"sync"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"demodesk/neko/internal/config" "demodesk/neko/internal/config"
"demodesk/neko/internal/member/dummy" "demodesk/neko/internal/member/dummy"
"demodesk/neko/internal/member/file" "demodesk/neko/internal/member/file"
@ -8,20 +13,70 @@ import (
"demodesk/neko/internal/types" "demodesk/neko/internal/types"
) )
func New(config *config.Member) types.MemberManager { func New(config *config.Member) *MemberManagerCtx {
manager := &MemberManagerCtx{
logger: log.With().Str("module", "member").Logger(),
config: config,
}
switch config.Provider { switch config.Provider {
case "file": case "file":
return file.New(file.Config{ manager.provider = file.New(file.Config{
File: config.FilePath, File: config.FilePath,
}) })
case "object": case "object":
return object.New(object.Config{ manager.provider = object.New(object.Config{
AdminPassword: config.AdminPassword, AdminPassword: config.AdminPassword,
UserPassword: config.Password, UserPassword: config.Password,
}) })
case "dummy": case "dummy":
return dummy.New() fallthrough
default:
manager.provider = dummy.New()
} }
return dummy.New() return manager
}
type MemberManagerCtx struct {
logger zerolog.Logger
config *config.Member
mu sync.Mutex
provider types.MemberProvider
}
func (manager *MemberManagerCtx) Connect() error {
return manager.provider.Connect()
}
func (manager *MemberManagerCtx) Disconnect() error {
return manager.provider.Disconnect()
}
func (manager *MemberManagerCtx) Authenticate(username string, password string) (string, types.MemberProfile, error) {
return manager.provider.Authenticate(username, password)
}
func (manager *MemberManagerCtx) Insert(username string, password string, profile types.MemberProfile) (string, error) {
return manager.provider.Insert(username, password, profile)
}
func (manager *MemberManagerCtx) Select(id string) (types.MemberProfile, error) {
return manager.provider.Select(id)
}
func (manager *MemberManagerCtx) SelectAll(limit int, offset int) (map[string]types.MemberProfile, error) {
return manager.provider.SelectAll(limit, offset)
}
func (manager *MemberManagerCtx) UpdateProfile(id string, profile types.MemberProfile) error {
return manager.provider.UpdateProfile(id, profile)
}
func (manager *MemberManagerCtx) UpdatePassword(id string, password string) error {
return manager.provider.UpdatePassword(id, password)
}
func (manager *MemberManagerCtx) Delete(id string) error {
return manager.provider.Delete(id)
} }

View File

@ -7,26 +7,26 @@ import (
"demodesk/neko/internal/types" "demodesk/neko/internal/types"
) )
func New(config Config) types.MemberManager { func New(config Config) types.MemberProvider {
return &MemberManagerCtx{ return &MemberProviderCtx{
config: config, config: config,
entries: make(map[string]*MemberEntry), entries: make(map[string]*MemberEntry),
mu: sync.Mutex{}, mu: sync.Mutex{},
} }
} }
type MemberManagerCtx struct { type MemberProviderCtx struct {
config Config config Config
entries map[string]*MemberEntry entries map[string]*MemberEntry
mu sync.Mutex mu sync.Mutex
} }
func (manager *MemberManagerCtx) Connect() error { func (provider *MemberProviderCtx) Connect() error {
var err error var err error
if manager.config.AdminPassword != "" { if provider.config.AdminPassword != "" {
// create default admin account at startup // create default admin account at startup
_, err = manager.Insert("admin", manager.config.AdminPassword, types.MemberProfile{ _, err = provider.Insert("admin", provider.config.AdminPassword, types.MemberProfile{
Name: "Administrator", Name: "Administrator",
IsAdmin: true, IsAdmin: true,
CanLogin: true, CanLogin: true,
@ -37,9 +37,9 @@ func (manager *MemberManagerCtx) Connect() error {
}) })
} }
if manager.config.UserPassword != "" { if provider.config.UserPassword != "" {
// create default user account at startup // create default user account at startup
_, err = manager.Insert("user", manager.config.UserPassword, types.MemberProfile{ _, err = provider.Insert("user", provider.config.UserPassword, types.MemberProfile{
Name: "User", Name: "User",
IsAdmin: false, IsAdmin: false,
CanLogin: true, CanLogin: true,
@ -53,18 +53,18 @@ func (manager *MemberManagerCtx) Connect() error {
return err return err
} }
func (manager *MemberManagerCtx) Disconnect() error { func (provider *MemberProviderCtx) Disconnect() error {
return nil return nil
} }
func (manager *MemberManagerCtx) Authenticate(username string, password string) (string, types.MemberProfile, error) { func (provider *MemberProviderCtx) Authenticate(username string, password string) (string, types.MemberProfile, error) {
manager.mu.Lock() provider.mu.Lock()
defer manager.mu.Unlock() defer provider.mu.Unlock()
// id will be also username // id will be also username
id := username id := username
entry, ok := manager.entries[id] entry, ok := provider.entries[id]
if !ok { if !ok {
return "", types.MemberProfile{}, fmt.Errorf("Member ID does not exist.") return "", types.MemberProfile{}, fmt.Errorf("Member ID does not exist.")
} }
@ -77,19 +77,19 @@ func (manager *MemberManagerCtx) Authenticate(username string, password string)
return id, entry.Profile, nil return id, entry.Profile, nil
} }
func (manager *MemberManagerCtx) Insert(username string, password string, profile types.MemberProfile) (string, error) { func (provider *MemberProviderCtx) Insert(username string, password string, profile types.MemberProfile) (string, error) {
manager.mu.Lock() provider.mu.Lock()
defer manager.mu.Unlock() defer provider.mu.Unlock()
// id will be also username // id will be also username
id := username id := username
_, ok := manager.entries[id] _, ok := provider.entries[id]
if ok { if ok {
return "", fmt.Errorf("Member ID already exists.") return "", fmt.Errorf("Member ID already exists.")
} }
manager.entries[id] = &MemberEntry{ provider.entries[id] = &MemberEntry{
// TODO: Use hash function. // TODO: Use hash function.
Password: password, Password: password,
Profile: profile, Profile: profile,
@ -98,11 +98,11 @@ func (manager *MemberManagerCtx) Insert(username string, password string, profil
return id, nil return id, nil
} }
func (manager *MemberManagerCtx) UpdateProfile(id string, profile types.MemberProfile) error { func (provider *MemberProviderCtx) UpdateProfile(id string, profile types.MemberProfile) error {
manager.mu.Lock() provider.mu.Lock()
defer manager.mu.Unlock() defer provider.mu.Unlock()
entry, ok := manager.entries[id] entry, ok := provider.entries[id]
if !ok { if !ok {
return fmt.Errorf("Member ID does not exist.") return fmt.Errorf("Member ID does not exist.")
} }
@ -112,11 +112,11 @@ func (manager *MemberManagerCtx) UpdateProfile(id string, profile types.MemberPr
return nil return nil
} }
func (manager *MemberManagerCtx) UpdatePassword(id string, password string) error { func (provider *MemberProviderCtx) UpdatePassword(id string, password string) error {
manager.mu.Lock() provider.mu.Lock()
defer manager.mu.Unlock() defer provider.mu.Unlock()
entry, ok := manager.entries[id] entry, ok := provider.entries[id]
if !ok { if !ok {
return fmt.Errorf("Member ID does not exist.") return fmt.Errorf("Member ID does not exist.")
} }
@ -127,11 +127,11 @@ func (manager *MemberManagerCtx) UpdatePassword(id string, password string) erro
return nil return nil
} }
func (manager *MemberManagerCtx) Select(id string) (types.MemberProfile, error) { func (provider *MemberProviderCtx) Select(id string) (types.MemberProfile, error) {
manager.mu.Lock() provider.mu.Lock()
defer manager.mu.Unlock() defer provider.mu.Unlock()
entry, ok := manager.entries[id] entry, ok := provider.entries[id]
if !ok { if !ok {
return types.MemberProfile{}, fmt.Errorf("Member ID does not exist.") return types.MemberProfile{}, fmt.Errorf("Member ID does not exist.")
} }
@ -139,14 +139,14 @@ func (manager *MemberManagerCtx) Select(id string) (types.MemberProfile, error)
return entry.Profile, nil return entry.Profile, nil
} }
func (manager *MemberManagerCtx) SelectAll(limit int, offset int) (map[string]types.MemberProfile, error) { func (provider *MemberProviderCtx) SelectAll(limit int, offset int) (map[string]types.MemberProfile, error) {
manager.mu.Lock() provider.mu.Lock()
defer manager.mu.Unlock() defer provider.mu.Unlock()
profiles := make(map[string]types.MemberProfile) profiles := make(map[string]types.MemberProfile)
i := 0 i := 0
for id, entry := range manager.entries { for id, entry := range provider.entries {
if i >= offset && (limit == 0 || i < offset+limit) { if i >= offset && (limit == 0 || i < offset+limit) {
profiles[id] = entry.Profile profiles[id] = entry.Profile
} }
@ -157,16 +157,16 @@ func (manager *MemberManagerCtx) SelectAll(limit int, offset int) (map[string]ty
return profiles, nil return profiles, nil
} }
func (manager *MemberManagerCtx) Delete(id string) error { func (provider *MemberProviderCtx) Delete(id string) error {
manager.mu.Lock() provider.mu.Lock()
defer manager.mu.Unlock() defer provider.mu.Unlock()
_, ok := manager.entries[id] _, ok := provider.entries[id]
if !ok { if !ok {
return fmt.Errorf("Member ID does not exist.") return fmt.Errorf("Member ID does not exist.")
} }
delete(manager.entries, id) delete(provider.entries, id)
return nil return nil
} }

View File

@ -10,7 +10,7 @@ type MemberProfile struct {
CanAccessClipboard bool `json:"can_access_clipboard"` CanAccessClipboard bool `json:"can_access_clipboard"`
} }
type MemberManager interface { type MemberProvider interface {
Connect() error Connect() error
Disconnect() error Disconnect() error
@ -23,3 +23,7 @@ type MemberManager interface {
UpdatePassword(id string, password string) error UpdatePassword(id string, password string) error
Delete(id string) error Delete(id string) error
} }
type MemberManager interface {
MemberProvider
}

View File

@ -13,7 +13,6 @@ import (
"demodesk/neko/internal/http" "demodesk/neko/internal/http"
"demodesk/neko/internal/member" "demodesk/neko/internal/member"
"demodesk/neko/internal/session" "demodesk/neko/internal/session"
"demodesk/neko/internal/types"
"demodesk/neko/internal/webrtc" "demodesk/neko/internal/webrtc"
"demodesk/neko/internal/websocket" "demodesk/neko/internal/websocket"
"demodesk/neko/modules" "demodesk/neko/modules"
@ -122,7 +121,7 @@ type Neko struct {
desktopManager *desktop.DesktopManagerCtx desktopManager *desktop.DesktopManagerCtx
captureManager *capture.CaptureManagerCtx captureManager *capture.CaptureManagerCtx
webRTCManager *webrtc.WebRTCManagerCtx webRTCManager *webrtc.WebRTCManagerCtx
memberManager types.MemberManager memberManager *member.MemberManagerCtx
sessionManager *session.SessionManagerCtx sessionManager *session.SessionManagerCtx
webSocketManager *websocket.WebSocketManagerCtx webSocketManager *websocket.WebSocketManagerCtx
apiManager *api.ApiManagerCtx apiManager *api.ApiManagerCtx