mirror of
https://github.com/fankes/beszel.git
synced 2025-10-20 02:09:28 +08:00
create UserManager
This commit is contained in:
@@ -1,8 +0,0 @@
|
|||||||
package user
|
|
||||||
|
|
||||||
type UserSettings struct {
|
|
||||||
// Language string `json:"lang"`
|
|
||||||
ChartTime string `json:"chartTime"`
|
|
||||||
NotificationEmails []string `json:"emails"`
|
|
||||||
NotificationWebhooks []string `json:"webhooks"`
|
|
||||||
}
|
|
@@ -4,8 +4,8 @@ import (
|
|||||||
"beszel"
|
"beszel"
|
||||||
"beszel/internal/alerts"
|
"beszel/internal/alerts"
|
||||||
"beszel/internal/entities/system"
|
"beszel/internal/entities/system"
|
||||||
"beszel/internal/entities/user"
|
|
||||||
"beszel/internal/records"
|
"beszel/internal/records"
|
||||||
|
"beszel/internal/user"
|
||||||
"beszel/site"
|
"beszel/site"
|
||||||
"context"
|
"context"
|
||||||
"crypto/ed25519"
|
"crypto/ed25519"
|
||||||
@@ -49,8 +49,9 @@ func NewHub(app *pocketbase.PocketBase) *Hub {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *Hub) Run() {
|
func (h *Hub) Run() {
|
||||||
var rm *records.RecordManager
|
rm := records.NewRecordManager(h.app)
|
||||||
var am *alerts.AlertManager
|
am := alerts.NewAlertManager(h.app)
|
||||||
|
um := user.NewUserManager(h.app)
|
||||||
|
|
||||||
// loosely check if it was executed using "go run"
|
// loosely check if it was executed using "go run"
|
||||||
isGoRun := strings.HasPrefix(os.Args[0], os.TempDir())
|
isGoRun := strings.HasPrefix(os.Args[0], os.TempDir())
|
||||||
@@ -64,9 +65,6 @@ func (h *Hub) Run() {
|
|||||||
|
|
||||||
// initial setup
|
// initial setup
|
||||||
h.app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
|
h.app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
|
||||||
// set up record manager and alert manager
|
|
||||||
rm = records.NewRecordManager(h.app)
|
|
||||||
am = alerts.NewAlertManager(h.app)
|
|
||||||
// create ssh client config
|
// create ssh client config
|
||||||
err := h.createSSHClientConfig()
|
err := h.createSSHClientConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -146,15 +144,6 @@ func (h *Hub) Run() {
|
|||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
// user creation - set default role to user if unset
|
|
||||||
h.app.OnModelBeforeCreate("users").Add(func(e *core.ModelEvent) error {
|
|
||||||
user := e.Model.(*models.Record)
|
|
||||||
if user.GetString("role") == "" {
|
|
||||||
user.Set("role", "user")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
// system creation defaults
|
// system creation defaults
|
||||||
h.app.OnModelBeforeCreate("systems").Add(func(e *core.ModelEvent) error {
|
h.app.OnModelBeforeCreate("systems").Add(func(e *core.ModelEvent) error {
|
||||||
record := e.Model.(*models.Record)
|
record := e.Model.(*models.Record)
|
||||||
@@ -168,36 +157,10 @@ func (h *Hub) Run() {
|
|||||||
go h.updateSystem(e.Model.(*models.Record))
|
go h.updateSystem(e.Model.(*models.Record))
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
// default user settings
|
|
||||||
h.app.OnModelBeforeCreate("user_settings").Add(func(e *core.ModelEvent) error {
|
// handle default values for user / user_settings creation
|
||||||
record := e.Model.(*models.Record)
|
h.app.OnModelBeforeCreate("users").Add(um.InitializeUserRole)
|
||||||
// intialize settings with defaults
|
h.app.OnModelBeforeCreate("user_settings").Add(um.InitializeUserSettings)
|
||||||
settings := user.UserSettings{
|
|
||||||
// Language: "en",
|
|
||||||
ChartTime: "1h",
|
|
||||||
NotificationEmails: []string{},
|
|
||||||
NotificationWebhooks: []string{},
|
|
||||||
}
|
|
||||||
record.UnmarshalJSONField("settings", &settings)
|
|
||||||
if len(settings.NotificationEmails) == 0 {
|
|
||||||
// get user email from auth record
|
|
||||||
if errs := h.app.Dao().ExpandRecord(record, []string{"user"}, nil); len(errs) == 0 {
|
|
||||||
// app.Logger().Error("failed to expand user relation", "errs", errs)
|
|
||||||
if user := record.ExpandedOne("user"); user != nil {
|
|
||||||
settings.NotificationEmails = []string{user.GetString("email")}
|
|
||||||
} else {
|
|
||||||
log.Println("Failed to get user email from auth record")
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
log.Println("failed to expand user relation", "errs", errs)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// if len(settings.NotificationWebhooks) == 0 {
|
|
||||||
// settings.NotificationWebhooks = []string{""}
|
|
||||||
// }
|
|
||||||
record.Set("settings", settings)
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
// do things after a systems record is updated
|
// do things after a systems record is updated
|
||||||
h.app.OnModelAfterUpdate("systems").Add(func(e *core.ModelEvent) error {
|
h.app.OnModelAfterUpdate("systems").Add(func(e *core.ModelEvent) error {
|
||||||
|
64
beszel/internal/user/user.go
Normal file
64
beszel/internal/user/user.go
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
package user
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/pocketbase/pocketbase"
|
||||||
|
"github.com/pocketbase/pocketbase/core"
|
||||||
|
"github.com/pocketbase/pocketbase/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
type UserManager struct {
|
||||||
|
app *pocketbase.PocketBase
|
||||||
|
}
|
||||||
|
|
||||||
|
type UserSettings struct {
|
||||||
|
// Language string `json:"lang"`
|
||||||
|
ChartTime string `json:"chartTime"`
|
||||||
|
NotificationEmails []string `json:"emails"`
|
||||||
|
NotificationWebhooks []string `json:"webhooks"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewUserManager(app *pocketbase.PocketBase) *UserManager {
|
||||||
|
return &UserManager{
|
||||||
|
app: app,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (um *UserManager) InitializeUserRole(e *core.ModelEvent) error {
|
||||||
|
user := e.Model.(*models.Record)
|
||||||
|
if user.GetString("role") == "" {
|
||||||
|
user.Set("role", "user")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (um *UserManager) InitializeUserSettings(e *core.ModelEvent) error {
|
||||||
|
record := e.Model.(*models.Record)
|
||||||
|
// intialize settings with defaults
|
||||||
|
settings := UserSettings{
|
||||||
|
// Language: "en",
|
||||||
|
ChartTime: "1h",
|
||||||
|
NotificationEmails: []string{},
|
||||||
|
NotificationWebhooks: []string{},
|
||||||
|
}
|
||||||
|
record.UnmarshalJSONField("settings", &settings)
|
||||||
|
if len(settings.NotificationEmails) == 0 {
|
||||||
|
// get user email from auth record
|
||||||
|
if errs := um.app.Dao().ExpandRecord(record, []string{"user"}, nil); len(errs) == 0 {
|
||||||
|
// app.Logger().Error("failed to expand user relation", "errs", errs)
|
||||||
|
if user := record.ExpandedOne("user"); user != nil {
|
||||||
|
settings.NotificationEmails = []string{user.GetString("email")}
|
||||||
|
} else {
|
||||||
|
log.Println("Failed to get user email from auth record")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.Println("failed to expand user relation", "errs", errs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// if len(settings.NotificationWebhooks) == 0 {
|
||||||
|
// settings.NotificationWebhooks = []string{""}
|
||||||
|
// }
|
||||||
|
record.Set("settings", settings)
|
||||||
|
return nil
|
||||||
|
}
|
Reference in New Issue
Block a user