diff --git a/controllers/api.go b/controllers/api.go index 5acfb83b..a4fdcc8f 100644 --- a/controllers/api.go +++ b/controllers/api.go @@ -176,7 +176,7 @@ func API_Groups_Id(w http.ResponseWriter, r *http.Request) { g.UserId = ctx.Get(r, "user_id").(int64) err = models.PutGroup(&g) if err != nil { - JSONResponse(w, models.Response{Success: false, Message: "Error updating group"}, http.StatusInternalServerError) + JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusBadRequest) return } JSONResponse(w, g, http.StatusOK) diff --git a/models/group.go b/models/group.go index 2d7da33a..070325e5 100644 --- a/models/group.go +++ b/models/group.go @@ -117,6 +117,9 @@ func PostGroup(g *Group) error { // PutGroup updates the given group if found in the database. func PutGroup(g *Group) error { + if err := g.Validate(); err != nil { + return err + } ts := []Target{} ts, err = GetTargets(g.Id) if err != nil { diff --git a/static/js/app/users.js b/static/js/app/users.js index d74d81d0..0ad1a1e7 100644 --- a/static/js/app/users.js +++ b/static/js/app/users.js @@ -1,5 +1,7 @@ -// Save attempts to POST to /groups/ -function save(){ +var groups = [] + +// Save attempts to POST or PUT to /groups/ +function save(idx){ var targets = [] $.each($("#targetsTable").DataTable().rows().data(), function(i, target){ targets.push({ @@ -13,31 +15,62 @@ function save(){ name: $("#name").val(), targets: targets } - console.log(group) // Submit the group - api.groups.post(group) - .success(function(data){ - successFlash("Group added successfully!") - load() - dismiss() - }) - .error(function(data){ - modalError(data.responseJSON.message) - }) + if (idx != -1) { + // If we're just editing an existing group, + // we need to PUT /groups/:id + group.id = groups[idx].id + api.groupId.put(group) + .success(function(data){ + successFlash("Group updated successfully!") + load() + dismiss() + }) + .error(function(data){ + modalError(data.responseJSON.message) + }) + } else { + // Else, if this is a new group, POST it + // to /groups + api.groups.post(group) + .success(function(data){ + successFlash("Group added successfully!") + load() + dismiss() + }) + .error(function(data){ + modalError(data.responseJSON.message) + }) + } } function dismiss(){ $("#targetsTable").dataTable().DataTable().clear().draw() + $("#name").val("") $("#modal\\.flashes").empty() $("#modal").modal('hide') } -function edit(group){ - if (group == "new") { +function edit(idx){ + targets = $("#targetsTable").dataTable() + $("#modalSubmit").unbind('click').click(function(){save(idx)}) + if (idx == -1) { group = {} + } else { + group = groups[idx] + $("#name").val(group.name) + $.each(group.targets, function(i, record) { + targets.DataTable() + .row.add([ + record.first_name, + record.last_name, + record.email, + record.position, + '' + ]).draw() + }); } // Handle file uploads - targets = $("#targetsTable").dataTable() $("#csvupload").fileupload({ dataType:"json", add: function(e, data){ @@ -51,7 +84,6 @@ function edit(group){ data.submit(); }, done: function(e, data){ - console.log(data.result) $.each(data.result, function(i, record) { targets.DataTable() .row.add([ @@ -64,6 +96,59 @@ function edit(group){ }); } }) +} + +function deleteGroup(idx){ + if (confirm("Delete " + groups[idx].name + "?")){ + api.groupId.delete(groups[idx].id) + .success(function(data){ + successFlash(data.message) + load() + }) + } +} + +function load(){ + api.groups.get() + .success(function(gs){ + if (gs.length > 0){ + groups = gs + $("#loading").hide() + $("#groupTable").show() + groupTable = $("#groupTable").DataTable(); + groupTable.clear(); + $.each(groups, function(i, group){ + var targets = "" + $.each(group.targets, function(i, target){ + targets += target.email + ", " + if (targets.length > 50) { + targets = targets.slice(0,-3) + "..." + return false; + } + }) + groupTable.row.add([ + group.name, + targets, + moment(group.modified_date).format('MMMM Do YYYY, h:mm:ss a'), + "
\ +
" + ]).draw() + }) + } + }) + .error(function(){ + errorFlash("Error fetching groups") + }) +} + +$(document).ready(function(){ + load() + $("#fileUpload").hover(function(){$("#fileUpload").tooltip('toggle')}) + // Setup the event listeners // Handle manual additions $("#targetForm").submit(function(){ targets.DataTable() @@ -86,44 +171,4 @@ function edit(group){ .remove() .draw(); }) -} - -function load(){ - api.groups.get() - .success(function(groups){ - if (groups.length > 0){ - $("#emptyMessage").hide() - $("#groupTable").show() - groupTable = $("#groupTable").DataTable(); - $.each(groups, function(i, group){ - var targets = "" - $.each(group.targets, function(i, target){ - targets += target.email + ", " - if (targets.length > 50) { - targets = targets.slice(0,-3) + "..." - return false; - } - }) - groupTable.row.add([ - group.name, - targets, - moment(group.modified_date).format('MMMM Do YYYY, h:mm:ss a'), - "
\ -
" - ]).draw() - }) - } - }) - .error(function(){ - errorFlash("Error fetching groups") - }) -} - -$(document).ready(function(){ - load() - $("#fileUpload").hover(function(){$("#fileUpload").tooltip('toggle')}) }) diff --git a/static/js/gophish.js b/static/js/gophish.js index 99186160..9103cf9d 100644 --- a/static/js/gophish.js +++ b/static/js/gophish.js @@ -1,10 +1,12 @@ function errorFlash(message) { + $("#flashes").empty() $("#flashes").append("
\ " + message + "
" ) } function successFlash(message) { + $("#flashes").empty() $("#flashes").append("
\ " + message + "
" ) @@ -47,14 +49,6 @@ var api = { get: function(id){ return query("/campaigns/" + id, "GET", {}) }, - // post() - Posts a campaign to POST /campaigns/:id - post: function(campaign){ - return query("/campaigns/" + campaign.id, "POST", data) - }, - // put() - Puts a campaign to PUT /campaigns/:id - put: function (campaign){ - return query("/campaigns/" + campaign.id, "PUT", data) - }, // delete() - Deletes a campaign at DELETE /campaigns/:id delete: function(id){ return query("/campaigns/" + id, "DELETE", data) @@ -77,17 +71,13 @@ var api = { get: function(id){ return query("/groups/" + id, "GET", {}) }, - // post() - Posts a campaign to POST /groups/:id - post: function(group){ - return query("/groups/" + group.id, "POST", data) - }, // put() - Puts a campaign to PUT /groups/:id put: function (group){ - return query("/groups/" + group.id, "PUT", data) + return query("/groups/" + group.id, "PUT", group) }, // delete() - Deletes a campaign at DELETE /groups/:id delete: function(id){ - return query("/groups/" + id, "DELETE", data) + return query("/groups/" + id, "DELETE", {}) } }, // templates contains the endpoints for /templates @@ -107,17 +97,13 @@ var api = { get: function(id){ return query("/templates/" + id, "GET", {}) }, - // post() - Posts a campaign to POST /templates/:id - post: function(template){ - return query("/templates/" + template.id, "POST", data) - }, // put() - Puts a campaign to PUT /templates/:id put: function (template){ - return query("/templates/" + template.id, "PUT", data) + return query("/templates/" + template.id, "PUT", template) }, // delete() - Deletes a campaign at DELETE /templates/:id delete: function(id){ - return query("/templates/" + id, "DELETE", data) + return query("/templates/" + id, "DELETE", {}) } } diff --git a/templates/users.html b/templates/users.html index 3aefd5e4..7e4c9271 100644 --- a/templates/users.html +++ b/templates/users.html @@ -29,10 +29,13 @@
- +
  -
+
+ +
+