From 76992df3f1a23ec0f01a00308a7d9a5297b6bf47 Mon Sep 17 00:00:00 2001 From: lash Date: Sat, 11 Jan 2025 22:26:47 +0000 Subject: [PATCH] Make admin store local --- cmd/africastalking/main.go | 2 +- devtools/admin/admin_numbers.json | 7 ++++ devtools/admin/commands/seed.go | 47 ++++++++++++++++++++++ devtools/admin/main.go | 17 ++++++++ go.mod | 4 +- go.sum | 8 ++-- handlers/application/menuhandler.go | 5 ++- handlers/application/menuhandler_test.go | 12 +++--- handlers/local.go | 6 +-- store/adminstore.go | 51 ++++++++++++++++++++++++ 10 files changed, 141 insertions(+), 18 deletions(-) create mode 100644 devtools/admin/admin_numbers.json create mode 100644 devtools/admin/commands/seed.go create mode 100644 devtools/admin/main.go create mode 100644 store/adminstore.go diff --git a/cmd/africastalking/main.go b/cmd/africastalking/main.go index c43f98f..1b43600 100644 --- a/cmd/africastalking/main.go +++ b/cmd/africastalking/main.go @@ -22,7 +22,7 @@ import ( "git.grassecon.net/grassrootseconomics/visedriver/session" at "git.grassecon.net/grassrootseconomics/visedriver-africastalking/africastalking" - "git.grassecon.net/grassrootseconomics/visedriver-africastalking/args" + "git.grassecon.net/grassrootseconomics/sarafu-vise/args" httpremote "git.grassecon.net/grassrootseconomics/sarafu-api/remote/http" "git.grassecon.net/grassrootseconomics/sarafu-vise/handlers" diff --git a/devtools/admin/admin_numbers.json b/devtools/admin/admin_numbers.json new file mode 100644 index 0000000..ca58a23 --- /dev/null +++ b/devtools/admin/admin_numbers.json @@ -0,0 +1,7 @@ +{ + "admins": [ + { + "phonenumber" : "" + } + ] +} \ No newline at end of file diff --git a/devtools/admin/commands/seed.go b/devtools/admin/commands/seed.go new file mode 100644 index 0000000..ec639b5 --- /dev/null +++ b/devtools/admin/commands/seed.go @@ -0,0 +1,47 @@ +package commands + +import ( + "context" + "encoding/json" + "os" + + "git.defalsify.org/vise.git/logging" + "git.grassecon.net/grassrootseconomics/sarafu-vise/store" +) + +var ( + logg = logging.NewVanilla().WithDomain("adminstore") +) + +type Admin struct { + PhoneNumber string `json:"phonenumber"` +} + +type Config struct { + Admins []Admin `json:"admins"` +} + +func Seed(ctx context.Context) error { + var config Config + adminstore, err := store.NewAdminStore(ctx, "../admin_numbers") + store := adminstore.FsStore + if err != nil { + return err + } + defer store.Close() + data, err := os.ReadFile("admin_numbers.json") + if err != nil { + return err + } + if err := json.Unmarshal(data, &config); err != nil { + return err + } + for _, admin := range config.Admins { + err := store.Put(ctx, []byte(admin.PhoneNumber), []byte("1")) + if err != nil { + logg.Printf(logging.LVL_DEBUG, "Failed to insert admin number", admin.PhoneNumber) + return err + } + } + return nil +} diff --git a/devtools/admin/main.go b/devtools/admin/main.go new file mode 100644 index 0000000..defbbf6 --- /dev/null +++ b/devtools/admin/main.go @@ -0,0 +1,17 @@ +package main + +import ( + "context" + "log" + + "git.grassecon.net/grassrootseconomics/sarafu-vise/devtools/admin/commands" +) + +func main() { + ctx := context.Background() + err := commands.Seed(ctx) + if err != nil { + log.Fatalf("Failed to initialize a list of admins with error %s", err) + } + +} diff --git a/go.mod b/go.mod index 23115fe..d97d956 100644 --- a/go.mod +++ b/go.mod @@ -5,8 +5,8 @@ go 1.23.4 require ( git.defalsify.org/vise.git v0.2.3-0.20250103172917-3e190a44568d git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250111211303-3ea726a0302c - git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250111163106-c93a07832daa - git.grassecon.net/grassrootseconomics/visedriver-africastalking v0.0.0-20250111203946-a65fe6559956 + git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250111222109-2ea51d88d8c0 + git.grassecon.net/grassrootseconomics/visedriver-africastalking v0.0.0-20250111212417-1f8ba0d7ff9b github.com/alecthomas/assert/v2 v2.2.2 github.com/gofrs/uuid v4.4.0+incompatible github.com/grassrootseconomics/ussd-data-service v1.2.0-beta diff --git a/go.sum b/go.sum index 29edbde..d36f18a 100644 --- a/go.sum +++ b/go.sum @@ -2,10 +2,10 @@ git.defalsify.org/vise.git v0.2.3-0.20250103172917-3e190a44568d h1:bPAOVZOX4frSG git.defalsify.org/vise.git v0.2.3-0.20250103172917-3e190a44568d/go.mod h1:jyBMe1qTYUz3mmuoC9JQ/TvFeW0vTanCUcPu3H8p4Ck= git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250111211303-3ea726a0302c h1:5h1nsczPXBhOfe5Wbyccp3ontooztKUVAtDw8aoT8BI= git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250111211303-3ea726a0302c/go.mod h1:CXdVutRsCkdWWCJ9hELi/72z3FDKkhLksxCXBSnjuKI= -git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250111163106-c93a07832daa h1:owrXxiDOp7uuyvGxi4DU8gu8PyA7YxVnX8bo7i28Anw= -git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250111163106-c93a07832daa/go.mod h1:E6W7ZOa7ZvVr0Bc5ot0LNSwpSPYq4hXlAIvEPy3AJ7U= -git.grassecon.net/grassrootseconomics/visedriver-africastalking v0.0.0-20250111203946-a65fe6559956 h1:wTfH1CP0bh1JDcrJoqbBlnpmelpPSOslknCeCXPUpLs= -git.grassecon.net/grassrootseconomics/visedriver-africastalking v0.0.0-20250111203946-a65fe6559956/go.mod h1:0vbG2P/BSIfKgI8GbhBTQ+YfPMptgbiGxz6CpZDYi3w= +git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250111222109-2ea51d88d8c0 h1:jzYZKsZBuho4kt0dEWqflXHPLM7tWxpNmXTGFP615dE= +git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250111222109-2ea51d88d8c0/go.mod h1:E6W7ZOa7ZvVr0Bc5ot0LNSwpSPYq4hXlAIvEPy3AJ7U= +git.grassecon.net/grassrootseconomics/visedriver-africastalking v0.0.0-20250111212417-1f8ba0d7ff9b h1:3bM4VFf3h3EoqeEtMiT49ymklpIUgBFnT53V246gy1g= +git.grassecon.net/grassrootseconomics/visedriver-africastalking v0.0.0-20250111212417-1f8ba0d7ff9b/go.mod h1:0vbG2P/BSIfKgI8GbhBTQ+YfPMptgbiGxz6CpZDYi3w= github.com/alecthomas/assert/v2 v2.2.2 h1:Z/iVC0xZfWTaFNE6bA3z07T86hd45Xe2eLt6WVy2bbk= github.com/alecthomas/assert/v2 v2.2.2/go.mod h1:pXcQ2Asjp247dahGEmsZ6ru0UVwnkhktn7S0bBDLxvQ= github.com/alecthomas/participle/v2 v2.0.0 h1:Fgrq+MbuSsJwIkw3fEj9h75vDP0Er5JzepJ0/HNHv0g= diff --git a/handlers/application/menuhandler.go b/handlers/application/menuhandler.go index f60b462..8da7a84 100644 --- a/handlers/application/menuhandler.go +++ b/handlers/application/menuhandler.go @@ -22,6 +22,7 @@ import ( "git.grassecon.net/grassrootseconomics/visedriver/utils" "git.grassecon.net/grassrootseconomics/sarafu-vise/profile" + "git.grassecon.net/grassrootseconomics/sarafu-vise/store" "git.grassecon.net/grassrootseconomics/sarafu-api/remote" dbstorage "git.grassecon.net/grassrootseconomics/visedriver/storage/db" @@ -62,7 +63,7 @@ type MenuHandlers struct { st *state.State ca cache.Memory userdataStore common.DataStore - adminstore *utils.AdminStore + adminstore *store.AdminStore flagManager *asm.FlagParser accountService remote.AccountService prefixDb dbstorage.PrefixDb @@ -71,7 +72,7 @@ type MenuHandlers struct { } // NewHandlers creates a new instance of the Handlers struct with the provided dependencies. -func NewMenuHandlers(appFlags *asm.FlagParser, userdataStore db.Db, adminstore *utils.AdminStore, accountService remote.AccountService, replaceSeparatorFunc func(string) string) (*MenuHandlers, error) { +func NewMenuHandlers(appFlags *asm.FlagParser, userdataStore db.Db, adminstore *store.AdminStore, accountService remote.AccountService, replaceSeparatorFunc func(string) string) (*MenuHandlers, error) { if userdataStore == nil { return nil, fmt.Errorf("cannot create handler with nil userdata store") } diff --git a/handlers/application/menuhandler_test.go b/handlers/application/menuhandler_test.go index 51653c6..bf8c718 100644 --- a/handlers/application/menuhandler_test.go +++ b/handlers/application/menuhandler_test.go @@ -15,10 +15,10 @@ import ( "git.defalsify.org/vise.git/resource" "git.defalsify.org/vise.git/state" dbstorage "git.grassecon.net/grassrootseconomics/visedriver/storage/db" - "git.grassecon.net/grassrootseconomics/visedriver/utils" "git.grassecon.net/grassrootseconomics/sarafu-api/models" "git.grassecon.net/grassrootseconomics/sarafu-api/testutil/testservice" "git.grassecon.net/grassrootseconomics/sarafu-api/testutil/mocks" + "git.grassecon.net/grassrootseconomics/sarafu-vise/store" "git.grassecon.net/grassrootseconomics/visedriver/common" "github.com/alecthomas/assert/v2" @@ -124,7 +124,7 @@ func TestNewMenuHandlers(t *testing.T) { func TestInit(t *testing.T) { sessionId := "session123" - ctx, store := InitializeTestStore(t) + ctx, testStore := InitializeTestStore(t) ctx = context.WithValue(ctx, "SessionId", sessionId) fm, err := NewFlagManager(flagsPath) @@ -132,7 +132,7 @@ func TestInit(t *testing.T) { t.Fatal(err.Error()) } - adminstore, err := utils.NewAdminStore(ctx, "admin_numbers") + adminstore, err := store.NewAdminStore(ctx, "admin_numbers") if err != nil { t.Fatal(err.Error()) } @@ -159,7 +159,7 @@ func TestInit(t *testing.T) { { name: "State and memory initialization", setup: func() (*MenuHandlers, context.Context) { - pe := persist.NewPersister(store).WithSession(sessionId).WithContent(st, ca) + pe := persist.NewPersister(testStore).WithSession(sessionId).WithContent(st, ca) h := &MenuHandlers{ flagManager: fm.parser, adminstore: adminstore, @@ -175,7 +175,7 @@ func TestInit(t *testing.T) { { name: "Non-admin session initialization", setup: func() (*MenuHandlers, context.Context) { - pe := persist.NewPersister(store).WithSession("0712345678").WithContent(st, ca) + pe := persist.NewPersister(testStore).WithSession("0712345678").WithContent(st, ca) h := &MenuHandlers{ flagManager: fm.parser, adminstore: adminstore, @@ -191,7 +191,7 @@ func TestInit(t *testing.T) { { name: "Move to top node on empty input", setup: func() (*MenuHandlers, context.Context) { - pe := persist.NewPersister(store).WithSession(sessionId).WithContent(st, ca) + pe := persist.NewPersister(testStore).WithSession(sessionId).WithContent(st, ca) h := &MenuHandlers{ flagManager: fm.parser, adminstore: adminstore, diff --git a/handlers/local.go b/handlers/local.go index 06a1f9f..5650cf0 100644 --- a/handlers/local.go +++ b/handlers/local.go @@ -10,9 +10,9 @@ import ( "git.defalsify.org/vise.git/persist" "git.defalsify.org/vise.git/resource" - "git.grassecon.net/grassrootseconomics/visedriver/utils" "git.grassecon.net/grassrootseconomics/sarafu-api/remote" "git.grassecon.net/grassrootseconomics/sarafu-vise/handlers/application" + "git.grassecon.net/grassrootseconomics/sarafu-vise/store" ) type HandlerService interface { @@ -33,7 +33,7 @@ type LocalHandlerService struct { DbRs *resource.DbResource Pe *persist.Persister UserdataStore *db.Db - AdminStore *utils.AdminStore + AdminStore *store.AdminStore Cfg engine.Config Rs resource.Resource } @@ -43,7 +43,7 @@ func NewLocalHandlerService(ctx context.Context, fp string, debug bool, dbResour if err != nil { return nil, err } - adminstore, err := utils.NewAdminStore(ctx, "admin_numbers") + adminstore, err := store.NewAdminStore(ctx, "admin_numbers") if err != nil { return nil, err } diff --git a/store/adminstore.go b/store/adminstore.go new file mode 100644 index 0000000..d20c5e1 --- /dev/null +++ b/store/adminstore.go @@ -0,0 +1,51 @@ +package store + +import ( + "context" + + "git.defalsify.org/vise.git/db" + fsdb "git.defalsify.org/vise.git/db/fs" + "git.defalsify.org/vise.git/logging" +) + +var ( + logg = logging.NewVanilla().WithDomain("adminstore") +) + +type AdminStore struct { + ctx context.Context + FsStore db.Db +} + +func NewAdminStore(ctx context.Context, fileName string) (*AdminStore, error) { + fsStore, err := getFsStore(ctx, fileName) + if err != nil { + return nil, err + } + return &AdminStore{ctx: ctx, FsStore: fsStore}, nil +} + +func getFsStore(ctx context.Context, connectStr string) (db.Db, error) { + fsStore := fsdb.NewFsDb() + err := fsStore.Connect(ctx, connectStr) + fsStore.SetPrefix(db.DATATYPE_USERDATA) + if err != nil { + return nil, err + } + return fsStore, nil +} + +// Checks if the given sessionId is listed as an admin. +func (as *AdminStore) IsAdmin(sessionId string) (bool, error) { + _, err := as.FsStore.Get(as.ctx, []byte(sessionId)) + if err != nil { + if db.IsNotFound(err) { + logg.Printf(logging.LVL_INFO, "Returning false because session id was not found") + return false, nil + } else { + return false, err + } + } + + return true, nil +}