github.com/amar224/phishing-tool@v0.9.0/models/webhook.go (about)

     1  package models
     2  
     3  import (
     4  	"errors"
     5  
     6  	log "github.com/gophish/gophish/logger"
     7  )
     8  
     9  // Webhook represents the webhook model
    10  type Webhook struct {
    11  	Id       int64  `json:"id" gorm:"column:id; primary_key:yes"`
    12  	Name     string `json:"name"`
    13  	URL      string `json:"url"`
    14  	Secret   string `json:"secret"`
    15  	IsActive bool   `json:"is_active"`
    16  }
    17  
    18  // ErrURLNotSpecified indicates there was no URL specified
    19  var ErrURLNotSpecified = errors.New("URL can't be empty")
    20  
    21  // ErrNameNotSpecified indicates there was no name specified
    22  var ErrNameNotSpecified = errors.New("Name can't be empty")
    23  
    24  // GetWebhooks returns the webhooks
    25  func GetWebhooks() ([]Webhook, error) {
    26  	whs := []Webhook{}
    27  	err := db.Find(&whs).Error
    28  	return whs, err
    29  }
    30  
    31  // GetActiveWebhooks returns the active webhooks
    32  func GetActiveWebhooks() ([]Webhook, error) {
    33  	whs := []Webhook{}
    34  	err := db.Where("is_active=?", true).Find(&whs).Error
    35  	return whs, err
    36  }
    37  
    38  // GetWebhook returns the webhook that the given id corresponds to.
    39  // If no webhook is found, an error is returned.
    40  func GetWebhook(id int64) (Webhook, error) {
    41  	wh := Webhook{}
    42  	err := db.Where("id=?", id).First(&wh).Error
    43  	return wh, err
    44  }
    45  
    46  // PostWebhook creates a new webhook in the database.
    47  func PostWebhook(wh *Webhook) error {
    48  	err := wh.Validate()
    49  	if err != nil {
    50  		log.Error(err)
    51  		return err
    52  	}
    53  	err = db.Save(wh).Error
    54  	if err != nil {
    55  		log.Error(err)
    56  	}
    57  	return err
    58  }
    59  
    60  // PutWebhook edits an existing webhook in the database.
    61  func PutWebhook(wh *Webhook) error {
    62  	err := wh.Validate()
    63  	if err != nil {
    64  		log.Error(err)
    65  		return err
    66  	}
    67  	err = db.Save(wh).Error
    68  	return err
    69  }
    70  
    71  // DeleteWebhook deletes an existing webhook in the database.
    72  // An error is returned if a webhook with the given id isn't found.
    73  func DeleteWebhook(id int64) error {
    74  	err := db.Where("id=?", id).Delete(&Webhook{}).Error
    75  	return err
    76  }
    77  
    78  func (wh *Webhook) Validate() error {
    79  	if wh.URL == "" {
    80  		return ErrURLNotSpecified
    81  	}
    82  	if wh.Name == "" {
    83  		return ErrNameNotSpecified
    84  	}
    85  	return nil
    86  }