From 87fbd41184b7b5cc6b89e6aa06cef4811636a5ed Mon Sep 17 00:00:00 2001 From: Jordan Date: Fri, 31 Jan 2014 20:49:22 -0600 Subject: [PATCH] Changing int to int64 Starting to implement angularjs Implemented /api/campaigns/:id GET Changed template delims to {{% and %}} --- auth/auth.go | 6 +++--- controllers/api.go | 23 +++++++++++++++++++++-- controllers/route.go | 10 +++++++++- middleware/middleware.go | 2 +- static/js/app/controllers.js | 7 +++++++ templates/base.html | 15 ++++++++++----- templates/dashboard.html | 29 +++++++---------------------- templates/flashes.html | 20 ++++++++++---------- templates/login.html | 6 +++--- templates/nav.html | 15 +++++++-------- templates/settings.html | 8 ++++---- templates/users.html | 4 ++-- 12 files changed, 84 insertions(+), 61 deletions(-) create mode 100644 static/js/app/controllers.js diff --git a/auth/auth.go b/auth/auth.go index 8bec4e3f..7f87ff10 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -51,9 +51,9 @@ func Login(r *http.Request) (bool, error) { // GetUserById returns the user that the given id corresponds to. If no user is found, an // error is thrown. -func GetUserById(id int) (models.User, error) { +func GetUserById(id int64) (models.User, error) { u := models.User{} - err := db.Conn.SelectOne(&u, "SELECT id, username, apikey FROM Users WHERE id=?", id) + err := db.Conn.SelectOne(&u, "SELECT id, username, api_key FROM Users WHERE id=?", id) if err != nil { return u, err } @@ -64,7 +64,7 @@ func GetUserById(id int) (models.User, error) { // error is thrown. func GetUserByAPIKey(key []byte) (models.User, error) { u := models.User{} - err := db.Conn.SelectOne(&u, "SELECT id, username, apikey FROM Users WHERE apikey=?", key) + err := db.Conn.SelectOne(&u, "SELECT id, username, api_key FROM Users WHERE apikey=?", key) if err != nil { return u, err } diff --git a/controllers/api.go b/controllers/api.go index b9af6ad4..6fa254f1 100644 --- a/controllers/api.go +++ b/controllers/api.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "net/http" + "strconv" "time" ctx "github.com/gorilla/context" @@ -78,9 +79,27 @@ func API_Campaigns(w http.ResponseWriter, r *http.Request) { //API_Campaigns_Id returns details about the requested campaign. If the campaign is not //valid, API_Campaigns_Id returns null. func API_Campaigns_Id(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json") vars := mux.Vars(r) - fmt.Fprintf(w, "{\"method\" : \""+r.Method+"\", \"id\" : "+vars["id"]+"}") + id, err := strconv.ParseInt(vars["id"], 0, 64) + if checkError(err, w, "Invalid Int") { + return + } + switch { + case r.Method == "GET": + c := models.Campaign{} + err := db.Conn.SelectOne(&c, "SELECT campaigns.id, name, created_date, completed_date, status, template FROM campaigns, users WHERE campaigns.uid=users.id AND campaigns.id =? AND users.api_key=?", id, ctx.Get(r, "api_key")) + if checkError(err, w, "No campaign found") { + return + } + fmt.Printf("%v\n", c) + cj, err := json.MarshalIndent(c, "", " ") + if checkError(err, w, "Error creating JSON response") { + return + } + writeJSON(w, cj) + case r.Method == "DELETE": + //c := models.Campaign{} + } } //API_Doc renders a template describing the API documentation. diff --git a/controllers/route.go b/controllers/route.go index 5bbb494f..2273cba4 100644 --- a/controllers/route.go +++ b/controllers/route.go @@ -39,6 +39,8 @@ import ( "github.com/jordan-wright/gophish/models" ) +var templateDelims = []string{"{{%", "%}}"} + func CreateRouter() *mux.Router { router := mux.NewRouter() // Base Front-end routes @@ -145,7 +147,13 @@ func Login(w http.ResponseWriter, r *http.Request) { } func getTemplate(w http.ResponseWriter, tmpl string) *template.Template { - return template.Must(template.New("template").ParseFiles("templates/base.html", "templates/nav.html", "templates/"+tmpl+".html", "templates/flashes.html")) + templates := template.New("template") + templates.Delims(templateDelims[0], templateDelims[1]) + _, err := templates.ParseFiles("templates/base.html", "templates/nav.html", "templates/"+tmpl+".html", "templates/flashes.html") + if err != nil { + fmt.Println(err) + } + return template.Must(templates, err) } func checkError(e error, w http.ResponseWriter, m string) bool { diff --git a/middleware/middleware.go b/middleware/middleware.go index 3230de32..7afdf70c 100644 --- a/middleware/middleware.go +++ b/middleware/middleware.go @@ -18,7 +18,7 @@ func GetContext(handler http.Handler) http.HandlerFunc { // Put the session in the context so that ctx.Set(r, "session", session) if id, ok := session.Values["id"]; ok { - u, err := auth.GetUserById(id.(int)) + u, err := auth.GetUserById(id.(int64)) if err != nil { ctx.Set(r, "user", nil) } diff --git a/static/js/app/controllers.js b/static/js/app/controllers.js new file mode 100644 index 00000000..d18e9106 --- /dev/null +++ b/static/js/app/controllers.js @@ -0,0 +1,7 @@ +var gophishApp = angular.module('gophishApp', []); + +gophishApp.controller('CampaignCtrl', function($scope, $http) { + $http.get('/api/campaigns?api_key=' + API_KEY).success(function(data) { + $scope.campaigns = data; + }) +}) \ No newline at end of file diff --git a/templates/base.html b/templates/base.html index 950e8743..d6f6f6e6 100644 --- a/templates/base.html +++ b/templates/base.html @@ -1,6 +1,6 @@ -{{define "base"}} +{{% define "base" %}} - + @@ -10,7 +10,7 @@ - Gophish - {{.Title}} + Gophish - {{% .Title %}} @@ -19,10 +19,13 @@ + {{%if .User%}} + + {{%end%}} - {{template "content" .}} + {{% template "content" . %}}

@@ -33,7 +36,9 @@ + + -{{end}} +{{% end %}} diff --git a/templates/dashboard.html b/templates/dashboard.html index 722cfa3d..18c7385d 100644 --- a/templates/dashboard.html +++ b/templates/dashboard.html @@ -1,4 +1,4 @@ -{{define "content"}} {{template "nav" .User}} +{{% define "content" %}} {{% template "nav" .User %}}

@@ -19,7 +19,7 @@

-
+
@@ -34,25 +34,10 @@ - - 1 - Mark - Otto - - - 3 - Mark - Otto - - - 2 - Jacob - Thornton - - - 3 - Larry the Bird - @twitter + + {{campaign.id}} + {{campaign.name}} + {{campaign.status}} @@ -84,4 +69,4 @@
-{{end}} +{{% end %}} diff --git a/templates/flashes.html b/templates/flashes.html index 9deec730..2cb31a0f 100644 --- a/templates/flashes.html +++ b/templates/flashes.html @@ -1,15 +1,15 @@ -{{define "flashes"}} - {{range .}} -
+{{%define "flashes"%}} + {{%range .%}} +
- {{.Message}} + {{%end%}}"> + {{%.Message%}}
- {{end}} -{{end}} \ No newline at end of file + {{%end%}} +{{%end%}} \ No newline at end of file diff --git a/templates/login.html b/templates/login.html index 06c63468..559c5de9 100644 --- a/templates/login.html +++ b/templates/login.html @@ -1,9 +1,9 @@ -{{define "content"}} +{{%define "content"%}}
-{{end}} +{{%end%}} diff --git a/templates/nav.html b/templates/nav.html index 2883b1d8..f8908a2c 100644 --- a/templates/nav.html +++ b/templates/nav.html @@ -1,4 +1,4 @@ -{{define "nav"}} +{{%define "nav"%}}
- +

-{{end}} +{{%end%}} diff --git a/templates/users.html b/templates/users.html index 03a5645b..e148d32a 100644 --- a/templates/users.html +++ b/templates/users.html @@ -1,4 +1,4 @@ -{{define "content"}} {{template "nav"}} +{{%define "content"%}} {{%template "nav"%}}

@@ -26,5 +26,5 @@

Test.

-{{end}} +{{%end%}}