Lots of UI and admin changes, need to clean up the three audit log tables and a few other niggles.

This commit is contained in:
2025-04-01 00:05:48 +01:00
parent 7eefb9ced0
commit aaf90b55da
11 changed files with 309 additions and 52 deletions

View File

@@ -7,6 +7,7 @@ import (
"net/http"
"synlotto-website/helpers"
"synlotto-website/middleware"
"synlotto-website/models"
)
type AdminLogEntry struct {
@@ -47,8 +48,51 @@ func AdminAccessLogHandler(db *sql.DB) http.HandlerFunc {
tmpl := template.Must(template.New("").Funcs(helpers.TemplateFuncs()).ParseFiles(
"templates/layout.html",
"templates/admin/access_log.html",
"templates/admin/logs/access_log.html",
))
_ = tmpl.ExecuteTemplate(w, "layout", context)
})
}
func AuditLogHandler(db *sql.DB) http.HandlerFunc {
return middleware.Auth(true)(func(w http.ResponseWriter, r *http.Request) {
context := helpers.TemplateContext(w, r)
rows, err := db.Query(`
SELECT timestamp, user_id, action, ip, user_agent
FROM audit_log
ORDER BY timestamp DESC
LIMIT 100
`)
if err != nil {
log.Println("❌ Failed to load audit log:", err)
http.Error(w, "Could not load audit log", http.StatusInternalServerError)
return
}
defer rows.Close()
var logs []models.AuditEntry
for rows.Next() {
var entry models.AuditEntry
err := rows.Scan(&entry.Timestamp, &entry.UserID, &entry.Action, &entry.IP, &entry.UserAgent)
if err != nil {
log.Println("⚠️ Failed to scan row:", err)
continue
}
logs = append(logs, entry)
}
context["AuditLogs"] = logs
tmpl := template.Must(template.New("").Funcs(helpers.TemplateFuncs()).ParseFiles(
"templates/layout.html",
"templates/admin/logs/audit.html",
))
err = tmpl.ExecuteTemplate(w, "layout", context)
if err != nil {
log.Println("❌ Failed to render audit page:", err)
http.Error(w, "Template error", http.StatusInternalServerError)
}
})
}