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"
)
func New() types.MemberManager {
return &MemberManagerCtx{}
func New() types.MemberProvider {
return &MemberProviderCtx{}
}
type MemberManagerCtx struct{}
type MemberProviderCtx struct{}
func (manager *MemberManagerCtx) Connect() error {
func (provider *MemberProviderCtx) Connect() error {
return nil
}
func (manager *MemberManagerCtx) Disconnect() error {
func (provider *MemberProviderCtx) Disconnect() error {
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{
Name: username,
IsAdmin: true,
@ -32,26 +32,26 @@ func (manager *MemberManagerCtx) Authenticate(username string, password string)
}, 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.")
}
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.")
}
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.")
}
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.")
}
func (manager *MemberManagerCtx) UpdatePassword(id string, password string) error {
func (provider *MemberProviderCtx) UpdatePassword(id string, password string) error {
return fmt.Errorf("Not implemented.")
}
func (manager *MemberManagerCtx) Delete(id string) error {
func (provider *MemberProviderCtx) Delete(id string) error {
return fmt.Errorf("Not implemented.")
}

View File

@ -10,34 +10,34 @@ import (
"demodesk/neko/internal/types"
)
func New(config Config) types.MemberManager {
return &MemberManagerCtx{
func New(config Config) types.MemberProvider {
return &MemberProviderCtx{
config: config,
mu: sync.Mutex{},
}
}
type MemberManagerCtx struct {
type MemberProviderCtx struct {
config Config
mu sync.Mutex
}
func (manager *MemberManagerCtx) Connect() error {
func (provider *MemberProviderCtx) Connect() error {
return nil
}
func (manager *MemberManagerCtx) Disconnect() error {
func (provider *MemberProviderCtx) Disconnect() error {
return nil
}
func (manager *MemberManagerCtx) Authenticate(username string, password string) (string, types.MemberProfile, error) {
manager.mu.Lock()
defer manager.mu.Unlock()
func (provider *MemberProviderCtx) Authenticate(username string, password string) (string, types.MemberProfile, error) {
provider.mu.Lock()
defer provider.mu.Unlock()
// id will be also username
id := username
entry, err := manager.getEntry(id)
entry, err := provider.getEntry(id)
if err != nil {
return "", types.MemberProfile{}, err
}
@ -50,14 +50,14 @@ func (manager *MemberManagerCtx) Authenticate(username string, password string)
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()
func (provider *MemberProviderCtx) Insert(username string, password string, profile types.MemberProfile) (string, error) {
provider.mu.Lock()
defer provider.mu.Unlock()
// id will be also username
id := username
entries, err := manager.deserialize()
entries, err := provider.deserialize()
if err != nil {
return "", err
}
@ -73,14 +73,14 @@ func (manager *MemberManagerCtx) Insert(username string, password string, profil
Profile: profile,
}
return id, manager.serialize(entries)
return id, provider.serialize(entries)
}
func (manager *MemberManagerCtx) UpdateProfile(id string, profile types.MemberProfile) error {
manager.mu.Lock()
defer manager.mu.Unlock()
func (provider *MemberProviderCtx) UpdateProfile(id string, profile types.MemberProfile) error {
provider.mu.Lock()
defer provider.mu.Unlock()
entries, err := manager.deserialize()
entries, err := provider.deserialize()
if err != nil {
return err
}
@ -93,14 +93,14 @@ func (manager *MemberManagerCtx) UpdateProfile(id string, profile types.MemberPr
entry.Profile = profile
entries[id] = entry
return manager.serialize(entries)
return provider.serialize(entries)
}
func (manager *MemberManagerCtx) UpdatePassword(id string, password string) error {
manager.mu.Lock()
defer manager.mu.Unlock()
func (provider *MemberProviderCtx) UpdatePassword(id string, password string) error {
provider.mu.Lock()
defer provider.mu.Unlock()
entries, err := manager.deserialize()
entries, err := provider.deserialize()
if err != nil {
return err
}
@ -114,14 +114,14 @@ func (manager *MemberManagerCtx) UpdatePassword(id string, password string) erro
entry.Password = password
entries[id] = entry
return manager.serialize(entries)
return provider.serialize(entries)
}
func (manager *MemberManagerCtx) Select(id string) (types.MemberProfile, error) {
manager.mu.Lock()
defer manager.mu.Unlock()
func (provider *MemberProviderCtx) Select(id string) (types.MemberProfile, error) {
provider.mu.Lock()
defer provider.mu.Unlock()
entry, err := manager.getEntry(id)
entry, err := provider.getEntry(id)
if err != nil {
return types.MemberProfile{}, err
}
@ -129,13 +129,13 @@ func (manager *MemberManagerCtx) Select(id string) (types.MemberProfile, error)
return entry.Profile, nil
}
func (manager *MemberManagerCtx) SelectAll(limit int, offset int) (map[string]types.MemberProfile, error) {
manager.mu.Lock()
defer manager.mu.Unlock()
func (provider *MemberProviderCtx) SelectAll(limit int, offset int) (map[string]types.MemberProfile, error) {
provider.mu.Lock()
defer provider.mu.Unlock()
profiles := map[string]types.MemberProfile{}
entries, err := manager.deserialize()
entries, err := provider.deserialize()
if err != nil {
return profiles, err
}
@ -152,11 +152,11 @@ func (manager *MemberManagerCtx) SelectAll(limit int, offset int) (map[string]ty
return profiles, nil
}
func (manager *MemberManagerCtx) Delete(id string) error {
manager.mu.Lock()
defer manager.mu.Unlock()
func (provider *MemberProviderCtx) Delete(id string) error {
provider.mu.Lock()
defer provider.mu.Unlock()
entries, err := manager.deserialize()
entries, err := provider.deserialize()
if err != nil {
return err
}
@ -168,11 +168,11 @@ func (manager *MemberManagerCtx) Delete(id string) error {
delete(entries, id)
return manager.serialize(entries)
return provider.serialize(entries)
}
func (manager *MemberManagerCtx) deserialize() (map[string]MemberEntry, error) {
file, err := os.OpenFile(manager.config.File, os.O_RDONLY|os.O_CREATE, os.ModePerm)
func (provider *MemberProviderCtx) deserialize() (map[string]MemberEntry, error) {
file, err := os.OpenFile(provider.config.File, os.O_RDONLY|os.O_CREATE, os.ModePerm)
if err != nil {
return nil, err
}
@ -194,8 +194,8 @@ func (manager *MemberManagerCtx) deserialize() (map[string]MemberEntry, error) {
return entries, nil
}
func (manager *MemberManagerCtx) getEntry(id string) (MemberEntry, error) {
entries, err := manager.deserialize()
func (provider *MemberProviderCtx) getEntry(id string) (MemberEntry, error) {
entries, err := provider.deserialize()
if err != nil {
return MemberEntry{}, err
}
@ -208,11 +208,11 @@ func (manager *MemberManagerCtx) getEntry(id string) (MemberEntry, error) {
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)
if err != nil {
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
import (
"sync"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"demodesk/neko/internal/config"
"demodesk/neko/internal/member/dummy"
"demodesk/neko/internal/member/file"
@ -8,20 +13,70 @@ import (
"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 {
case "file":
return file.New(file.Config{
manager.provider = file.New(file.Config{
File: config.FilePath,
})
case "object":
return object.New(object.Config{
manager.provider = object.New(object.Config{
AdminPassword: config.AdminPassword,
UserPassword: config.Password,
})
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"
)
func New(config Config) types.MemberManager {
return &MemberManagerCtx{
func New(config Config) types.MemberProvider {
return &MemberProviderCtx{
config: config,
entries: make(map[string]*MemberEntry),
mu: sync.Mutex{},
}
}
type MemberManagerCtx struct {
type MemberProviderCtx struct {
config Config
entries map[string]*MemberEntry
mu sync.Mutex
}
func (manager *MemberManagerCtx) Connect() error {
func (provider *MemberProviderCtx) Connect() error {
var err error
if manager.config.AdminPassword != "" {
if provider.config.AdminPassword != "" {
// 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",
IsAdmin: 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
_, err = manager.Insert("user", manager.config.UserPassword, types.MemberProfile{
_, err = provider.Insert("user", provider.config.UserPassword, types.MemberProfile{
Name: "User",
IsAdmin: false,
CanLogin: true,
@ -53,18 +53,18 @@ func (manager *MemberManagerCtx) Connect() error {
return err
}
func (manager *MemberManagerCtx) Disconnect() error {
func (provider *MemberProviderCtx) Disconnect() error {
return nil
}
func (manager *MemberManagerCtx) Authenticate(username string, password string) (string, types.MemberProfile, error) {
manager.mu.Lock()
defer manager.mu.Unlock()
func (provider *MemberProviderCtx) Authenticate(username string, password string) (string, types.MemberProfile, error) {
provider.mu.Lock()
defer provider.mu.Unlock()
// id will be also username
id := username
entry, ok := manager.entries[id]
entry, ok := provider.entries[id]
if !ok {
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
}
func (manager *MemberManagerCtx) Insert(username string, password string, profile types.MemberProfile) (string, error) {
manager.mu.Lock()
defer manager.mu.Unlock()
func (provider *MemberProviderCtx) Insert(username string, password string, profile types.MemberProfile) (string, error) {
provider.mu.Lock()
defer provider.mu.Unlock()
// id will be also username
id := username
_, ok := manager.entries[id]
_, ok := provider.entries[id]
if ok {
return "", fmt.Errorf("Member ID already exists.")
}
manager.entries[id] = &MemberEntry{
provider.entries[id] = &MemberEntry{
// TODO: Use hash function.
Password: password,
Profile: profile,
@ -98,11 +98,11 @@ func (manager *MemberManagerCtx) Insert(username string, password string, profil
return id, nil
}
func (manager *MemberManagerCtx) UpdateProfile(id string, profile types.MemberProfile) error {
manager.mu.Lock()
defer manager.mu.Unlock()
func (provider *MemberProviderCtx) UpdateProfile(id string, profile types.MemberProfile) error {
provider.mu.Lock()
defer provider.mu.Unlock()
entry, ok := manager.entries[id]
entry, ok := provider.entries[id]
if !ok {
return fmt.Errorf("Member ID does not exist.")
}
@ -112,11 +112,11 @@ func (manager *MemberManagerCtx) UpdateProfile(id string, profile types.MemberPr
return nil
}
func (manager *MemberManagerCtx) UpdatePassword(id string, password string) error {
manager.mu.Lock()
defer manager.mu.Unlock()
func (provider *MemberProviderCtx) UpdatePassword(id string, password string) error {
provider.mu.Lock()
defer provider.mu.Unlock()
entry, ok := manager.entries[id]
entry, ok := provider.entries[id]
if !ok {
return fmt.Errorf("Member ID does not exist.")
}
@ -127,11 +127,11 @@ func (manager *MemberManagerCtx) UpdatePassword(id string, password string) erro
return nil
}
func (manager *MemberManagerCtx) Select(id string) (types.MemberProfile, error) {
manager.mu.Lock()
defer manager.mu.Unlock()
func (provider *MemberProviderCtx) Select(id string) (types.MemberProfile, error) {
provider.mu.Lock()
defer provider.mu.Unlock()
entry, ok := manager.entries[id]
entry, ok := provider.entries[id]
if !ok {
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
}
func (manager *MemberManagerCtx) SelectAll(limit int, offset int) (map[string]types.MemberProfile, error) {
manager.mu.Lock()
defer manager.mu.Unlock()
func (provider *MemberProviderCtx) SelectAll(limit int, offset int) (map[string]types.MemberProfile, error) {
provider.mu.Lock()
defer provider.mu.Unlock()
profiles := make(map[string]types.MemberProfile)
i := 0
for id, entry := range manager.entries {
for id, entry := range provider.entries {
if i >= offset && (limit == 0 || i < offset+limit) {
profiles[id] = entry.Profile
}
@ -157,16 +157,16 @@ func (manager *MemberManagerCtx) SelectAll(limit int, offset int) (map[string]ty
return profiles, nil
}
func (manager *MemberManagerCtx) Delete(id string) error {
manager.mu.Lock()
defer manager.mu.Unlock()
func (provider *MemberProviderCtx) Delete(id string) error {
provider.mu.Lock()
defer provider.mu.Unlock()
_, ok := manager.entries[id]
_, ok := provider.entries[id]
if !ok {
return fmt.Errorf("Member ID does not exist.")
}
delete(manager.entries, id)
delete(provider.entries, id)
return nil
}

View File

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

View File

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