pin-reset #139

Merged
lash merged 61 commits from pin-reset into pre-mock-remove 2024-11-02 14:54:20 +01:00
Showing only changes of commit b404ae95fb - Show all commits

View File

@ -1,80 +1,85 @@
package utils package utils
import ( import (
"bufio" "context"
"log" "encoding/json"
"os" "os"
"strconv"
"strings"
"git.grassecon.net/urdt/ussd/initializers" "git.defalsify.org/vise.git/db"
fsdb "git.defalsify.org/vise.git/db/fs"
"git.defalsify.org/vise.git/logging"
) )
type AdminStore struct { var (
filePath string logg = logging.NewVanilla().WithDomain("adminstore")
)
type Admin struct {
PhoneNumber string `json:"phonenumber"`
} }
// Creates a new Admin store type Config struct {
func NewAdminStore(filePath string) *AdminStore { Admins []Admin `json:"admins"`
return &AdminStore{filePath: filePath} }
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
} }
// Seed initializes a list of phonenumbers with admin privileges // Seed initializes a list of phonenumbers with admin privileges
func (as *AdminStore) Seed() error { func (as *AdminStore) Seed() error {
var adminNumbers []int64 var config Config
numbersEnv := initializers.GetEnv("ADMIN_NUMBERS", "") store := as.fsStore
for _, numStr := range strings.Split(numbersEnv, ",") { defer store.Close()
if num, err := strconv.ParseInt(strings.TrimSpace(numStr), 10, 64); err == nil {
adminNumbers = append(adminNumbers, num) data, err := os.ReadFile("admin_numbers.json")
Outdated
Review

Let's extract this into a devtools folder, separate executable.

Let's extract this into a devtools folder, separate executable.

Consider: 7fa38340dd

Consider: 7fa38340ddf3cfaab0207506d048db32bf82dc52
} else {
log.Printf("Skipping invalid number: %s", numStr)
}
}
file, err := os.Create(as.filePath)
if err != nil { if err != nil {
return err return err
} }
defer file.Close() if err := json.Unmarshal(data, &config); err != nil {
return err
writer := bufio.NewWriter(file) }
for _, num := range adminNumbers { for _, admin := range config.Admins {
_, err := writer.WriteString(strconv.FormatInt(num, 10) + "\n") err := store.Put(as.ctx, []byte(admin.PhoneNumber), []byte("1"))
if err != nil { if err != nil {
logg.Printf(logging.LVL_DEBUG, "Failed to insert admin number", admin.PhoneNumber)
return err return err
} }
} }
return writer.Flush() return nil
} }
func (as *AdminStore) load() ([]int64, error) { // Checks if the given sessionId is listed as an admin.
file, err := os.Open(as.filePath) func (as *AdminStore) IsAdmin(sessionId string) (bool, error) {
_, err := as.fsStore.Get(as.ctx, []byte(sessionId))
if err != nil { if err != nil {
return nil, err if db.IsNotFound(err) {
} logg.Printf(logging.LVL_INFO, "Returning false because session id was not found")
defer file.Close() return false, nil
} else {
var numbers []int64 return false, err
scanner := bufio.NewScanner(file)
for scanner.Scan() {
num, err := strconv.ParseInt(scanner.Text(), 10, 64)
if err != nil {
return nil, err
}
numbers = append(numbers, num)
}
return numbers, scanner.Err()
}
func (as *AdminStore) IsAdmin(phoneNumber int64) (bool, error) {
phoneNumbers, err := as.load()
if err != nil {
return false, err
}
for _, phonenumber := range phoneNumbers {
if phonenumber == phoneNumber {
return true, nil
} }
} }
return false, nil
return true, nil
} }