From d2aca2b7f8ce970587e3c92d697421d0228a3323 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?B=C3=A1lint=20J=C3=B3zsef=20J=C3=A1nv=C3=A1ri?=
<4534880+dzsibi@users.noreply.github.com>
Date: Tue, 4 Jun 2019 04:58:59 +0200
Subject: [PATCH] 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
---
models/template_context.go | 10 +++++++++-
models/template_context_test.go | 1 +
2 files changed, 10 insertions(+), 1 deletion(-)
diff --git a/models/template_context.go b/models/template_context.go
index 96e03f3e..af0cae91 100644
--- a/models/template_context.go
+++ b/models/template_context.go
@@ -6,6 +6,7 @@ import (
"net/url"
"path"
"text/template"
+ "time"
)
// TemplateContext is an interface that allows both campaigns and email
@@ -24,6 +25,7 @@ type PhishingTemplateContext struct {
TrackingURL string
RId string
BaseURL string
+ Now time.Time
BaseRecipient
}
@@ -66,6 +68,7 @@ func NewPhishingTemplateContext(ctx TemplateContext, r BaseRecipient, rid string
Tracker: "",
From: fn,
RId: rid,
+ Now: time.Now(),
}, nil
}
@@ -73,7 +76,12 @@ func NewPhishingTemplateContext(ctx TemplateContext, r BaseRecipient, rid string
// template body and data.
func ExecuteTemplate(text string, data interface{}) (string, error) {
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 {
return buff.String(), err
}
diff --git a/models/template_context_test.go b/models/template_context_test.go
index 0b6ac4e1..018df601 100644
--- a/models/template_context_test.go
+++ b/models/template_context_test.go
@@ -43,5 +43,6 @@ func (s *ModelsSuite) TestNewTemplateContext(c *check.C) {
expected.Tracker = ""
got, err := NewPhishingTemplateContext(ctx, r.BaseRecipient, r.RId)
c.Assert(err, check.Equals, nil)
+ expected.Now = got.Now
c.Assert(got, check.DeepEquals, expected)
}