2024-10-29 11:15:41 +01:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2024-10-30 12:30:38 +01:00
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2024-10-29 11:15:41 +01:00
|
|
|
"os"
|
2024-10-29 12:26:24 +01:00
|
|
|
|
2024-10-30 12:30:38 +01:00
|
|
|
"git.defalsify.org/vise.git/db"
|
|
|
|
fsdb "git.defalsify.org/vise.git/db/fs"
|
|
|
|
"git.defalsify.org/vise.git/logging"
|
2024-10-29 11:15:41 +01:00
|
|
|
)
|
|
|
|
|
2024-10-30 12:30:38 +01:00
|
|
|
var (
|
|
|
|
logg = logging.NewVanilla().WithDomain("adminstore")
|
|
|
|
)
|
|
|
|
|
|
|
|
type Admin struct {
|
|
|
|
PhoneNumber string `json:"phonenumber"`
|
2024-10-29 11:15:41 +01:00
|
|
|
}
|
|
|
|
|
2024-10-30 12:30:38 +01:00
|
|
|
type Config struct {
|
|
|
|
Admins []Admin `json:"admins"`
|
2024-10-29 11:15:41 +01:00
|
|
|
}
|
|
|
|
|
2024-10-30 12:30:38 +01:00
|
|
|
type AdminStore struct {
|
|
|
|
ctx context.Context
|
|
|
|
fsStore db.Db
|
|
|
|
}
|
2024-10-29 12:26:24 +01:00
|
|
|
|
2024-10-30 12:30:38 +01:00
|
|
|
func NewAdminStore(ctx context.Context, fileName string) (*AdminStore, error) {
|
|
|
|
fsStore, err := getFsStore(ctx, fileName)
|
2024-10-29 11:15:41 +01:00
|
|
|
if err != nil {
|
2024-10-30 12:30:38 +01:00
|
|
|
return nil, err
|
2024-10-29 11:15:41 +01:00
|
|
|
}
|
2024-10-30 12:30:38 +01:00
|
|
|
return &AdminStore{ctx: ctx, fsStore: fsStore}, nil
|
2024-10-29 11:15:41 +01:00
|
|
|
}
|
|
|
|
|
2024-10-30 12:30:38 +01:00
|
|
|
func getFsStore(ctx context.Context, connectStr string) (db.Db, error) {
|
|
|
|
fsStore := fsdb.NewFsDb()
|
|
|
|
err := fsStore.Connect(ctx, connectStr)
|
|
|
|
fsStore.SetPrefix(db.DATATYPE_USERDATA)
|
2024-10-29 11:15:41 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-10-30 12:30:38 +01:00
|
|
|
return fsStore, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Seed initializes a list of phonenumbers with admin privileges
|
|
|
|
func (as *AdminStore) Seed() error {
|
|
|
|
var config Config
|
|
|
|
|
|
|
|
store := as.fsStore
|
|
|
|
defer store.Close()
|
2024-10-29 11:15:41 +01:00
|
|
|
|
2024-10-30 12:30:38 +01:00
|
|
|
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(as.ctx, []byte(admin.PhoneNumber), []byte("1"))
|
2024-10-29 11:15:41 +01:00
|
|
|
if err != nil {
|
2024-10-30 12:30:38 +01:00
|
|
|
logg.Printf(logging.LVL_DEBUG, "Failed to insert admin number", admin.PhoneNumber)
|
|
|
|
return err
|
2024-10-29 11:15:41 +01:00
|
|
|
}
|
|
|
|
}
|
2024-10-30 12:30:38 +01:00
|
|
|
return nil
|
2024-10-29 11:15:41 +01:00
|
|
|
}
|
|
|
|
|
2024-10-30 12:30:38 +01:00
|
|
|
// 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))
|
2024-10-29 11:15:41 +01:00
|
|
|
if err != nil {
|
2024-10-30 12:30:38 +01:00
|
|
|
if db.IsNotFound(err) {
|
|
|
|
logg.Printf(logging.LVL_INFO, "Returning false because session id was not found")
|
|
|
|
return false, nil
|
|
|
|
} else {
|
|
|
|
return false, err
|
2024-10-29 11:15:41 +01:00
|
|
|
}
|
|
|
|
}
|
2024-10-30 12:30:38 +01:00
|
|
|
|
|
|
|
return true, nil
|
2024-10-29 11:15:41 +01:00
|
|
|
}
|