From b24c217509a9e2eec596811549d38e1b823534de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Sat, 5 Dec 2020 23:31:41 +0100 Subject: [PATCH] members database import. --- internal/session/database/manager.go | 5 ++--- internal/session/manager.go | 23 ++++++++++++++++++----- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/internal/session/database/manager.go b/internal/session/database/manager.go index f26701b1..e066ace6 100644 --- a/internal/session/database/manager.go +++ b/internal/session/database/manager.go @@ -58,10 +58,9 @@ func (manager *MembersDatabaseCtx) Delete(id string) error { return nil } -func (manager *MembersDatabaseCtx) Select(id string) (types.MemberProfile, bool) { +func (manager *MembersDatabaseCtx) Select() map[string]types.MemberProfile { manager.mu.Lock() defer manager.mu.Unlock() - profile, ok := manager.profiles[id] - return profile, ok + return manager.profiles } diff --git a/internal/session/manager.go b/internal/session/manager.go index ea6efbda..a0c99053 100644 --- a/internal/session/manager.go +++ b/internal/session/manager.go @@ -26,17 +26,20 @@ func New(config *config.Session) *SessionManagerCtx { emmiter: events.New(), } - // TODO: Import from Database at startup. + // import database users + for id, profile := range manager.database.Select() { + _ = manager.add(id, profile) + } // create default admin account at startup - _, _ = manager.Create("admin", types.MemberProfile{ + _ = manager.add("admin", types.MemberProfile{ Secret: config.AdminPassword, Name: "Administrator", IsAdmin: true, }) // create default user account at startup - _, _ = manager.Create("user", types.MemberProfile{ + _ = manager.add("user", types.MemberProfile{ Secret: config.Password, Name: "User", IsAdmin: false, @@ -56,7 +59,8 @@ type SessionManagerCtx struct { emmiter events.EventEmmiter } -func (manager *SessionManagerCtx) Create(id string, profile types.MemberProfile) (types.Session, error) { +// TODO: Extract members map + primitives. +func (manager *SessionManagerCtx) add(id string, profile types.MemberProfile) types.Session { session := &SessionCtx{ id: id, manager: manager, @@ -64,6 +68,15 @@ func (manager *SessionManagerCtx) Create(id string, profile types.MemberProfile) profile: profile, } + manager.members[id] = session + return session +} + +// --- +// members +// --- + +func (manager *SessionManagerCtx) Create(id string, profile types.MemberProfile) (types.Session, error) { manager.membersMu.Lock() _, ok := manager.members[id] @@ -78,7 +91,7 @@ func (manager *SessionManagerCtx) Create(id string, profile types.MemberProfile) return nil, err } - manager.members[id] = session + session := manager.add(id, profile) manager.membersMu.Unlock() manager.emmiter.Emit("created", session)