2014-03-25 03:31:33 +00:00
|
|
|
package models
|
|
|
|
|
2014-07-06 18:06:18 +00:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"time"
|
2014-08-07 10:48:52 +00:00
|
|
|
|
2018-05-04 00:07:41 +00:00
|
|
|
log "github.com/gophish/gophish/logger"
|
2014-08-07 10:48:52 +00:00
|
|
|
"github.com/jinzhu/gorm"
|
2014-07-06 18:06:18 +00:00
|
|
|
)
|
2014-03-25 03:31:33 +00:00
|
|
|
|
2015-02-07 20:31:41 +00:00
|
|
|
// Template models hold the attributes for an email template to be sent to targets
|
2014-03-25 03:31:33 +00:00
|
|
|
type Template struct {
|
2015-02-07 20:31:41 +00:00
|
|
|
Id int64 `json:"id" gorm:"column:id; primary_key:yes"`
|
|
|
|
UserId int64 `json:"-" gorm:"column:user_id"`
|
2014-07-12 18:46:38 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
Subject string `json:"subject"`
|
|
|
|
Text string `json:"text"`
|
2015-02-07 20:31:41 +00:00
|
|
|
HTML string `json:"html" gorm:"column:html"`
|
2014-07-12 18:46:38 +00:00
|
|
|
ModifiedDate time.Time `json:"modified_date"`
|
|
|
|
Attachments []Attachment `json:"attachments"`
|
2014-03-25 03:31:33 +00:00
|
|
|
}
|
|
|
|
|
2015-02-07 20:31:41 +00:00
|
|
|
// ErrTemplateNameNotSpecified is thrown when a template name is not specified
|
2015-02-21 06:11:22 +00:00
|
|
|
var ErrTemplateNameNotSpecified = errors.New("Template name not specified")
|
2015-02-07 20:31:41 +00:00
|
|
|
|
|
|
|
// ErrTemplateMissingParameter is thrown when a needed parameter is not provided
|
2015-02-18 02:49:09 +00:00
|
|
|
var ErrTemplateMissingParameter = errors.New("Need to specify at least plaintext or HTML content")
|
2014-07-06 18:06:18 +00:00
|
|
|
|
2015-02-07 20:31:41 +00:00
|
|
|
// Validate checks the given template to make sure values are appropriate and complete
|
2014-07-06 18:06:18 +00:00
|
|
|
func (t *Template) Validate() error {
|
2014-06-02 06:57:04 +00:00
|
|
|
switch {
|
|
|
|
case t.Name == "":
|
2014-07-06 18:06:18 +00:00
|
|
|
return ErrTemplateNameNotSpecified
|
2014-06-05 04:54:46 +00:00
|
|
|
case t.Text == "" && t.HTML == "":
|
2014-07-06 18:06:18 +00:00
|
|
|
return ErrTemplateMissingParameter
|
2014-06-02 06:57:04 +00:00
|
|
|
}
|
2018-12-15 21:42:32 +00:00
|
|
|
if err := ValidateTemplate(t.HTML); err != nil {
|
2018-09-08 23:04:21 +00:00
|
|
|
return err
|
2016-04-02 23:33:25 +00:00
|
|
|
}
|
2018-12-15 21:42:32 +00:00
|
|
|
if err := ValidateTemplate(t.Text); err != nil {
|
2016-04-02 23:33:25 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-08-16 21:34:13 +00:00
|
|
|
for _, a := range t.Attachments {
|
|
|
|
if err := a.ValidateAttachment(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-09 02:20:52 +00:00
|
|
|
return nil
|
2014-06-02 06:57:04 +00:00
|
|
|
}
|
|
|
|
|
2014-03-25 03:31:33 +00:00
|
|
|
// GetTemplates returns the templates owned by the given user.
|
|
|
|
func GetTemplates(uid int64) ([]Template, error) {
|
|
|
|
ts := []Template{}
|
2014-05-28 23:48:30 +00:00
|
|
|
err := db.Where("user_id=?", uid).Find(&ts).Error
|
|
|
|
if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Error(err)
|
2014-05-28 23:48:30 +00:00
|
|
|
return ts, err
|
|
|
|
}
|
2018-12-16 03:38:51 +00:00
|
|
|
for i := range ts {
|
2017-02-20 00:43:08 +00:00
|
|
|
// Get Attachments
|
2014-07-12 18:46:38 +00:00
|
|
|
err = db.Where("template_id=?", ts[i].Id).Find(&ts[i].Attachments).Error
|
2015-02-14 02:17:25 +00:00
|
|
|
if err == nil && len(ts[i].Attachments) == 0 {
|
|
|
|
ts[i].Attachments = make([]Attachment, 0)
|
|
|
|
}
|
2016-03-09 04:37:55 +00:00
|
|
|
if err != nil && err != gorm.ErrRecordNotFound {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Error(err)
|
2014-07-12 18:46:38 +00:00
|
|
|
return ts, err
|
|
|
|
}
|
|
|
|
}
|
2014-03-25 03:31:33 +00:00
|
|
|
return ts, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTemplate returns the template, if it exists, specified by the given id and user_id.
|
|
|
|
func GetTemplate(id int64, uid int64) (Template, error) {
|
|
|
|
t := Template{}
|
2014-05-28 23:48:30 +00:00
|
|
|
err := db.Where("user_id=? and id=?", uid, id).Find(&t).Error
|
|
|
|
if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Error(err)
|
2014-05-28 23:48:30 +00:00
|
|
|
return t, err
|
|
|
|
}
|
2017-02-20 00:43:08 +00:00
|
|
|
|
|
|
|
// Get Attachments
|
2014-07-12 18:46:38 +00:00
|
|
|
err = db.Where("template_id=?", t.Id).Find(&t.Attachments).Error
|
2016-03-09 04:37:55 +00:00
|
|
|
if err != nil && err != gorm.ErrRecordNotFound {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Error(err)
|
2014-07-12 18:46:38 +00:00
|
|
|
return t, err
|
|
|
|
}
|
2015-02-14 02:17:25 +00:00
|
|
|
if err == nil && len(t.Attachments) == 0 {
|
|
|
|
t.Attachments = make([]Attachment, 0)
|
2014-08-07 10:48:52 +00:00
|
|
|
}
|
2014-03-25 03:31:33 +00:00
|
|
|
return t, err
|
|
|
|
}
|
|
|
|
|
2014-06-02 04:38:21 +00:00
|
|
|
// GetTemplateByName returns the template, if it exists, specified by the given name and user_id.
|
|
|
|
func GetTemplateByName(n string, uid int64) (Template, error) {
|
|
|
|
t := Template{}
|
|
|
|
err := db.Where("user_id=? and name=?", uid, n).Find(&t).Error
|
|
|
|
if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Error(err)
|
2016-02-15 17:58:14 +00:00
|
|
|
return t, err
|
2014-06-02 04:38:21 +00:00
|
|
|
}
|
2017-02-20 00:43:08 +00:00
|
|
|
|
|
|
|
// Get Attachments
|
2016-02-13 04:19:59 +00:00
|
|
|
err = db.Where("template_id=?", t.Id).Find(&t.Attachments).Error
|
2016-03-09 04:37:55 +00:00
|
|
|
if err != nil && err != gorm.ErrRecordNotFound {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Error(err)
|
2016-02-13 04:19:59 +00:00
|
|
|
return t, err
|
|
|
|
}
|
|
|
|
if err == nil && len(t.Attachments) == 0 {
|
|
|
|
t.Attachments = make([]Attachment, 0)
|
|
|
|
}
|
2015-02-07 20:31:41 +00:00
|
|
|
return t, err
|
2014-06-02 04:38:21 +00:00
|
|
|
}
|
|
|
|
|
2014-03-25 03:31:33 +00:00
|
|
|
// PostTemplate creates a new template in the database.
|
2014-05-28 23:48:30 +00:00
|
|
|
func PostTemplate(t *Template) error {
|
2014-03-25 03:31:33 +00:00
|
|
|
// Insert into the DB
|
2015-02-18 02:49:09 +00:00
|
|
|
if err := t.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-15 21:42:32 +00:00
|
|
|
err := db.Save(t).Error
|
2014-03-25 03:31:33 +00:00
|
|
|
if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Error(err)
|
2014-03-25 03:31:33 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-02-20 00:43:08 +00:00
|
|
|
|
|
|
|
// Save every attachment
|
2018-05-04 00:07:41 +00:00
|
|
|
for i := range t.Attachments {
|
2014-07-24 02:04:38 +00:00
|
|
|
t.Attachments[i].TemplateId = t.Id
|
|
|
|
err := db.Save(&t.Attachments[i]).Error
|
|
|
|
if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Error(err)
|
2014-07-24 02:04:38 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2014-03-25 03:31:33 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-06-02 04:38:21 +00:00
|
|
|
// PutTemplate edits an existing template in the database.
|
|
|
|
// Per the PUT Method RFC, it presumes all data for a template is provided.
|
2014-07-06 18:06:18 +00:00
|
|
|
func PutTemplate(t *Template) error {
|
2015-02-21 06:11:22 +00:00
|
|
|
if err := t.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-07-24 02:04:38 +00:00
|
|
|
// Delete all attachments, and replace with new ones
|
2018-12-15 21:42:32 +00:00
|
|
|
err := db.Where("template_id=?", t.Id).Delete(&Attachment{}).Error
|
2016-03-09 04:37:55 +00:00
|
|
|
if err != nil && err != gorm.ErrRecordNotFound {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Error(err)
|
2014-07-24 02:04:38 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-03-09 04:37:55 +00:00
|
|
|
if err == gorm.ErrRecordNotFound {
|
2014-08-07 10:48:52 +00:00
|
|
|
err = nil
|
|
|
|
}
|
2018-12-15 21:42:32 +00:00
|
|
|
for i := range t.Attachments {
|
2014-07-24 02:04:38 +00:00
|
|
|
t.Attachments[i].TemplateId = t.Id
|
|
|
|
err := db.Save(&t.Attachments[i]).Error
|
|
|
|
if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Error(err)
|
2014-07-24 02:04:38 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-02-20 00:43:08 +00:00
|
|
|
|
|
|
|
// Save final template
|
2015-02-21 06:11:22 +00:00
|
|
|
err = db.Where("id=?", t.Id).Save(t).Error
|
2014-07-06 18:06:18 +00:00
|
|
|
if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Error(err)
|
2014-07-06 18:06:18 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-03-25 03:31:33 +00:00
|
|
|
return nil
|
|
|
|
}
|
2014-05-28 23:48:30 +00:00
|
|
|
|
2014-06-02 04:38:21 +00:00
|
|
|
// DeleteTemplate deletes an existing template in the database.
|
|
|
|
// An error is returned if a template with the given user id and template id is not found.
|
2014-05-28 23:48:30 +00:00
|
|
|
func DeleteTemplate(id int64, uid int64) error {
|
2017-02-20 00:43:08 +00:00
|
|
|
// Delete attachments
|
2014-07-24 02:04:38 +00:00
|
|
|
err := db.Where("template_id=?", id).Delete(&Attachment{}).Error
|
|
|
|
if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Error(err)
|
2014-07-24 02:04:38 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-02-20 00:43:08 +00:00
|
|
|
|
|
|
|
// Finally, delete the template itself
|
2014-07-24 02:04:38 +00:00
|
|
|
err = db.Where("user_id=?", uid).Delete(Template{Id: id}).Error
|
2014-05-28 23:48:30 +00:00
|
|
|
if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Error(err)
|
2014-05-28 23:48:30 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|