diff --git a/models/campaign.go b/models/campaign.go index a4181126..bf4e6f97 100644 --- a/models/campaign.go +++ b/models/campaign.go @@ -1,9 +1,10 @@ package models import ( - "database/sql" "fmt" "time" + + "github.com/jinzhu/gorm" ) //Campaign is a struct representing a created campaign @@ -13,10 +14,24 @@ type Campaign struct { Name string `json:"name" sql:"not null"` CreatedDate time.Time `json:"created_date"` CompletedDate time.Time `json:"completed_date"` + TemplateId int64 `json:"-"` Template Template `json:"template"` //This may change Status string `json:"status"` Results []Result `json:"results,omitempty"` Groups []Group `json:"groups,omitempty"` + SMTP SMTP `json:"options,omitempty"` +} + +func (c *Campaign) Validate() (string, bool) { + switch { + case c.Name == "": + return "Must specify campaign name", false + case len(c.Groups) == 0: + return "No groups specified", false + case c.Template.Name == "": + return "No template specified", false + } + return "", true } type Result struct { @@ -58,7 +73,7 @@ func PostCampaign(c *Campaign, uid int64) error { // Check to make sure all the groups already exist for i, g := range c.Groups { c.Groups[i], err = GetGroupByName(g.Name, uid) - if err == sql.ErrNoRows { + if err == gorm.RecordNotFound { Logger.Printf("Error - Group %s does not exist", g.Name) return err } else if err != nil { @@ -66,6 +81,17 @@ func PostCampaign(c *Campaign, uid int64) error { return err } } + // Check to make sure the template exists + t, err := GetTemplateByName(c.Template.Name, uid) + if err == gorm.RecordNotFound { + Logger.Printf("Error - Template %s does not exist", t.Name) + return err + } else if err != nil { + Logger.Println(err) + return err + } + c.TemplateId = t.Id + // Insert into the DB err = db.Save(c).Error if err != nil { @@ -109,13 +135,3 @@ func DeleteCampaign(id int64) error { } return err } - -func ValidateCampaign(c *Campaign) (string, bool) { - if c.Name == "" { - return "Must specify campaign name", false - } - if len(c.Groups) == 0 { - return "No groups specified", false - } - return "", true -} diff --git a/models/models.go b/models/models.go index 00a3898b..9c5b0bcc 100644 --- a/models/models.go +++ b/models/models.go @@ -56,6 +56,7 @@ func Setup() error { db.CreateTable(Group{}) db.CreateTable(GroupTarget{}) db.CreateTable(Template{}) + db.CreateTable(SMTP{}) db.CreateTable(Campaign{}) //Create the default user init_user := User{ diff --git a/models/smtp.go b/models/smtp.go new file mode 100644 index 00000000..0015bb94 --- /dev/null +++ b/models/smtp.go @@ -0,0 +1,26 @@ +package models + +type SMTP struct { + SMTPId int64 `json:"-"` + CampaignId int64 `json:"-"` + Hostname string `json:"hostname"` + Port int `json:"port"` + UseAuth bool `json:"use_auth"` + Username string `json:"username,omitempty"` + Password string `json:"password,omitempty" sql:"-"` + FromAddress string `json:"from_address"` +} + +func (s *SMTP) Validate() (string, bool) { + switch { + case s.UseAuth == false && (s.Username == "" && s.Password == ""): + return "Auth requested, but username or password blank", false + case s.FromAddress == "": + return "No from address specified", false + case s.Hostname == "": + return "No hostname specified", false + case s.Port == 0: + return "No port specified", false + } + return "", true +} diff --git a/static/js/app/controllers.js b/static/js/app/controllers.js index 194a162e..fdc23d42 100644 --- a/static/js/app/controllers.js +++ b/static/js/app/controllers.js @@ -98,6 +98,7 @@ app.controller('CampaignCtrl', function($scope, $modal, CampaignService, GroupSe deleteCampaign.$delete({ id: deleteCampaign.id }, function() { + $scope.successFlash("Campaign deleted successfully") $scope.mainTableParams.reload(); }); } @@ -157,6 +158,23 @@ app.controller('CampaignResultsCtrl', function($scope, CampaignService, GroupSer }); app.controller('GroupCtrl', function($scope, $modal, GroupService, ngTableParams) { + $scope.errorFlash = function(message) { + $scope.flashes = []; + $scope.flashes.push({ + "type": "danger", + "message": message, + "icon": "fa-exclamation-circle" + }) + } + + $scope.successFlash = function(message) { + $scope.flashes = []; + $scope.flashes.push({ + "type": "success", + "message": message, + "icon": "fa-check-circle" + }) + } $scope.mainTableParams = new ngTableParams({ page: 1, // show first page count: 10, // count per page @@ -254,9 +272,31 @@ var GroupModalCtrl = function($scope, $modalInstance) { $scope.cancel = function() { $modalInstance.dismiss('cancel'); }; + $scope.ok = function(group) { + $modalInstance.dismiss('') + $scope.saveGroup(group) + }; } app.controller('TemplateCtrl', function($scope, $modal, TemplateService, ngTableParams) { + $scope.errorFlash = function(message) { + $scope.flashes = []; + $scope.flashes.push({ + "type": "danger", + "message": message, + "icon": "fa-exclamation-circle" + }) + } + + $scope.successFlash = function(message) { + $scope.flashes = []; + $scope.flashes.push({ + "type": "success", + "message": message, + "icon": "fa-check-circle" + }) + } + $scope.mainTableParams = new ngTableParams({ page: 1, // show first page count: 10, // count per page @@ -322,7 +362,12 @@ app.controller('TemplateCtrl', function($scope, $modal, TemplateService, ngTable var deleteTemplate = new TemplateService(template); deleteTemplate.$delete({ id: deleteTemplate.id - }, function() { + }, function(response) { + if (response.success) { + $scope.successFlash(response.message) + } else { + $scope.errorFlash(response.message) + } $scope.mainTableParams.reload(); }); } @@ -367,10 +412,12 @@ app.controller('SettingsCtrl', function($scope, $http, $window) { 'Content-Type': 'application/x-www-form-urlencoded' } // set the headers so angular passing info as form data (not request payload) }) - .success(function(data) { - $scope.user.api_key = data; - $window.user.api_key = data; - $scope.successFlash("API Key Successfully Reset") + .success(function(response) { + if (response.success) { + $scope.user.api_key = response.data; + $window.user.api_key = response.data; + $scope.successFlash(response.message) + } }) } $scope.save_settings = function(){ diff --git a/static/js/app/partials/modals/campaignModal.html b/static/js/app/partials/modals/campaignModal.html index 1b55d394..70d20d75 100644 --- a/static/js/app/partials/modals/campaignModal.html +++ b/static/js/app/partials/modals/campaignModal.html @@ -9,14 +9,30 @@
- -
- - -
- - +
+ + + + SMTP Options + + + +
+ + +
+ + +
+ + +
+ + +
+
+
diff --git a/static/js/app/partials/modals/userModal.html b/static/js/app/partials/modals/userModal.html index ccc41a64..4211f22c 100644 --- a/static/js/app/partials/modals/userModal.html +++ b/static/js/app/partials/modals/userModal.html @@ -38,6 +38,6 @@