generic set template

This commit is contained in:
Your Name 2024-01-11 12:02:16 -05:00
parent b79e38a5f9
commit 00af249ff2
3 changed files with 41 additions and 7 deletions

View File

@ -9,7 +9,6 @@ import (
"time" "time"
) )
//
func SendRegistrationEmail(recipient, username, password string) bool { func SendRegistrationEmail(recipient, username, password string) bool {
data := struct { data := struct {
SiteName string SiteName string
@ -65,7 +64,6 @@ func SendAlertEmail(username, recipient, message string) bool {
return true return true
} }
func SendRecoveryEmail(recipient, username, code string) bool { func SendRecoveryEmail(recipient, username, code string) bool {
data := struct { data := struct {
SiteName string SiteName string
@ -93,7 +91,6 @@ func SendRecoveryEmail(recipient, username, code string) bool {
return true return true
} }
//
func Send2faEmail(recipient, fullname, code string) error { func Send2faEmail(recipient, fullname, code string) error {
data := struct { data := struct {
SiteName string SiteName string
@ -156,7 +153,6 @@ func SendMail(addr, from, subject, body string, to string) error {
return c.Quit() return c.Quit()
} }
//
func SendAlert(u int64, subject, body string) error { func SendAlert(u int64, subject, body string) error {
if c.Testing { if c.Testing {
return nil return nil

View File

@ -50,7 +50,7 @@ func Init(dbin *sqlx.DB, tp, sitename, fromaddr, smtpserver string, testing bool
c.SiteName = sitename c.SiteName = sitename
c.FromEmail = fromaddr c.FromEmail = fromaddr
c.SMTPServer = smtpserver c.SMTPServer = smtpserver
c.Testing=testing c.Testing = testing
SetRegistrationTemplate("") SetRegistrationTemplate("")
SetAlertTemplate("") SetAlertTemplate("")

View File

@ -9,9 +9,47 @@ import (
//go:embed templates/*.html //go:embed templates/*.html
var FS embed.FS 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 { func SetRegistrationTemplate(t string) bool {
if len(t) != 0 { if len(t) != 0 {
r, err := template.New("reg").Parse(t) r, err := template.New("registration").Parse(t)
if err != nil { if err != nil {
c.Templates.Registration = r c.Templates.Registration = r
return true return true
@ -22,7 +60,7 @@ func SetRegistrationTemplate(t string) bool {
log.Fatal("Missing recovery template") log.Fatal("Missing recovery template")
} }
r, err := template.New("reg").Parse(string(df)) r, err := template.New("registration").Parse(string(df))
if err != nil { if err != nil {
log.Fatal("scsusers.SetRegistrationTemplate: Default registration template MUST compile. Error: " + err.Error()) log.Fatal("scsusers.SetRegistrationTemplate: Default registration template MUST compile. Error: " + err.Error())
} }