From 86dca67a5a5d1ba9454551bb6401d2aa0eca72d4 Mon Sep 17 00:00:00 2001 From: Jordan Date: Wed, 23 Jul 2014 21:04:38 -0500 Subject: [PATCH] Finished implementing PUT /templates/:id Now deleting events on campaign deletion Added some minor UI fixes and changes --- controllers/api.go | 9 +- gophish.go | 7 +- models/attachment.go | 3 +- models/campaign.go | 5 ++ models/template.go | 32 ++++++- static/js/app/controllers.js | 10 ++- static/js/app/partials/campaign_results.html | 1 - static/js/app/partials/campaigns.html | 9 +- static/js/app/partials/dashboard.html | 88 ++++++++++--------- .../js/app/partials/modals/templateModal.html | 19 ++-- 10 files changed, 122 insertions(+), 61 deletions(-) diff --git a/controllers/api.go b/controllers/api.go index fe7be5ea..57c06b38 100644 --- a/controllers/api.go +++ b/controllers/api.go @@ -234,14 +234,17 @@ func API_Templates_Id(w http.ResponseWriter, r *http.Request) { case r.Method == "PUT": t = models.Template{} err = json.NewDecoder(r.Body).Decode(&t) + if err != nil { + Logger.Println(err) + } if t.Id != id { http.Error(w, "Error: /:id and template_id mismatch", http.StatusBadRequest) return } err = t.Validate() -/* if checkError(err, w, http.StatusBadRequest) { - return - }*/ + /* if checkError(err, w, http.StatusBadRequest) { + return + }*/ t.ModifiedDate = time.Now() t.UserId = ctx.Get(r, "user_id").(int64) err = models.PutTemplate(&t) diff --git a/gophish.go b/gophish.go index 3e83147f..90d520fc 100644 --- a/gophish.go +++ b/gophish.go @@ -27,6 +27,7 @@ THE SOFTWARE. */ import ( "fmt" + "log" "net/http" "os" @@ -36,6 +37,8 @@ import ( "github.com/jordan-wright/gophish/models" ) +var Logger = log.New(os.Stdout, " ", log.Ldate|log.Ltime|log.Lshortfile) + func main() { // Setup the global variables and settings err := models.Setup() @@ -43,8 +46,8 @@ func main() { fmt.Println(err) } // Start the web servers - fmt.Printf("Admin server started at http://%s\n", config.Conf.AdminURL) + Logger.Printf("Admin server started at http://%s\n", config.Conf.AdminURL) go http.ListenAndServe(config.Conf.AdminURL, handlers.CombinedLoggingHandler(os.Stdout, controllers.CreateAdminRouter())) - fmt.Printf("Phishing server started at http://%s\n", config.Conf.PhishURL) + Logger.Printf("Phishing server started at http://%s\n", config.Conf.PhishURL) http.ListenAndServe(config.Conf.PhishURL, handlers.CombinedLoggingHandler(os.Stdout, controllers.CreatePhishingRouter())) } diff --git a/models/attachment.go b/models/attachment.go index 49366eb8..996c3ea6 100644 --- a/models/attachment.go +++ b/models/attachment.go @@ -1,7 +1,8 @@ package models type Attachment struct { - TemplateId string `json:"-"` + Id int64 `json:"-"` + TemplateId int64 `json:"-"` Content string `json:"content"` Type string `json:"type"` Name string `json:"name"` diff --git a/models/campaign.go b/models/campaign.go index 81287a06..1720e529 100644 --- a/models/campaign.go +++ b/models/campaign.go @@ -162,6 +162,11 @@ func DeleteCampaign(id int64) error { Logger.Println(err) return err } + err = db.Where("campaign_id=?", id).Delete(&Event{}).Error + if err != nil { + Logger.Println(err) + return err + } // Delete the campaign err = db.Delete(&Campaign{Id: id}).Error if err != nil { diff --git a/models/template.go b/models/template.go index 8017ed7c..d4e78dac 100644 --- a/models/template.go +++ b/models/template.go @@ -82,13 +82,36 @@ func PostTemplate(t *Template) error { Logger.Println(err) return err } + for i, _ := range t.Attachments { + Logger.Println(t.Attachments[i].Name) + t.Attachments[i].TemplateId = t.Id + err := db.Save(&t.Attachments[i]).Error + if err != nil { + Logger.Println(err) + return err + } + } return nil } // PutTemplate edits an existing template in the database. // Per the PUT Method RFC, it presumes all data for a template is provided. func PutTemplate(t *Template) error { - err := db.Where("id=?", t.Id).Save(t).Error + // Delete all attachments, and replace with new ones + err := db.Where("template_id=?", t.Id).Delete(&Attachment{}).Error + if err != nil { + Logger.Println(err) + return err + } + for i, _ := range t.Attachments { + t.Attachments[i].TemplateId = t.Id + err := db.Save(&t.Attachments[i]).Error + if err != nil { + Logger.Println(err) + return err + } + } + err = db.Where("id=?", t.Id).Save(t).Error if err != nil { Logger.Println(err) return err @@ -99,7 +122,12 @@ func PutTemplate(t *Template) error { // DeleteTemplate deletes an existing template in the database. // An error is returned if a template with the given user id and template id is not found. func DeleteTemplate(id int64, uid int64) error { - err := db.Where("user_id=?", uid).Delete(Template{Id: id}).Error + err := db.Where("template_id=?", id).Delete(&Attachment{}).Error + if err != nil { + Logger.Println(err) + return err + } + err = db.Where("user_id=?", uid).Delete(Template{Id: id}).Error if err != nil { Logger.Println(err) return err diff --git a/static/js/app/controllers.js b/static/js/app/controllers.js index 5ff20f3b..9097615d 100644 --- a/static/js/app/controllers.js +++ b/static/js/app/controllers.js @@ -600,7 +600,7 @@ app.controller('TemplateCtrl', function($scope, $modal, TemplateService, ngTable name: '', html: '', text: '', - files: [] + attachments: [] }; } else { @@ -654,11 +654,12 @@ app.controller('TemplateCtrl', function($scope, $modal, TemplateService, ngTable }) var TemplateModalCtrl = function($scope, $upload, $modalInstance) { - var reader = new FileReader(); $scope.onFileSelect = function($files) { + console.log($files) angular.forEach($files, function(file, key) { + var reader = new FileReader(); reader.onload = function(e) { - $scope.template.files.push({ + $scope.template.attachments.push({ name : file.name, content : reader.result.split(",")[1], type : file.type || "application/octet-stream" @@ -678,6 +679,9 @@ var TemplateModalCtrl = function($scope, $upload, $modalInstance) { $modalInstance.dismiss('') $scope.saveTemplate(template) }; + $scope.removeFile = function(file) { + $scope.template.attachments.splice($scope.template.attachments.indexOf(file), 1); + } }; app.controller('SettingsCtrl', function($scope, $http, $window) { diff --git a/static/js/app/partials/campaign_results.html b/static/js/app/partials/campaign_results.html index 316ecf24..aa31a503 100644 --- a/static/js/app/partials/campaign_results.html +++ b/static/js/app/partials/campaign_results.html @@ -52,7 +52,6 @@ - Timeline here Plugins here Demographics here diff --git a/static/js/app/partials/campaigns.html b/static/js/app/partials/campaigns.html index 0e99b3de..16d60a07 100644 --- a/static/js/app/partials/campaigns.html +++ b/static/js/app/partials/campaigns.html @@ -31,7 +31,14 @@   -
+
+
+
+ No campaigns yet. +
+
+
+
diff --git a/static/js/app/partials/dashboard.html b/static/js/app/partials/dashboard.html index 4e40bc18..287fcac6 100644 --- a/static/js/app/partials/dashboard.html +++ b/static/js/app/partials/dashboard.html @@ -22,51 +22,55 @@

Dashboard

-
-
- {{flash.message}} +
+
+
+ No campaigns yet. +
-
-
- +
+
+
+ +
+
+ +
-
- +
+

Recent Campaigns

+
+
+ +
+    +
+
+ + + + + + + +
{{campaign.created_date | date:'medium'}}{{campaign.name}} +
+ + +
+
{{campaign.status}}
-
-
-

Recent Campaigns

-
-
- -
-    -
- - - - - - - - -
{{campaign.created_date | date:'medium'}}{{campaign.name}} -
- - -
-
{{campaign.status}}
diff --git a/static/js/app/partials/modals/templateModal.html b/static/js/app/partials/modals/templateModal.html index a813fcbb..34256ece 100644 --- a/static/js/app/partials/modals/templateModal.html +++ b/static/js/app/partials/modals/templateModal.html @@ -31,17 +31,24 @@
- Add Files (Coming Soon!) - - -
+ +
- + + + + +  {{file.name}} - {{file.type}} + +
+
+ Add Files + +