Files
fnx_web/webcontroller/admin_panel.go

93 lines
2.3 KiB
Go
Raw Normal View History

2019-12-17 19:28:30 +01:00
package webcontroller
import (
"fmt"
"html/template"
"net/http"
2021-03-10 20:13:32 +01:00
"fornaxian.tech/pixeldrain_api_client/pixelapi"
2019-12-17 19:28:30 +01:00
"github.com/Fornaxian/log"
)
2019-12-23 23:56:57 +01:00
func (wc *WebController) adminGlobalsForm(td *TemplateData, r *http.Request) (f Form) {
2020-07-31 21:21:14 +02:00
if !td.Authenticated || !td.User.IsAdmin {
return Form{Title: ";-)"}
2019-12-17 19:28:30 +01:00
}
2019-12-23 23:56:57 +01:00
f = Form{
2019-12-17 19:28:30 +01:00
Name: "admin_globals",
2020-07-31 21:21:14 +02:00
Title: "Pixeldrain global configuration",
2019-12-17 19:28:30 +01:00
PreFormHTML: template.HTML("<p>Careful! The slightest typing error could bring the whole website down</p>"),
BackLink: "/admin",
SubmitLabel: "Submit",
}
globals, err := td.PixelAPI.AdminGetGlobals()
if err != nil {
f.SubmitMessages = []template.HTML{template.HTML(err.Error())}
return f
}
var globalsMap = make(map[string]string)
2021-02-23 16:50:13 +01:00
for _, v := range globals {
2019-12-23 23:56:57 +01:00
f.Fields = append(f.Fields, Field{
2019-12-17 19:28:30 +01:00
Name: v.Key,
DefaultValue: v.Value,
Label: v.Key,
2019-12-23 23:56:57 +01:00
Type: func() FieldType {
2019-12-17 19:28:30 +01:00
switch v.Key {
case
"email_address_change_body",
2019-12-23 23:56:57 +01:00
"email_password_reset_body",
"email_register_user_body":
return FieldTypeTextarea
2019-12-17 19:28:30 +01:00
case
"api_ratelimit_limit",
"api_ratelimit_rate",
"cron_interval_seconds",
"file_inactive_expiry_days",
"max_file_size",
"pixelstore_min_redundancy":
2019-12-23 23:56:57 +01:00
return FieldTypeNumber
2019-12-17 19:28:30 +01:00
default:
2019-12-23 23:56:57 +01:00
return FieldTypeText
2019-12-17 19:28:30 +01:00
}
}(),
})
globalsMap[v.Key] = v.Value
}
if f.ReadInput(r) {
var successfulUpdates = 0
for k, v := range f.Fields {
if v.EnteredValue == globalsMap[v.Name] {
continue // Change changes, no need to update
}
// Value changed, try to update global setting
2020-05-05 22:03:34 +02:00
if err = td.PixelAPI.AdminSetGlobals(v.Name, v.EnteredValue); err != nil {
2021-03-10 20:13:32 +01:00
if apiErr, ok := err.(pixelapi.Error); ok {
2019-12-17 19:28:30 +01:00
f.SubmitMessages = append(f.SubmitMessages, template.HTML(apiErr.Message))
} else {
log.Error("%s", err)
f.SubmitMessages = append(f.SubmitMessages, template.HTML(
fmt.Sprintf("Failed to set '%s': %s", v.Name, err),
))
return f
}
} else {
f.Fields[k].DefaultValue = v.EnteredValue
successfulUpdates++
}
}
if len(f.SubmitMessages) == 0 {
// Request was a success
f.SubmitSuccess = true
f.SubmitMessages = []template.HTML{template.HTML(
fmt.Sprintf("Success! %d values updated", successfulUpdates),
)}
}
}
return f
}