Add admin-settable MOTD, rewrite existing warning system to use it

This commit is contained in:
MassiveBox 2023-10-31 23:59:09 +01:00
parent 4bf1455ba4
commit 8b81c41bd7
No known key found for this signature in database
GPG Key ID: 9B74D3A59181947D
6 changed files with 75 additions and 37 deletions

View File

@ -4,6 +4,7 @@ import (
"database/sql"
"encoding/json"
"errors"
"html/template"
"os"
"reflect"
"regexp"
@ -41,6 +42,13 @@ type Dashboard struct {
Theme string `json:"theme"`
FooterLinks []Link `json:"footer_links"`
HeaderLinks []Link `json:"header_links"`
MOTD *MessageCard `json:"motd"`
}
type MessageCard struct {
Title string `json:"title"`
Content template.HTML `json:"content"`
Style string `json:"style"`
}
var errBadHAFormat = errors.New("HomeAssistant base URL is badly formatted")

View File

@ -47,16 +47,17 @@ func (config *Config) AdminEndpoint(c *fiber.Ctx) error {
if config.IsAuthorized(c) { // here the user is submitting the form to change configuration
err := config.saveAdminForm(c)
if err != nil {
return config.RenderAdminPanel(c, Warning{
Header: "An error occurred!",
Body: html.EscapeString(err.Error()),
return config.RenderAdminPanel(c, &MessageCard{
Title: "An error occurred!",
Content: template.HTML(html.EscapeString(err.Error())),
Style: "error",
})
}
return config.RenderAdminPanel(c, Warning{
Header: "Settings applied",
Body: "Your settings have been tested and <b>applied successfully</b>.<br>" +
return config.RenderAdminPanel(c, &MessageCard{
Title: "Settings applied",
Content: "Your settings have been tested and <b>applied successfully</b>.<br>" +
"You can continue using EcoDash on the <a href='./'>Home</a>.",
IsSuccess: true,
Style: "success",
})
}
@ -64,38 +65,28 @@ func (config *Config) AdminEndpoint(c *fiber.Ctx) error {
if c.FormValue("username") == config.Administrator.Username && tools.Hash(c.FormValue("password")) == config.Administrator.PasswordHash {
c.Cookie(&fiber.Cookie{Name: "admin_username", Value: c.FormValue("username")})
c.Cookie(&fiber.Cookie{Name: "admin_password_hash", Value: tools.Hash(c.FormValue("password"))})
return config.RenderAdminPanel(c)
return config.RenderAdminPanel(c, nil)
}
return c.Render("login", fiber.Map{"Defaults": config.getTemplateDefaults(), "Failed": true}, "base")
}
if config.IsAuthorized(c) {
return config.RenderAdminPanel(c)
return config.RenderAdminPanel(c, nil)
}
return c.Render("login", config.TemplateDefaultsMap(), "base")
}
func (config *Config) RenderAdminPanel(c *fiber.Ctx, warning ...Warning) error {
func (config *Config) RenderAdminPanel(c *fiber.Ctx, message *MessageCard) error {
dirs, err := os.ReadDir("./templates")
if err != nil {
return err
}
if len(warning) > 0 {
// #nosec // TODO this is dangerous, even if we're escaping the only place where we're passing a non-literal
warning[0].BodyHTML = template.HTML(warning[0].Body)
return c.Render("admin", fiber.Map{
"Defaults": config.getTemplateDefaults(),
"Themes": dirs,
"Config": config,
"Warning": warning[0],
}, "base")
}
return c.Render("admin", fiber.Map{
"Defaults": config.getTemplateDefaults(),
"Themes": dirs,
"Config": config,
"Message": message,
}, "base")
}
@ -121,7 +112,7 @@ func (config *Config) saveAdminForm(c *fiber.Ctx) error {
HomeAssistant: HomeAssistant{ /*BaseURL to be filled later*/ APIKey: c.FormValue("api_key"), InstallationDate: dayStart(parsedTime)},
Sensors: Sensors{PolledSmartEnergySummation: c.FormValue("polled_smart_energy_summation"), FossilPercentage: c.FormValue("fossil_percentage")},
Administrator: Administrator{Username: c.FormValue("username") /*PasswordHash to be filled later*/},
Dashboard: Dashboard{Theme: c.FormValue("theme"), Name: c.FormValue("name"), HeaderLinks: config.Dashboard.HeaderLinks, FooterLinks: config.Dashboard.FooterLinks},
Dashboard: Dashboard{Theme: c.FormValue("theme"), Name: c.FormValue("name"), HeaderLinks: config.Dashboard.HeaderLinks, FooterLinks: config.Dashboard.FooterLinks /*MessageCard to be filled later*/},
}
if c.FormValue("keep_old_password") == "" {
@ -130,6 +121,14 @@ func (config *Config) saveAdminForm(c *fiber.Ctx) error {
form.Administrator.PasswordHash = config.Administrator.PasswordHash
}
if c.FormValue("motd_title") != "" || c.FormValue("motd_content") != "" {
form.Dashboard.MOTD = &MessageCard{
Title: c.FormValue("motd_title"),
Content: template.HTML(c.FormValue("motd_content")),
Style: c.FormValue("motd_style"),
}
}
fmtURL, err := formatURL(c.FormValue("base_url"))
if err != nil {
return err
@ -202,5 +201,6 @@ func (config *Config) RenderIndex(c *fiber.Ctx) error {
"EnergyConsumptions": energyConsumptions,
"GreenEnergyPercent": averageExcludingCurrentDay(greenEnergyPercents),
"PerDayUsage": perDayUsage,
"MOTD": config.Dashboard.MOTD,
}, "base")
}

View File

@ -38,7 +38,7 @@ func main() {
if config.Administrator.Username == "" || config.Administrator.PasswordHash == "" {
c.Cookie(&fiber.Cookie{Name: "admin_username", Value: ""})
c.Cookie(&fiber.Cookie{Name: "admin_password_hash", Value: tools.Hash("")})
return config.RenderAdminPanel(c)
return config.RenderAdminPanel(c, nil)
}
return config.RenderIndex(c)
})

View File

@ -4,15 +4,15 @@
<a href="https://ecodash.xyz/docs/setup/admin-panel">Documentation</a>
</p>
{{if .Warning}}
<article class="card" style="background-color: {{if .Warning.IsSuccess}}#008000{{else}}#ff5050{{end}}; color: white">
{{if .Message}}
<article class="card {{.Message.Style}}">
<header>
<h3>{{.Warning.Header}}</h3>
<h3>{{.Message.Title}}</h3>
</header>
<footer>
<p>{{.Warning.BodyHTML}}</p>
<p>{{.Message.Content}}</p>
</footer>
</article>
</article>
{{end}}
<form action="./admin" method="POST">
@ -45,6 +45,16 @@
</select>
</label>
<label>Dashboard name <input type="text" name="name" value="{{.Config.Dashboard.Name}}"></label>
<label>MOTD title <input type="text" name="motd_title" value="{{if .Config.Dashboard.MOTD}}{{.Config.Dashboard.MOTD.Title}}{{end}}"></label>
<label>MOTD content <input type="text" name="motd_content" value="{{if .Config.Dashboard.MOTD}}{{.Config.Dashboard.MOTD.Content}}{{end}}"></label>
<label>MOTD style
<select name="motd_style">
<option value="" {{if .Config.Dashboard.MOTD}}{{if eq .Config.Dashboard.MOTD.Style ""}}selected{{end}}{{end}}>Default</option>
<option value="success" {{if .Config.Dashboard.MOTD}}{{if eq .Config.Dashboard.MOTD.Style "success"}}selected{{end}}{{end}}>Success</option>
<option value="warning" {{if .Config.Dashboard.MOTD}}{{if eq .Config.Dashboard.MOTD.Style "warning"}}selected{{end}}{{end}}>Warning</option>
<option value="error" {{if .Config.Dashboard.MOTD}}{{if eq .Config.Dashboard.MOTD.Style "error"}}selected{{end}}{{end}}>Error</option>
</select>
</label>
<input type="submit" placeholder="Submit" style="margin-top: 2em; width: 100%">
</form>

View File

@ -47,3 +47,13 @@ svg, footer img { width: 100% }
}
}
.success {
background-color: #008000; color: white
}
.warning {
background-color: #807a00; color: white
}
.error {
background-color: #800000; color: white
}

View File

@ -1,5 +1,16 @@
<script src="assets/chartjs/chart.js"></script>
{{if .MOTD}}
<article class="card {{.MOTD.Style}}">
<header>
<h3>{{.MOTD.Title}}</h3>
</header>
<footer>
<p>{{.MOTD.Content}}</p>
</footer>
</article>
{{end}}
<h1>Green report</h1>
<canvas id="report"></canvas>
@ -7,7 +18,6 @@
This server's energy statistics for the last eight days (current day included)
</p>
<div class="flex two">
<div>
<div class="home-cards card">