neko/internal/member/manager.go

169 lines
4.1 KiB
Go
Raw Normal View History

2021-03-14 13:08:50 +13:00
package member
import (
2021-03-15 07:59:34 +13:00
"fmt"
2021-03-15 04:58:18 +13:00
"sync"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
2021-03-14 13:08:50 +13:00
"demodesk/neko/internal/config"
"demodesk/neko/internal/member/dummy"
"demodesk/neko/internal/member/file"
"demodesk/neko/internal/member/object"
"demodesk/neko/internal/types"
)
2021-03-15 07:59:34 +13:00
func New(sessions types.SessionManager, config *config.Member) *MemberManagerCtx {
2021-03-15 04:58:18 +13:00
manager := &MemberManagerCtx{
2021-03-15 07:59:34 +13:00
logger: log.With().Str("module", "member").Logger(),
sessions: sessions,
config: config,
2021-03-15 04:58:18 +13:00
}
2021-03-14 13:08:50 +13:00
switch config.Provider {
case "file":
2021-03-15 04:58:18 +13:00
manager.provider = file.New(file.Config{
2021-03-15 01:07:03 +13:00
File: config.FilePath,
})
2021-03-14 13:08:50 +13:00
case "object":
2021-03-15 04:58:18 +13:00
manager.provider = object.New(object.Config{
2021-03-15 01:07:03 +13:00
AdminPassword: config.AdminPassword,
UserPassword: config.Password,
})
2021-03-14 13:08:50 +13:00
case "dummy":
2021-03-15 04:58:18 +13:00
fallthrough
default:
manager.provider = dummy.New()
2021-03-14 13:08:50 +13:00
}
2021-03-15 04:58:18 +13:00
return manager
}
type MemberManagerCtx struct {
logger zerolog.Logger
sessions types.SessionManager
config *config.Member
providerMu sync.Mutex
provider types.MemberProvider
sessionMu sync.Mutex
2021-03-15 04:58:18 +13:00
}
func (manager *MemberManagerCtx) Connect() error {
manager.providerMu.Lock()
defer manager.providerMu.Unlock()
2021-03-15 07:36:30 +13:00
2021-03-15 04:58:18 +13:00
return manager.provider.Connect()
}
func (manager *MemberManagerCtx) Disconnect() error {
manager.providerMu.Lock()
defer manager.providerMu.Unlock()
2021-03-15 07:36:30 +13:00
2021-03-15 04:58:18 +13:00
return manager.provider.Disconnect()
}
func (manager *MemberManagerCtx) Authenticate(username string, password string) (string, types.MemberProfile, error) {
manager.providerMu.Lock()
defer manager.providerMu.Unlock()
2021-03-15 07:36:30 +13:00
2021-03-15 04:58:18 +13:00
return manager.provider.Authenticate(username, password)
}
func (manager *MemberManagerCtx) Insert(username string, password string, profile types.MemberProfile) (string, error) {
manager.providerMu.Lock()
defer manager.providerMu.Unlock()
2021-03-15 07:36:30 +13:00
2021-03-15 04:58:18 +13:00
return manager.provider.Insert(username, password, profile)
}
func (manager *MemberManagerCtx) Select(id string) (types.MemberProfile, error) {
manager.providerMu.Lock()
defer manager.providerMu.Unlock()
2021-03-15 07:36:30 +13:00
2021-03-15 04:58:18 +13:00
return manager.provider.Select(id)
}
func (manager *MemberManagerCtx) SelectAll(limit int, offset int) (map[string]types.MemberProfile, error) {
manager.providerMu.Lock()
defer manager.providerMu.Unlock()
2021-03-15 07:36:30 +13:00
2021-03-15 04:58:18 +13:00
return manager.provider.SelectAll(limit, offset)
}
func (manager *MemberManagerCtx) UpdateProfile(id string, profile types.MemberProfile) error {
manager.providerMu.Lock()
defer manager.providerMu.Unlock()
2021-03-15 07:36:30 +13:00
2021-03-15 07:59:34 +13:00
// update corresponding session, if exists
manager.sessionMu.Lock()
2021-03-15 07:59:34 +13:00
if _, ok := manager.sessions.Get(id); ok {
if err := manager.sessions.Update(id, profile); err != nil {
manager.logger.Err(err).Msg("error while updating session")
}
}
manager.sessionMu.Unlock()
2021-03-15 07:59:34 +13:00
2021-03-15 04:58:18 +13:00
return manager.provider.UpdateProfile(id, profile)
}
func (manager *MemberManagerCtx) UpdatePassword(id string, password string) error {
manager.providerMu.Lock()
defer manager.providerMu.Unlock()
2021-03-15 07:36:30 +13:00
2021-03-15 04:58:18 +13:00
return manager.provider.UpdatePassword(id, password)
}
2021-03-14 13:08:50 +13:00
2021-03-15 04:58:18 +13:00
func (manager *MemberManagerCtx) Delete(id string) error {
manager.providerMu.Lock()
defer manager.providerMu.Unlock()
2021-03-15 07:36:30 +13:00
2021-03-15 07:59:34 +13:00
// destroy corresponding session, if exists
manager.sessionMu.Lock()
2021-03-15 07:59:34 +13:00
if _, ok := manager.sessions.Get(id); ok {
if err := manager.sessions.Delete(id); err != nil {
manager.logger.Err(err).Msg("error while deleting session")
}
}
manager.sessionMu.Unlock()
2021-03-15 07:59:34 +13:00
2021-03-15 04:58:18 +13:00
return manager.provider.Delete(id)
2021-03-14 13:08:50 +13:00
}
2021-03-15 07:59:34 +13:00
//
// member -> session
//
func (manager *MemberManagerCtx) Login(username string, password string) (types.Session, string, error) {
manager.sessionMu.Lock()
defer manager.sessionMu.Unlock()
2021-03-15 07:59:34 +13:00
id, profile, err := manager.provider.Authenticate(username, password)
if err != nil {
return nil, "", err
}
session, ok := manager.sessions.Get(id)
if ok {
if session.State().IsConnected {
return nil, "", fmt.Errorf("session is already connected")
}
// delete existing session
manager.sessions.Delete(id)
}
2021-03-15 07:59:34 +13:00
return manager.sessions.Create(id, profile)
}
func (manager *MemberManagerCtx) Logout(id string) error {
manager.sessionMu.Lock()
defer manager.sessionMu.Unlock()
2021-03-15 07:59:34 +13:00
if _, ok := manager.sessions.Get(id); !ok {
return fmt.Errorf("session not found")
}
return manager.sessions.Delete(id)
}