github.com/Ne0nd0g/gophish@v0.7.1-0.20190220040016-11493024a07d/models/template.go (about)

     1  package models
     2  
     3  import (
     4  	"errors"
     5  	"time"
     6  
     7  	log "github.com/gophish/gophish/logger"
     8  	"github.com/jinzhu/gorm"
     9  )
    10  
    11  // Template models hold the attributes for an email template to be sent to targets
    12  type Template struct {
    13  	Id           int64        `json:"id" gorm:"column:id; primary_key:yes"`
    14  	UserId       int64        `json:"-" gorm:"column:user_id"`
    15  	Name         string       `json:"name"`
    16  	Subject      string       `json:"subject"`
    17  	Text         string       `json:"text"`
    18  	HTML         string       `json:"html" gorm:"column:html"`
    19  	ModifiedDate time.Time    `json:"modified_date"`
    20  	Attachments  []Attachment `json:"attachments"`
    21  }
    22  
    23  // ErrTemplateNameNotSpecified is thrown when a template name is not specified
    24  var ErrTemplateNameNotSpecified = errors.New("Template name not specified")
    25  
    26  // ErrTemplateMissingParameter is thrown when a needed parameter is not provided
    27  var ErrTemplateMissingParameter = errors.New("Need to specify at least plaintext or HTML content")
    28  
    29  // Validate checks the given template to make sure values are appropriate and complete
    30  func (t *Template) Validate() error {
    31  	switch {
    32  	case t.Name == "":
    33  		return ErrTemplateNameNotSpecified
    34  	case t.Text == "" && t.HTML == "":
    35  		return ErrTemplateMissingParameter
    36  	}
    37  	if err := ValidateTemplate(t.HTML); err != nil {
    38  		return err
    39  	}
    40  	if err := ValidateTemplate(t.Text); err != nil {
    41  		return err
    42  	}
    43  	return nil
    44  }
    45  
    46  // GetTemplates returns the templates owned by the given user.
    47  func GetTemplates(uid int64) ([]Template, error) {
    48  	ts := []Template{}
    49  	err := db.Where("user_id=?", uid).Find(&ts).Error
    50  	if err != nil {
    51  		log.Error(err)
    52  		return ts, err
    53  	}
    54  	for i := range ts {
    55  		// Get Attachments
    56  		err = db.Where("template_id=?", ts[i].Id).Find(&ts[i].Attachments).Error
    57  		if err == nil && len(ts[i].Attachments) == 0 {
    58  			ts[i].Attachments = make([]Attachment, 0)
    59  		}
    60  		if err != nil && err != gorm.ErrRecordNotFound {
    61  			log.Error(err)
    62  			return ts, err
    63  		}
    64  	}
    65  	return ts, err
    66  }
    67  
    68  // GetTemplate returns the template, if it exists, specified by the given id and user_id.
    69  func GetTemplate(id int64, uid int64) (Template, error) {
    70  	t := Template{}
    71  	err := db.Where("user_id=? and id=?", uid, id).Find(&t).Error
    72  	if err != nil {
    73  		log.Error(err)
    74  		return t, err
    75  	}
    76  
    77  	// Get Attachments
    78  	err = db.Where("template_id=?", t.Id).Find(&t.Attachments).Error
    79  	if err != nil && err != gorm.ErrRecordNotFound {
    80  		log.Error(err)
    81  		return t, err
    82  	}
    83  	if err == nil && len(t.Attachments) == 0 {
    84  		t.Attachments = make([]Attachment, 0)
    85  	}
    86  	return t, err
    87  }
    88  
    89  // GetTemplateByName returns the template, if it exists, specified by the given name and user_id.
    90  func GetTemplateByName(n string, uid int64) (Template, error) {
    91  	t := Template{}
    92  	err := db.Where("user_id=? and name=?", uid, n).Find(&t).Error
    93  	if err != nil {
    94  		log.Error(err)
    95  		return t, err
    96  	}
    97  
    98  	// Get Attachments
    99  	err = db.Where("template_id=?", t.Id).Find(&t.Attachments).Error
   100  	if err != nil && err != gorm.ErrRecordNotFound {
   101  		log.Error(err)
   102  		return t, err
   103  	}
   104  	if err == nil && len(t.Attachments) == 0 {
   105  		t.Attachments = make([]Attachment, 0)
   106  	}
   107  	return t, err
   108  }
   109  
   110  // PostTemplate creates a new template in the database.
   111  func PostTemplate(t *Template) error {
   112  	// Insert into the DB
   113  	if err := t.Validate(); err != nil {
   114  		return err
   115  	}
   116  	err := db.Save(t).Error
   117  	if err != nil {
   118  		log.Error(err)
   119  		return err
   120  	}
   121  
   122  	// Save every attachment
   123  	for i := range t.Attachments {
   124  		t.Attachments[i].TemplateId = t.Id
   125  		err := db.Save(&t.Attachments[i]).Error
   126  		if err != nil {
   127  			log.Error(err)
   128  			return err
   129  		}
   130  	}
   131  	return nil
   132  }
   133  
   134  // PutTemplate edits an existing template in the database.
   135  // Per the PUT Method RFC, it presumes all data for a template is provided.
   136  func PutTemplate(t *Template) error {
   137  	if err := t.Validate(); err != nil {
   138  		return err
   139  	}
   140  	// Delete all attachments, and replace with new ones
   141  	err := db.Where("template_id=?", t.Id).Delete(&Attachment{}).Error
   142  	if err != nil && err != gorm.ErrRecordNotFound {
   143  		log.Error(err)
   144  		return err
   145  	}
   146  	if err == gorm.ErrRecordNotFound {
   147  		err = nil
   148  	}
   149  	for i := range t.Attachments {
   150  		t.Attachments[i].TemplateId = t.Id
   151  		err := db.Save(&t.Attachments[i]).Error
   152  		if err != nil {
   153  			log.Error(err)
   154  			return err
   155  		}
   156  	}
   157  
   158  	// Save final template
   159  	err = db.Where("id=?", t.Id).Save(t).Error
   160  	if err != nil {
   161  		log.Error(err)
   162  		return err
   163  	}
   164  	return nil
   165  }
   166  
   167  // DeleteTemplate deletes an existing template in the database.
   168  // An error is returned if a template with the given user id and template id is not found.
   169  func DeleteTemplate(id int64, uid int64) error {
   170  	// Delete attachments
   171  	err := db.Where("template_id=?", id).Delete(&Attachment{}).Error
   172  	if err != nil {
   173  		log.Error(err)
   174  		return err
   175  	}
   176  
   177  	// Finally, delete the template itself
   178  	err = db.Where("user_id=?", uid).Delete(Template{Id: id}).Error
   179  	if err != nil {
   180  		log.Error(err)
   181  		return err
   182  	}
   183  	return nil
   184  }