forked from grassrootseconomics/visedriver
Factor out adminstore
This commit is contained in:
parent
2e0e854c4b
commit
2ea51d88d8
@ -1,7 +0,0 @@
|
||||
{
|
||||
"admins": [
|
||||
{
|
||||
"phonenumber" : "<replace with any admin number to test with >"
|
||||
}
|
||||
]
|
||||
}
|
@ -1,47 +0,0 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"os"
|
||||
|
||||
"git.defalsify.org/vise.git/logging"
|
||||
"git.grassecon.net/grassrootseconomics/visedriver/utils"
|
||||
)
|
||||
|
||||
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 := utils.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
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
"git.grassecon.net/grassrootseconomics/visedriver/cmd/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)
|
||||
}
|
||||
|
||||
}
|
@ -1,51 +0,0 @@
|
||||
package utils
|
||||
|
||||
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
|
||||
}
|
Loading…
Reference in New Issue
Block a user