diff --git a/controllers/api.go b/controllers/api.go index 3858b3a9..0cd1ef2f 100644 --- a/controllers/api.go +++ b/controllers/api.go @@ -73,6 +73,10 @@ 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) + return + } // Fill in the details c.CreatedDate = time.Now() c.CompletedDate = time.Time{} diff --git a/db/db.go b/db/db.go index 017b8a21..a8749b98 100644 --- a/db/db.go +++ b/db/db.go @@ -124,13 +124,7 @@ func GetCampaign(id int64, uid int64) (models.Campaign, error) { // PostCampaign inserts a campaign and all associated records into the database. func PostCampaign(c *models.Campaign, uid int64) error { - // Insert into the DB - err = Conn.Insert(c) - if err != nil { - Logger.Println(err) - return err - } - // Insert all the results + // 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 { @@ -140,8 +134,17 @@ func PostCampaign(c *models.Campaign, uid int64) error { Logger.Println(err) return err } + } + // Insert into the DB + err = Conn.Insert(c) + if err != nil { + Logger.Println(err) + return err + } + // Insert all the results + for _, g := range c.Groups { // Insert a result for each target in the group - for _, t := range c.Groups[i].Targets { + for _, t := range g.Targets { r := models.Result{Target: t, Status: "Unknown"} c.Results = append(c.Results, r) fmt.Printf("%v", c.Results) diff --git a/static/js/app/gophish.js b/static/js/app/gophish.js index fce746c7..4c9811d3 100644 --- a/static/js/app/gophish.js +++ b/static/js/app/gophish.js @@ -21,6 +21,7 @@ app.factory('GroupService', function($resource) { }); app.controller('CampaignCtrl', function($scope, CampaignService, ngTableParams) { + $scope.flashes = [] $scope.mainTableParams = new ngTableParams({ page: 1, // show first page count: 10, // count per page @@ -89,14 +90,16 @@ app.controller('CampaignCtrl', function($scope, CampaignService, ngTableParams) }; $scope.saveCampaign = function(campaign) { + $scope.flashes = [] var newCampaign = new CampaignService(campaign); newCampaign.$save({}, function() { $scope.campaigns.push(newCampaign); $scope.mainTableParams.reload() + }, function(response){ + $scope.errorFlash(response.data) }); - $scope.group = { - name: '', - targets: [], + $scope.campaign = { + groups: [], }; $scope.editGroupTableParams.reload() } @@ -109,6 +112,10 @@ app.controller('CampaignCtrl', function($scope, CampaignService, ngTableParams) $scope.mainTableParams.reload(); }); } + + $scope.errorFlash = function(message) { + $scope.flashes.push({"type" : "danger", "message" : message, "icon" : "fa-exclamation-circle"}) + } }); app.controller('GroupCtrl', function($scope, GroupService, ngTableParams) { diff --git a/templates/dashboard.html b/templates/dashboard.html index 2dcb29d0..fd3d8450 100644 --- a/templates/dashboard.html +++ b/templates/dashboard.html @@ -60,6 +60,9 @@