From 631cd1ad138c0ab6acfa158ff1573ffe37cfe1a9 Mon Sep 17 00:00:00 2001 From: Jordan Date: Wed, 12 Feb 2014 10:43:54 -0600 Subject: [PATCH] Fixed the way Angular POSTS data Added error code to checkError to support responses such as BadRequest, NotFound, etc. --- controllers/api.go | 36 ++++++++++++++++++------------------ controllers/route.go | 4 ++-- static/js/app/gophish.js | 4 +--- templates/settings.html | 1 + 4 files changed, 22 insertions(+), 23 deletions(-) diff --git a/controllers/api.go b/controllers/api.go index 21aa3438..24d1f57a 100644 --- a/controllers/api.go +++ b/controllers/api.go @@ -61,7 +61,7 @@ func API_Campaigns(w http.ResponseWriter, r *http.Request) { fmt.Println(err) } cj, err := json.MarshalIndent(cs, "", " ") - if checkError(err, w, "Error looking up campaigns") { + if checkError(err, w, "Error looking up campaigns", http.StatusInternalServerError) { return } writeJSON(w, cj) @@ -70,7 +70,7 @@ func API_Campaigns(w http.ResponseWriter, r *http.Request) { c := models.Campaign{} // Put the request into a campaign err := json.NewDecoder(r.Body).Decode(&c) - if checkError(err, w, "Invalid Request") { + if checkError(err, w, "Invalid Request", http.StatusBadRequest) { return } // Fill in the details @@ -80,11 +80,11 @@ func API_Campaigns(w http.ResponseWriter, r *http.Request) { c.Uid = ctx.Get(r, "user_id").(int64) // Insert into the DB err = db.Conn.Insert(&c) - if checkError(err, w, "Cannot insert campaign into database") { + if checkError(err, w, "Cannot insert campaign into database", http.StatusInternalServerError) { return } cj, err := json.MarshalIndent(c, "", " ") - if checkError(err, w, "Error creating JSON response") { + if checkError(err, w, "Error creating JSON response", http.StatusInternalServerError) { return } writeJSON(w, cj) @@ -100,11 +100,11 @@ func API_Campaigns_Id(w http.ResponseWriter, r *http.Request) { case r.Method == "GET": c := models.Campaign{} c, err := db.GetCampaign(id, ctx.Get(r, "user_id").(int64)) - if checkError(err, w, "No campaign found") { + if checkError(err, w, "No campaign found", http.StatusNotFound) { return } cj, err := json.MarshalIndent(c, "", " ") - if checkError(err, w, "Error creating JSON response") { + if checkError(err, w, "Error creating JSON response", http.StatusInternalServerError) { return } writeJSON(w, cj) @@ -144,11 +144,11 @@ func API_Groups(w http.ResponseWriter, r *http.Request) { switch { case r.Method == "GET": gs, err := db.GetGroups(ctx.Get(r, "user_id").(int64)) - if checkError(err, w, "Cannot retrieve group information") { + if checkError(err, w, "Groups not found", http.StatusNotFound) { return } gj, err := json.MarshalIndent(gs, "", " ") - if checkError(err, w, "Error marshaling group information") { + if checkError(err, w, "Error marshaling group information", http.StatusInternalServerError) { return } writeJSON(w, gj) @@ -157,21 +157,21 @@ func API_Groups(w http.ResponseWriter, r *http.Request) { g := models.Group{} // Put the request into a group err := json.NewDecoder(r.Body).Decode(&g) - if checkError(err, w, "Invalid Request") { + if checkError(err, w, "Invalid Request", http.StatusBadRequest) { return } // Check to make sure targets were specified if len(g.Targets) == 0 { - http.Error(w, "Error: No targets specified", http.StatusInternalServerError) + http.Error(w, "Error: No targets specified", http.StatusBadRequest) return } g.ModifiedDate = time.Now() err = db.PostGroup(&g, ctx.Get(r, "user_id").(int64)) - if checkError(err, w, "Error inserting group") { + if checkError(err, w, "Error inserting group", http.StatusInternalServerError) { return } gj, err := json.MarshalIndent(g, "", " ") - if checkError(err, w, "Error creating JSON response") { + if checkError(err, w, "Error creating JSON response", http.StatusInternalServerError) { return } writeJSON(w, gj) @@ -186,23 +186,23 @@ func API_Groups_Id(w http.ResponseWriter, r *http.Request) { switch { case r.Method == "GET": g, err := db.GetGroup(id, ctx.Get(r, "user_id").(int64)) - if checkError(err, w, "No group found") { + if checkError(err, w, "No group found", http.StatusNotFound) { return } gj, err := json.MarshalIndent(g, "", " ") - if checkError(err, w, "Error creating JSON response") { + if checkError(err, w, "Error creating JSON response", http.StatusInternalServerError) { return } 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") { + if checkError(err, w, "Error creating JSON response", http.StatusInternalServerError) { return } writeJSON(w, []byte("{\"success\" : \"true\"}")) case r.Method == "PUT": _, err := db.GetGroup(id, ctx.Get(r, "user_id").(int64)) - if checkError(err, w, "No group found") { + if checkError(err, w, "No group found", http.StatusNotFound) { return } g := models.Group{} @@ -212,11 +212,11 @@ func API_Groups_Id(w http.ResponseWriter, r *http.Request) { return } err = db.PutGroup(&g, ctx.Get(r, "user_id").(int64)) - if checkError(err, w, "Error updating group") { + if checkError(err, w, "Error updating group", http.StatusInternalServerError) { return } gj, err := json.MarshalIndent(g, "", " ") - if checkError(err, w, "Error creating JSON response") { + if checkError(err, w, "Error creating JSON response", http.StatusInternalServerError) { return } writeJSON(w, gj) diff --git a/controllers/route.go b/controllers/route.go index 8e4a0f63..f3b861fd 100644 --- a/controllers/route.go +++ b/controllers/route.go @@ -210,10 +210,10 @@ func getTemplate(w http.ResponseWriter, tmpl string) *template.Template { return template.Must(templates, err) } -func checkError(e error, w http.ResponseWriter, m string) bool { +func checkError(e error, w http.ResponseWriter, m string, c int) bool { if e != nil { fmt.Println(e) - http.Error(w, "Error: "+m, http.StatusInternalServerError) + http.Error(w, "Error: "+m, c) return true } return false diff --git a/static/js/app/gophish.js b/static/js/app/gophish.js index 8056fa45..da8ec1ec 100644 --- a/static/js/app/gophish.js +++ b/static/js/app/gophish.js @@ -73,7 +73,6 @@ app.controller('GroupCtrl', function($scope, GroupService, ngTableParams) { $scope.group = { name: '', targets: [], - id: 0 }; } else { @@ -99,7 +98,7 @@ app.controller('GroupCtrl', function($scope, GroupService, ngTableParams) { $scope.saveGroup = function(group) { var newGroup = new GroupService($scope.group); if ($scope.newGroup) { - newGroup.$save(function() { + newGroup.$save({},function() { $scope.groups.push(newGroup); $scope.mainTableParams.reload() }); @@ -110,7 +109,6 @@ app.controller('GroupCtrl', function($scope, GroupService, ngTableParams) { $scope.group = { name: '', targets: [], - id: 0 }; $scope.editGroupTableParams.reload() } diff --git a/templates/settings.html b/templates/settings.html index 6b0f0231..a52a85e0 100644 --- a/templates/settings.html +++ b/templates/settings.html @@ -55,6 +55,7 @@ +