From 8c1bb58fe245d9cdbd18750e5fd7b8497dbc7d02 Mon Sep 17 00:00:00 2001 From: Jordan Date: Fri, 28 Mar 2014 00:21:42 -0500 Subject: [PATCH] Added validation for campaigns... I'm considering making everything branch off of a Model Interface with Get(), Put(), Post(), and Delete(), etc. Might make things cleaner. Added better error messages on the UI side --- controllers/api.go | 4 ++-- models/campaign.go | 13 +++++++++++-- static/js/app/gophish.js | 15 ++++++++++++++- templates/dashboard.html | 11 ++++++++--- 4 files changed, 35 insertions(+), 8 deletions(-) diff --git a/controllers/api.go b/controllers/api.go index 1b52b275..c63f5eb9 100644 --- a/controllers/api.go +++ b/controllers/api.go @@ -73,8 +73,8 @@ func API_Campaigns(w http.ResponseWriter, r *http.Request) { if checkError(err, w, "Invalid Request", http.StatusBadRequest) { return } - if len(c.Groups) == 0 { - http.Error(w, "Error: No groups specified", http.StatusBadRequest) + if m, ok := models.ValidateCampaign(&c); !ok { + http.Error(w, "Error: "+m, http.StatusBadRequest) return } // Fill in the details diff --git a/models/campaign.go b/models/campaign.go index 99a262a4..9624f735 100644 --- a/models/campaign.go +++ b/models/campaign.go @@ -78,7 +78,6 @@ func PostCampaign(c *Campaign, uid int64) error { for _, t := range g.Targets { r := Result{Email: t.Email, Status: "Unknown", CampaignId: c.Id} c.Results = append(c.Results, r) - fmt.Printf("%v", c.Results) err := db.Save(&r).Error if err != nil { Logger.Printf("Error adding result record for target %s\n", t.Email) @@ -90,7 +89,7 @@ func PostCampaign(c *Campaign, uid int64) error { } func UpdateCampaignStatus(c *Campaign, s string) error { - return db.Debug().Model(c).UpdateColumn("status", s).Error + return db.Model(c).UpdateColumn("status", s).Error } //DeleteCampaign deletes the specified campaign @@ -109,3 +108,13 @@ 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/static/js/app/gophish.js b/static/js/app/gophish.js index 28cc5762..9b185d67 100644 --- a/static/js/app/gophish.js +++ b/static/js/app/gophish.js @@ -30,7 +30,7 @@ app.factory('TemplateService', function($resource) { }); }); -app.controller('CampaignCtrl', function($scope, CampaignService, GroupService, ngTableParams, $http) { +app.controller('CampaignCtrl', function($scope, CampaignService, GroupService, TemplateService, ngTableParams, $http) { $scope.flashes = [] $scope.mainTableParams = new ngTableParams({ page: 1, // show first page @@ -53,6 +53,10 @@ app.controller('CampaignCtrl', function($scope, CampaignService, GroupService, n $scope.groups = groups; }) + TemplateService.query(function(templates) { + $scope.templates = templates; + }) + $scope.addGroup = function() { if ($scope.group.name != "") { $scope.campaign.groups.push({ @@ -91,8 +95,13 @@ app.controller('CampaignCtrl', function($scope, CampaignService, GroupService, n $scope.saveCampaign = function(campaign) { $scope.flashes = [] + $scope.validated = true + /*if (campaign.template.name == "") { + $scope.errorFlash("Must specify a template") + }*/ var newCampaign = new CampaignService(campaign); newCampaign.$save({}, function() { + $scope.successFlash("Campaign added successfully") $scope.campaigns.push(newCampaign); $scope.mainTableParams.reload() }, function(response){ @@ -116,6 +125,10 @@ app.controller('CampaignCtrl', function($scope, CampaignService, GroupService, n $scope.errorFlash = function(message) { $scope.flashes.push({"type" : "danger", "message" : message, "icon" : "fa-exclamation-circle"}) } + + $scope.successFlash = function(message) { + $scope.flashes.push({"type" : "success", "message" : message, "icon" : "fa-check-circle"}) + } }); app.controller('CampaignResultsCtrl', function($scope, CampaignService, GroupService, ngTableParams, $http, $window) { diff --git a/templates/dashboard.html b/templates/dashboard.html index 649e368c..e20f7723 100644 --- a/templates/dashboard.html +++ b/templates/dashboard.html @@ -22,6 +22,11 @@
+
+
+ {{flash.message}} +
+
@@ -62,13 +67,13 @@