2023-09-26 20:36:03 +00:00
|
|
|
package scsusers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"embed"
|
|
|
|
"html/template"
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
|
|
|
//go:embed templates/*.html
|
|
|
|
var FS embed.FS
|
|
|
|
|
2024-01-11 17:02:16 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-09-26 20:36:03 +00:00
|
|
|
func SetRegistrationTemplate(t string) bool {
|
|
|
|
if len(t) != 0 {
|
2024-01-11 17:02:16 +00:00
|
|
|
r, err := template.New("registration").Parse(t)
|
2023-09-26 20:36:03 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Templates.Registration = r
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
df, err := FS.ReadFile("templates/registration.html")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Missing recovery template")
|
|
|
|
}
|
|
|
|
|
2024-01-11 17:02:16 +00:00
|
|
|
r, err := template.New("registration").Parse(string(df))
|
2023-09-26 20:36:03 +00:00
|
|
|
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 {
|
2023-10-09 15:46:36 +00:00
|
|
|
c.Templates.TwoFA = r
|
2023-09-26 20:36:03 +00:00
|
|
|
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())
|
|
|
|
}
|
2023-10-09 15:46:36 +00:00
|
|
|
c.Templates.TwoFA = r
|
2023-09-26 20:36:03 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|