Changing int to int64

Starting to implement angularjs
Implemented /api/campaigns/:id GET
Changed template delims to {{% and %}}
pull/24/head
Jordan 2014-01-31 20:49:22 -06:00
parent c4c57639e2
commit 87fbd41184
12 changed files with 84 additions and 61 deletions

View File

@ -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 // GetUserById returns the user that the given id corresponds to. If no user is found, an
// error is thrown. // error is thrown.
func GetUserById(id int) (models.User, error) { func GetUserById(id int64) (models.User, error) {
u := models.User{} 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 { if err != nil {
return u, err return u, err
} }
@ -64,7 +64,7 @@ func GetUserById(id int) (models.User, error) {
// error is thrown. // error is thrown.
func GetUserByAPIKey(key []byte) (models.User, error) { func GetUserByAPIKey(key []byte) (models.User, error) {
u := models.User{} 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 { if err != nil {
return u, err return u, err
} }

View File

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"strconv"
"time" "time"
ctx "github.com/gorilla/context" 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 //API_Campaigns_Id returns details about the requested campaign. If the campaign is not
//valid, API_Campaigns_Id returns null. //valid, API_Campaigns_Id returns null.
func API_Campaigns_Id(w http.ResponseWriter, r *http.Request) { func API_Campaigns_Id(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
vars := mux.Vars(r) 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. //API_Doc renders a template describing the API documentation.

View File

@ -39,6 +39,8 @@ import (
"github.com/jordan-wright/gophish/models" "github.com/jordan-wright/gophish/models"
) )
var templateDelims = []string{"{{%", "%}}"}
func CreateRouter() *mux.Router { func CreateRouter() *mux.Router {
router := mux.NewRouter() router := mux.NewRouter()
// Base Front-end routes // 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 { 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 { func checkError(e error, w http.ResponseWriter, m string) bool {

View File

@ -18,7 +18,7 @@ func GetContext(handler http.Handler) http.HandlerFunc {
// Put the session in the context so that // Put the session in the context so that
ctx.Set(r, "session", session) ctx.Set(r, "session", session)
if id, ok := session.Values["id"]; ok { if id, ok := session.Values["id"]; ok {
u, err := auth.GetUserById(id.(int)) u, err := auth.GetUserById(id.(int64))
if err != nil { if err != nil {
ctx.Set(r, "user", nil) ctx.Set(r, "user", nil)
} }

View File

@ -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;
})
})

View File

@ -1,6 +1,6 @@
{{define "base"}} {{% define "base" %}}
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en" ng-app="gophishApp">
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
@ -10,7 +10,7 @@
<meta name="author" content=""> <meta name="author" content="">
<link rel="shortcut icon" href="../../docs-assets/ico/favicon.png"> <link rel="shortcut icon" href="../../docs-assets/ico/favicon.png">
<title>Gophish - {{.Title}}</title> <title>Gophish - {{% .Title %}}</title>
<!-- Bootstrap core CSS --> <!-- Bootstrap core CSS -->
<link href="/css/bootstrap.css" rel="stylesheet"> <link href="/css/bootstrap.css" rel="stylesheet">
@ -19,10 +19,13 @@
<link href="/css/main.css" rel="stylesheet"> <link href="/css/main.css" rel="stylesheet">
<link href="/css/font-awesome.min.css" rel="stylesheet"> <link href="/css/font-awesome.min.css" rel="stylesheet">
<link href='http://fonts.googleapis.com/css?family=Open+Sans:400,300,600,700' rel='stylesheet' type='text/css'> <link href='http://fonts.googleapis.com/css?family=Open+Sans:400,300,600,700' rel='stylesheet' type='text/css'>
{{%if .User%}}
<script>var API_KEY = {{%.User.APIKey%}}</script>
{{%end%}}
</head> </head>
<body> <body>
{{template "content" .}} {{% template "content" . %}}
<!-- Footer --> <!-- Footer -->
<div class="container"> <div class="container">
<hr> <hr>
@ -33,7 +36,9 @@
<!-- Placed at the end of the document so the pages load faster --> <!-- Placed at the end of the document so the pages load faster -->
<script src="/js/jquery.js"></script> <script src="/js/jquery.js"></script>
<script src="/js/bootstrap.min.js"></script> <script src="/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.min.js"></script>
<script src="/js/app/controllers.js"></script>
</body> </body>
</html> </html>
{{end}} {{% end %}}

View File

@ -1,4 +1,4 @@
{{define "content"}} {{template "nav" .User}} {{% define "content" %}} {{% template "nav" .User %}}
<div class="jumbotron"> <div class="jumbotron">
<div class="container" style="text-align:center;"> <div class="container" style="text-align:center;">
<h1 class="sans header"> <h1 class="sans header">
@ -19,7 +19,7 @@
</li> </li>
</ul> </ul>
</div> </div>
<div class="col-md-9"> <div class="col-md-9" ng-controller="CampaignCtrl">
<div class="row"> <div class="row">
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#newCampaignModal"><i class="fa fa-plus"></i> New Campaign</button> <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#newCampaignModal"><i class="fa fa-plus"></i> New Campaign</button>
</div> </div>
@ -34,25 +34,10 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<tr> <tr ng-repeat="campaign in campaigns">
<td>1</td> <td>{{campaign.id}}</td>
<td>Mark</td> <td>{{campaign.name}}</td>
<td>Otto</td> <td>{{campaign.status}}</td>
</tr>
<tr>
<td>3</td>
<td>Mark</td>
<td>Otto</td>
</tr>
<tr>
<td>2</td>
<td>Jacob</td>
<td>Thornton</td>
</tr>
<tr>
<td>3</td>
<td>Larry the Bird</td>
<td>@twitter</td>
</tr> </tr>
</tbody> </tbody>
</table> </table>
@ -84,4 +69,4 @@
</div> </div>
</div> </div>
</div> </div>
{{end}} {{% end %}}

View File

@ -1,15 +1,15 @@
{{define "flashes"}} {{%define "flashes"%}}
{{range .}} {{%range .%}}
<div style="text-align:center" class="alert alert-{{.Type}}"> <div style="text-align:center" class="alert alert-{{%.Type%}}">
<i class="fa <i class="fa
{{if eq .Type "danger"}} {{%if eq .Type "danger"%}}
fa-exclamation-circle fa-exclamation-circle
{{else if eq .Type "warning"}} {{%else if eq .Type "warning"%}}
fa-exclamation-triangle fa-exclamation-triangle
{{else if eq .Type "success"}} {{%else if eq .Type "success"%}}
fa-check-circle fa-check-circle
{{end}}"></i> {{%end%}}"></i>
{{.Message}} {{%.Message%}}
</div> </div>
{{end}} {{%end%}}
{{end}} {{%end%}}

View File

@ -1,9 +1,9 @@
{{define "content"}} {{%define "content"%}}
<div class="container"> <div class="container">
<form class="form-signin" action="/login" method="POST"> <form class="form-signin" action="/login" method="POST">
<img id="logo" src="/images/logo.png" /> <img id="logo" src="/images/logo.png" />
<h2 class="form-signin-heading">Please sign in</h2> <h2 class="form-signin-heading">Please sign in</h2>
{{template "flashes" .Flashes}} {{%template "flashes" .Flashes%}}
<input type="text" name="username" class="form-control" placeholder="Username" required autofocus> <input type="text" name="username" class="form-control" placeholder="Username" required autofocus>
<input type="password" name="password" class="form-control" placeholder="Password" required> <input type="password" name="password" class="form-control" placeholder="Password" required>
<label class="checkbox"> <label class="checkbox">
@ -12,4 +12,4 @@
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button> <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form> </form>
</div> </div>
{{end}} {{%end%}}

View File

@ -1,4 +1,4 @@
{{define "nav"}} {{%define "nav"%}}
<div class="navbar navbar-inverse navbar-fixed-top"> <div class="navbar navbar-inverse navbar-fixed-top">
<div class="container"> <div class="container">
<div class="navbar-header"> <div class="navbar-header">
@ -13,29 +13,28 @@
<div class="navbar-collapse collapse"> <div class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right"> <ul class="nav navbar-nav navbar-right">
<li> <li>
{{if .}} {{%if .%}}
<div class="btn-group" id="navbar-dropdown"> <div class="btn-group" id="navbar-dropdown">
<button type="button" class="btn btn-primary"><i class="fa fa-user"></i> {{.Username}}</button> <button type="button" class="btn btn-primary"><i class="fa fa-user"></i> {{%.Username%}}</button>
<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown"> <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
<span class="caret" style="border-top-color:#FFFFFF"></span> <span class="caret" style="border-top-color:#FFFFFF"></span>
<span class="sr-only">Toggle Dropdown</span> <span class="sr-only">Toggle Dropdown</span>
</button> </button>
<ul class="dropdown-menu" role="menu"> <ul class="dropdown-menu" role="menu"> <li><a href="/settings">Settings</a>
<li><a href="/settings">Settings</a>
</li> </li>
<li class="divider"></li> <li class="divider"></li>
<li><a href="/logout">Logout</a> <li><a href="/logout">Logout</a>
</li> </li>
</ul> </ul>
</div> </div>
{{else}} {{%else%}}
<a href="/login"> <a href="/login">
<button type="button" class="btn btn-primary">Login</button> <button type="button" class="btn btn-primary">Login</button>
</a> </a>
{{end}} {{%end%}}
</li> </li>
</ul> </ul>
</div> </div>
</div> </div>
</div> </div>
{{end}} {{%end%}}

View File

@ -1,4 +1,4 @@
{{define "content"}} {{template "nav" .User}} {{%define "content"%}} {{%template "nav" .User%}}
<div class="jumbotron"> <div class="jumbotron">
<div class="container" style="text-align:center;"> <div class="container" style="text-align:center;">
<h1 class="sans header"> <h1 class="sans header">
@ -27,7 +27,7 @@
</p> </p>
</div> </div>
<div class="col-md-6"> <div class="col-md-6">
<input type="text" value="{{.User.Username}}" class="form-control" /> <input type="text" value="{{%.User.Username%}}" class="form-control" />
</div> </div>
</div> </div>
<br/> <br/>
@ -37,11 +37,11 @@
</p> </p>
</div> </div>
<div class="col-md-6"> <div class="col-md-6">
<input type="text" value="{{.User.APIKey}}" class="form-control" readonly/> <input type="text" value="{{%.User.APIKey%}}" class="form-control" readonly/>
</div> </div>
</div> </div>
<br /> <br />
<button class="btn btn-primary">Save</button> <button class="btn btn-primary">Save</button>
</div> </div>
</div> </div>
{{end}} {{%end%}}

View File

@ -1,4 +1,4 @@
{{define "content"}} {{template "nav"}} {{%define "content"%}} {{%template "nav"%}}
<div class="jumbotron"> <div class="jumbotron">
<div class="container" style="text-align:center;"> <div class="container" style="text-align:center;">
<h1 class="sans header"> <h1 class="sans header">
@ -26,5 +26,5 @@
<p>Test.</p> <p>Test.</p>
</div> </div>
</div> </div>
{{end}} {{%end%}}