Add date parsing and manipulation for e-mail templates (#1451)

* Add date parsing and manipulation for e-mail templates

* Add duration parser to date handling in e-mail templates

* Correct whitespace usage in template_context.go
1451-template-dates
Bálint József Jánvári 2019-06-04 04:58:59 +02:00 committed by Jordan Wright
parent a1a2de13a4
commit d2aca2b7f8
2 changed files with 10 additions and 1 deletions

View File

@ -6,6 +6,7 @@ import (
"net/url" "net/url"
"path" "path"
"text/template" "text/template"
"time"
) )
// TemplateContext is an interface that allows both campaigns and email // TemplateContext is an interface that allows both campaigns and email
@ -24,6 +25,7 @@ type PhishingTemplateContext struct {
TrackingURL string TrackingURL string
RId string RId string
BaseURL string BaseURL string
Now time.Time
BaseRecipient BaseRecipient
} }
@ -66,6 +68,7 @@ func NewPhishingTemplateContext(ctx TemplateContext, r BaseRecipient, rid string
Tracker: "<img alt='' style='display: none' src='" + trackingURL.String() + "'/>", Tracker: "<img alt='' style='display: none' src='" + trackingURL.String() + "'/>",
From: fn, From: fn,
RId: rid, RId: rid,
Now: time.Now(),
}, nil }, nil
} }
@ -73,7 +76,12 @@ func NewPhishingTemplateContext(ctx TemplateContext, r BaseRecipient, rid string
// template body and data. // template body and data.
func ExecuteTemplate(text string, data interface{}) (string, error) { func ExecuteTemplate(text string, data interface{}) (string, error) {
buff := bytes.Buffer{} buff := bytes.Buffer{}
tmpl, err := template.New("template").Parse(text) funcMap := template.FuncMap{
"date": time.Parse,
"duration": time.ParseDuration,
"location": time.LoadLocation,
}
tmpl, err := template.New("template").Funcs(funcMap).Parse(text)
if err != nil { if err != nil {
return buff.String(), err return buff.String(), err
} }

View File

@ -43,5 +43,6 @@ func (s *ModelsSuite) TestNewTemplateContext(c *check.C) {
expected.Tracker = "<img alt='' style='display: none' src='" + expected.TrackingURL + "'/>" expected.Tracker = "<img alt='' style='display: none' src='" + expected.TrackingURL + "'/>"
got, err := NewPhishingTemplateContext(ctx, r.BaseRecipient, r.RId) got, err := NewPhishingTemplateContext(ctx, r.BaseRecipient, r.RId)
c.Assert(err, check.Equals, nil) c.Assert(err, check.Equals, nil)
expected.Now = got.Now
c.Assert(got, check.DeepEquals, expected) c.Assert(got, check.DeepEquals, expected)
} }