scsusers/templates.go

135 lines
2.9 KiB
Go

package scsusers
import (
"embed"
"html/template"
"log"
)
//go:embed templates/*.html
var FS embed.FS
func SetTemplate(name, t string) bool {
if len(t) != 0 {
r, err := template.New(name).Parse(t)
if err != nil {
log.Printf("scsusers.SetTemplate: Template %s failed to compile: %s\n", name, err.Error())
return true
}
switch name {
case "registration":
c.Templates.Registration = r
case "alert":
c.Templates.Alert = r
case "recovery":
c.Templates.Recovery = r
case "2fa":
c.Templates.TwoFA = r
}
return false
}
r, err := template.New(name).Parse(t)
if err != nil {
log.Printf("scsusers.SetTemplate: Default %s template MUST compile. Error: %s\n", name, err.Error())
return false
}
switch name {
case "registration":
c.Templates.Registration = r
case "alert":
c.Templates.Alert = r
case "recovery":
c.Templates.Recovery = r
case "2fa":
c.Templates.TwoFA = r
}
return false
}
func SetRegistrationTemplate(t string) bool {
if len(t) != 0 {
r, err := template.New("registration").Parse(t)
if err != nil {
c.Templates.Registration = r
return true
}
}
df, err := FS.ReadFile("templates/registration.html")
if err != nil {
log.Fatal("Missing recovery template")
}
r, err := template.New("registration").Parse(string(df))
if err != nil {
log.Fatal("scsusers.SetRegistrationTemplate: Default registration template MUST compile. Error: " + err.Error())
}
c.Templates.Registration = r
return false
}
func Set2faTemplate(t string) bool {
if len(t) != 0 {
r, err := template.New("2fa").Parse(t)
if err != nil {
c.Templates.TwoFA = r
return true
}
}
df, err := FS.ReadFile("templates/2fa.html")
if err != nil {
log.Fatal("Missing 2fa template")
}
r, err := template.New("2fa").Parse(string(df))
if err != nil {
log.Fatal("scsusers.Set2faTemplate: Default 2fa template MUST compile. Error: " + err.Error())
}
c.Templates.TwoFA = r
return false
}
func SetAlertTemplate(t string) bool {
if len(t) != 0 {
r, err := template.New("alert").Parse(t)
if err != nil {
c.Templates.Alert = r
return true
}
}
df, err := FS.ReadFile("templates/alert.html")
if err != nil {
log.Fatal("Missing recovery template")
}
r, err := template.New("alert").Parse(string(df))
if err != nil {
log.Fatal("scsusers.SetAlertTemplate: Default alert template MUST compile. Error: " + err.Error())
}
c.Templates.Alert = r
return false
}
func SetRecoveryTemplate(t string) bool {
if len(t) != 0 {
r, err := template.New("recovery").Parse(t)
if err != nil {
c.Templates.Recovery = r
return true
}
}
df, err := FS.ReadFile("templates/recovery.html")
if err != nil {
log.Fatal("Missing recovery template")
}
r, err := template.New("recovery").Parse(string(df))
if err != nil {
log.Fatal("scsusers.SetRecoveryTemplate: Default recovery template MUST compile. Error: " + err.Error())
}
c.Templates.Recovery = r
return false
}