Make admin store local

This commit is contained in:
lash 2025-01-11 22:26:47 +00:00
parent 2822ab459a
commit 76992df3f1
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
10 changed files with 141 additions and 18 deletions

View File

@ -22,7 +22,7 @@ import (
"git.grassecon.net/grassrootseconomics/visedriver/session" "git.grassecon.net/grassrootseconomics/visedriver/session"
at "git.grassecon.net/grassrootseconomics/visedriver-africastalking/africastalking" 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" httpremote "git.grassecon.net/grassrootseconomics/sarafu-api/remote/http"
"git.grassecon.net/grassrootseconomics/sarafu-vise/handlers" "git.grassecon.net/grassrootseconomics/sarafu-vise/handlers"

View File

@ -0,0 +1,7 @@
{
"admins": [
{
"phonenumber" : "<replace with any admin number to test with >"
}
]
}

View File

@ -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
}

17
devtools/admin/main.go Normal file
View File

@ -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)
}
}

4
go.mod
View File

@ -5,8 +5,8 @@ go 1.23.4
require ( require (
git.defalsify.org/vise.git v0.2.3-0.20250103172917-3e190a44568d 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/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 v0.8.0-beta.10.0.20250111222109-2ea51d88d8c0
git.grassecon.net/grassrootseconomics/visedriver-africastalking v0.0.0-20250111203946-a65fe6559956 git.grassecon.net/grassrootseconomics/visedriver-africastalking v0.0.0-20250111212417-1f8ba0d7ff9b
github.com/alecthomas/assert/v2 v2.2.2 github.com/alecthomas/assert/v2 v2.2.2
github.com/gofrs/uuid v4.4.0+incompatible github.com/gofrs/uuid v4.4.0+incompatible
github.com/grassrootseconomics/ussd-data-service v1.2.0-beta github.com/grassrootseconomics/ussd-data-service v1.2.0-beta

8
go.sum
View File

@ -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.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 h1:5h1nsczPXBhOfe5Wbyccp3ontooztKUVAtDw8aoT8BI=
git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250111211303-3ea726a0302c/go.mod h1:CXdVutRsCkdWWCJ9hELi/72z3FDKkhLksxCXBSnjuKI= 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.20250111222109-2ea51d88d8c0 h1:jzYZKsZBuho4kt0dEWqflXHPLM7tWxpNmXTGFP615dE=
git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250111163106-c93a07832daa/go.mod h1:E6W7ZOa7ZvVr0Bc5ot0LNSwpSPYq4hXlAIvEPy3AJ7U= 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-20250111203946-a65fe6559956 h1:wTfH1CP0bh1JDcrJoqbBlnpmelpPSOslknCeCXPUpLs= git.grassecon.net/grassrootseconomics/visedriver-africastalking v0.0.0-20250111212417-1f8ba0d7ff9b h1:3bM4VFf3h3EoqeEtMiT49ymklpIUgBFnT53V246gy1g=
git.grassecon.net/grassrootseconomics/visedriver-africastalking v0.0.0-20250111203946-a65fe6559956/go.mod h1:0vbG2P/BSIfKgI8GbhBTQ+YfPMptgbiGxz6CpZDYi3w= 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 h1:Z/iVC0xZfWTaFNE6bA3z07T86hd45Xe2eLt6WVy2bbk=
github.com/alecthomas/assert/v2 v2.2.2/go.mod h1:pXcQ2Asjp247dahGEmsZ6ru0UVwnkhktn7S0bBDLxvQ= github.com/alecthomas/assert/v2 v2.2.2/go.mod h1:pXcQ2Asjp247dahGEmsZ6ru0UVwnkhktn7S0bBDLxvQ=
github.com/alecthomas/participle/v2 v2.0.0 h1:Fgrq+MbuSsJwIkw3fEj9h75vDP0Er5JzepJ0/HNHv0g= github.com/alecthomas/participle/v2 v2.0.0 h1:Fgrq+MbuSsJwIkw3fEj9h75vDP0Er5JzepJ0/HNHv0g=

View File

@ -22,6 +22,7 @@ import (
"git.grassecon.net/grassrootseconomics/visedriver/utils" "git.grassecon.net/grassrootseconomics/visedriver/utils"
"git.grassecon.net/grassrootseconomics/sarafu-vise/profile" "git.grassecon.net/grassrootseconomics/sarafu-vise/profile"
"git.grassecon.net/grassrootseconomics/sarafu-vise/store"
"git.grassecon.net/grassrootseconomics/sarafu-api/remote" "git.grassecon.net/grassrootseconomics/sarafu-api/remote"
dbstorage "git.grassecon.net/grassrootseconomics/visedriver/storage/db" dbstorage "git.grassecon.net/grassrootseconomics/visedriver/storage/db"
@ -62,7 +63,7 @@ type MenuHandlers struct {
st *state.State st *state.State
ca cache.Memory ca cache.Memory
userdataStore common.DataStore userdataStore common.DataStore
adminstore *utils.AdminStore adminstore *store.AdminStore
flagManager *asm.FlagParser flagManager *asm.FlagParser
accountService remote.AccountService accountService remote.AccountService
prefixDb dbstorage.PrefixDb prefixDb dbstorage.PrefixDb
@ -71,7 +72,7 @@ type MenuHandlers struct {
} }
// NewHandlers creates a new instance of the Handlers struct with the provided dependencies. // 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 { if userdataStore == nil {
return nil, fmt.Errorf("cannot create handler with nil userdata store") return nil, fmt.Errorf("cannot create handler with nil userdata store")
} }

View File

@ -15,10 +15,10 @@ import (
"git.defalsify.org/vise.git/resource" "git.defalsify.org/vise.git/resource"
"git.defalsify.org/vise.git/state" "git.defalsify.org/vise.git/state"
dbstorage "git.grassecon.net/grassrootseconomics/visedriver/storage/db" 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/models"
"git.grassecon.net/grassrootseconomics/sarafu-api/testutil/testservice" "git.grassecon.net/grassrootseconomics/sarafu-api/testutil/testservice"
"git.grassecon.net/grassrootseconomics/sarafu-api/testutil/mocks" "git.grassecon.net/grassrootseconomics/sarafu-api/testutil/mocks"
"git.grassecon.net/grassrootseconomics/sarafu-vise/store"
"git.grassecon.net/grassrootseconomics/visedriver/common" "git.grassecon.net/grassrootseconomics/visedriver/common"
"github.com/alecthomas/assert/v2" "github.com/alecthomas/assert/v2"
@ -124,7 +124,7 @@ func TestNewMenuHandlers(t *testing.T) {
func TestInit(t *testing.T) { func TestInit(t *testing.T) {
sessionId := "session123" sessionId := "session123"
ctx, store := InitializeTestStore(t) ctx, testStore := InitializeTestStore(t)
ctx = context.WithValue(ctx, "SessionId", sessionId) ctx = context.WithValue(ctx, "SessionId", sessionId)
fm, err := NewFlagManager(flagsPath) fm, err := NewFlagManager(flagsPath)
@ -132,7 +132,7 @@ func TestInit(t *testing.T) {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
adminstore, err := utils.NewAdminStore(ctx, "admin_numbers") adminstore, err := store.NewAdminStore(ctx, "admin_numbers")
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
@ -159,7 +159,7 @@ func TestInit(t *testing.T) {
{ {
name: "State and memory initialization", name: "State and memory initialization",
setup: func() (*MenuHandlers, context.Context) { 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{ h := &MenuHandlers{
flagManager: fm.parser, flagManager: fm.parser,
adminstore: adminstore, adminstore: adminstore,
@ -175,7 +175,7 @@ func TestInit(t *testing.T) {
{ {
name: "Non-admin session initialization", name: "Non-admin session initialization",
setup: func() (*MenuHandlers, context.Context) { 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{ h := &MenuHandlers{
flagManager: fm.parser, flagManager: fm.parser,
adminstore: adminstore, adminstore: adminstore,
@ -191,7 +191,7 @@ func TestInit(t *testing.T) {
{ {
name: "Move to top node on empty input", name: "Move to top node on empty input",
setup: func() (*MenuHandlers, context.Context) { 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{ h := &MenuHandlers{
flagManager: fm.parser, flagManager: fm.parser,
adminstore: adminstore, adminstore: adminstore,

View File

@ -10,9 +10,9 @@ import (
"git.defalsify.org/vise.git/persist" "git.defalsify.org/vise.git/persist"
"git.defalsify.org/vise.git/resource" "git.defalsify.org/vise.git/resource"
"git.grassecon.net/grassrootseconomics/visedriver/utils"
"git.grassecon.net/grassrootseconomics/sarafu-api/remote" "git.grassecon.net/grassrootseconomics/sarafu-api/remote"
"git.grassecon.net/grassrootseconomics/sarafu-vise/handlers/application" "git.grassecon.net/grassrootseconomics/sarafu-vise/handlers/application"
"git.grassecon.net/grassrootseconomics/sarafu-vise/store"
) )
type HandlerService interface { type HandlerService interface {
@ -33,7 +33,7 @@ type LocalHandlerService struct {
DbRs *resource.DbResource DbRs *resource.DbResource
Pe *persist.Persister Pe *persist.Persister
UserdataStore *db.Db UserdataStore *db.Db
AdminStore *utils.AdminStore AdminStore *store.AdminStore
Cfg engine.Config Cfg engine.Config
Rs resource.Resource Rs resource.Resource
} }
@ -43,7 +43,7 @@ func NewLocalHandlerService(ctx context.Context, fp string, debug bool, dbResour
if err != nil { if err != nil {
return nil, err return nil, err
} }
adminstore, err := utils.NewAdminStore(ctx, "admin_numbers") adminstore, err := store.NewAdminStore(ctx, "admin_numbers")
if err != nil { if err != nil {
return nil, err return nil, err
} }

51
store/adminstore.go Normal file
View File

@ -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
}