mirror of
https://github.com/ClusterCockpit/cc-backend
synced 2025-07-23 12:51:40 +02:00
individual configurations per user
This commit is contained in:
134
config/config.go
134
config/config.go
@@ -3,63 +3,143 @@ package config
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/ClusterCockpit/cc-jobarchive/auth"
|
||||
"github.com/ClusterCockpit/cc-jobarchive/graph/model"
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
var db *sqlx.DB
|
||||
var lock sync.RWMutex
|
||||
var config map[string]interface{}
|
||||
var uiDefaults map[string]interface{}
|
||||
|
||||
var Clusters []*model.Cluster
|
||||
|
||||
const configFilePath string = "./var/ui.config.json"
|
||||
|
||||
func init() {
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
|
||||
bytes, err := os.ReadFile(configFilePath)
|
||||
func Init(usersdb *sqlx.DB, authEnabled bool, uiConfig map[string]interface{}, jobArchive string) error {
|
||||
db = usersdb
|
||||
uiDefaults = uiConfig
|
||||
entries, err := os.ReadDir(jobArchive)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return err
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(bytes, &config); err != nil {
|
||||
log.Fatal(err)
|
||||
Clusters = []*model.Cluster{}
|
||||
for _, de := range entries {
|
||||
bytes, err := os.ReadFile(filepath.Join(jobArchive, de.Name(), "cluster.json"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var cluster model.Cluster
|
||||
if err := json.Unmarshal(bytes, &cluster); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if cluster.FilterRanges.StartTime.To.IsZero() {
|
||||
cluster.FilterRanges.StartTime.To = time.Unix(0, 0)
|
||||
}
|
||||
|
||||
if cluster.ClusterID != de.Name() {
|
||||
return fmt.Errorf("the file '%s/cluster.json' contains the clusterId '%s'", de.Name(), cluster.ClusterID)
|
||||
}
|
||||
|
||||
Clusters = append(Clusters, &cluster)
|
||||
}
|
||||
|
||||
if authEnabled {
|
||||
_, err := db.Exec(`
|
||||
CREATE TABLE IF NOT EXISTS configuration (
|
||||
username varchar(255),
|
||||
key varchar(255),
|
||||
value varchar(255),
|
||||
PRIMARY KEY (username, key),
|
||||
FOREIGN KEY (username) REFERENCES user (username) ON DELETE CASCADE ON UPDATE NO ACTION);`)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Call this function to change the current configuration.
|
||||
// `value` must be valid JSON. This This function is thread-safe.
|
||||
func UpdateConfig(key, value string, ctx context.Context) error {
|
||||
var v interface{}
|
||||
if err := json.Unmarshal([]byte(value), &v); err != nil {
|
||||
return err
|
||||
// Return the personalised UI config for the currently authenticated
|
||||
// user or return the plain default config.
|
||||
func GetUIConfig(r *http.Request) (map[string]interface{}, error) {
|
||||
lock.RLock()
|
||||
config := make(map[string]interface{}, len(uiDefaults))
|
||||
for k, v := range uiDefaults {
|
||||
config[k] = v
|
||||
}
|
||||
lock.RUnlock()
|
||||
|
||||
user := auth.GetUser(r.Context())
|
||||
if user == nil {
|
||||
return config, nil
|
||||
}
|
||||
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
|
||||
config[key] = v
|
||||
bytes, err := json.Marshal(config)
|
||||
rows, err := db.Query(`SELECT key, value FROM configuration WHERE configuration.username = ?`, user.Username)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := os.WriteFile(configFilePath, bytes, 0644); err != nil {
|
||||
for rows.Next() {
|
||||
var key, rawval string
|
||||
if err := rows.Scan(&key, &rawval); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var val interface{}
|
||||
if err := json.Unmarshal([]byte(rawval), &val); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
config[key] = val
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
||||
|
||||
// If the context does not have a user, update the global ui configuration without persisting it!
|
||||
// If there is a (authenticated) user, update only his configuration.
|
||||
func UpdateConfig(key, value string, ctx context.Context) error {
|
||||
user := auth.GetUser(ctx)
|
||||
if user == nil {
|
||||
lock.RLock()
|
||||
defer lock.RUnlock()
|
||||
|
||||
var val interface{}
|
||||
if err := json.Unmarshal([]byte(value), &val); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
uiDefaults[key] = val
|
||||
return nil
|
||||
}
|
||||
|
||||
if _, err := db.Exec(`REPLACE INTO configuration (username, key, value) VALUES (?, ?, ?)`,
|
||||
user.Username, key, value); err != nil {
|
||||
log.Printf("db.Exec: %s\n", err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// http.HandlerFunc compatible function that serves the current configuration as JSON
|
||||
// http.HandlerFunc compatible function that serves the current configuration as JSON.
|
||||
// TODO: Use templates and stuff instead of this...
|
||||
func ServeConfig(rw http.ResponseWriter, r *http.Request) {
|
||||
lock.RLock()
|
||||
defer lock.RUnlock()
|
||||
config, err := GetUIConfig(r)
|
||||
if err != nil {
|
||||
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
rw.Header().Set("Content-Type", "application/json")
|
||||
if err := json.NewEncoder(rw).Encode(config); err != nil {
|
||||
|
Reference in New Issue
Block a user