From 2420e19e1585487d2ab7a19972b906e458e0dc1b Mon Sep 17 00:00:00 2001 From: Jordan Date: Thu, 13 Feb 2014 12:05:22 -0600 Subject: [PATCH] Implemented DELETE /api/groups/:id --- controllers/api.go | 8 ++++++-- db/db.go | 20 +++++++++++++++++--- static/js/app/gophish.js | 9 +++++++-- templates/users.html | 2 +- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/controllers/api.go b/controllers/api.go index 24d1f57a..08392e75 100644 --- a/controllers/api.go +++ b/controllers/api.go @@ -195,8 +195,12 @@ func API_Groups_Id(w http.ResponseWriter, r *http.Request) { } writeJSON(w, gj) case r.Method == "DELETE": - err := db.DeleteGroup(id, ctx.Get(r, "user_id").(int64)) - if checkError(err, w, "Error creating JSON response", http.StatusInternalServerError) { + _, err := db.GetGroup(id, ctx.Get(r, "user_id").(int64)) + if checkError(err, w, "No group found", http.StatusNotFound) { + return + } + err = db.DeleteGroup(id) + if checkError(err, w, "Error deleting group", http.StatusInternalServerError) { return } writeJSON(w, []byte("{\"success\" : \"true\"}")) diff --git a/db/db.go b/db/db.go index b1443e8e..917659df 100644 --- a/db/db.go +++ b/db/db.go @@ -37,7 +37,8 @@ func Setup() error { `CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT NOT NULL, hash VARCHAR(60) NOT NULL, api_key VARCHAR(32), UNIQUE(username), UNIQUE(api_key));`, `CREATE TABLE campaigns (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, created_date TIMESTAMP NOT NULL, completed_date TIMESTAMP, template TEXT, status TEXT NOT NULL, uid INTEGER, FOREIGN KEY (uid) REFERENCES users(id));`, `CREATE TABLE targets (id INTEGER PRIMARY KEY AUTOINCREMENT, email TEXT NOT NULL, UNIQUE(email));`, - `CREATE TABLE groups (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, modified_date TIMESTAMP NOT NULL, UNIQUE(name));`, + `CREATE TABLE groups (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, modified_date TIMESTAMP NOT NULL);`, + `CREATE TABLE campaign_results (cid INTEGER NOT NULL, tid INTEGER NOT NULL, result TEXT NOT NULL, FOREIGN KEY (cid) REFERENCES users(id), FOREIGN KEY (tid) REFERENCES targets(id), UNIQUE(cid, tid))`, `CREATE TABLE user_groups (uid INTEGER NOT NULL, gid INTEGER NOT NULL, FOREIGN KEY (uid) REFERENCES users(id), FOREIGN KEY (gid) REFERENCES groups(id), UNIQUE(uid, gid))`, `CREATE TABLE group_targets (gid INTEGER NOT NULL, tid INTEGER NOT NULL, FOREIGN KEY (gid) REFERENCES groups(id), FOREIGN KEY (tid) REFERENCES targets(id), UNIQUE(gid, tid));`, } @@ -265,6 +266,19 @@ func insertTargetIntoGroup(t models.Target, gid int64) error { return nil } -func DeleteGroup(id int64, uid int64) error { - return nil +// DeleteGroup deletes a given group by group ID and user ID +func DeleteGroup(id int64) error { + // Delete all the group_targets entries for this group + _, err := Conn.Exec("DELETE FROM group_targets WHERE gid=?", id) + if err != nil { + return err + } + // Delete the reference to the group in the user_group table + _, err = Conn.Exec("DELETE FROM user_groups WHERE gid=?", id) + if err != nil { + return err + } + // Delete the group itself + _, err = Conn.Exec("DELETE FROM groups WHERE id=?", id) + return err } diff --git a/static/js/app/gophish.js b/static/js/app/gophish.js index da8ec1ec..c621374c 100644 --- a/static/js/app/gophish.js +++ b/static/js/app/gophish.js @@ -96,14 +96,13 @@ app.controller('GroupCtrl', function($scope, GroupService, ngTableParams) { $scope.editGroupTableParams.reload() }; $scope.saveGroup = function(group) { - var newGroup = new GroupService($scope.group); + var newGroup = new GroupService(group); if ($scope.newGroup) { newGroup.$save({},function() { $scope.groups.push(newGroup); $scope.mainTableParams.reload() }); } else { - console.log(newGroup.id) newGroup.$update({id : newGroup.id}) } $scope.group = { @@ -112,4 +111,10 @@ app.controller('GroupCtrl', function($scope, GroupService, ngTableParams) { }; $scope.editGroupTableParams.reload() } + $scope.deleteGroup = function(group) { + var deleteGroup = new GroupService(group); + deleteGroup.$delete({id : deleteGroup.id}, function() { + $scope.mainTableParams.reload(); + }); + } }) diff --git a/templates/users.html b/templates/users.html index 4d53a952..a621149a 100644 --- a/templates/users.html +++ b/templates/users.html @@ -44,7 +44,7 @@
  • Edit
  • -
  • Delete +
  • Delete