diff --git a/controllers/api.go b/controllers/api.go index 0cd1ef2f..f9dfb142 100644 --- a/controllers/api.go +++ b/controllers/api.go @@ -237,6 +237,14 @@ func API_Groups_Id(w http.ResponseWriter, r *http.Request) { } } +func API_Templates(w http.ResponseWriter, r *http.Request) { + http.Redirect(w, r, "/", 302) +} + +func API_Templates_Id(w http.ResponseWriter, r *http.Request) { + http.Redirect(w, r, "/", 302) +} + func writeJSON(w http.ResponseWriter, c []byte) { w.Header().Set("Content-Type", "application/json") fmt.Fprintf(w, "%s", c) diff --git a/controllers/route.go b/controllers/route.go index 84923abc..b0fbae7c 100644 --- a/controllers/route.go +++ b/controllers/route.go @@ -34,9 +34,10 @@ func CreateRouter() *nosurf.CSRFHandler { api.HandleFunc("/reset", Use(API_Reset, mid.RequireLogin)) api.HandleFunc("/campaigns/", Use(API_Campaigns, mid.RequireAPIKey)) api.HandleFunc("/campaigns/{id:[0-9]+}", Use(API_Campaigns_Id, mid.RequireAPIKey)) - //api.HandleFunc("/campaigns/id:[0-9]+}", Use(API_Campaigns_Id_Launch, mid.RequireAPIKey)) api.HandleFunc("/groups/", Use(API_Groups, mid.RequireAPIKey)) api.HandleFunc("/groups/{id:[0-9]+}", Use(API_Groups_Id, mid.RequireAPIKey)) + api.HandleFunc("/templates", Use(API_Templates, mid.RequireAPIKey)) + api.HandleFunc("/templates/{id:[0-9]+}", Use(API_Templates_Id, mid.RequireAPIKey)) // Setup static file serving router.PathPrefix("/").Handler(http.FileServer(http.Dir("./static/"))) @@ -46,6 +47,7 @@ func CreateRouter() *nosurf.CSRFHandler { // Exempt API routes and Static files csrfHandler.ExemptGlob("/api/campaigns/*") csrfHandler.ExemptGlob("/api/groups/*") + csrfHandler.ExemptGlob("/api/templates/*") csrfHandler.ExemptGlob("/static/*") return csrfHandler } diff --git a/db/db.go b/db/db.go index f28f2652..c7885629 100644 --- a/db/db.go +++ b/db/db.go @@ -42,6 +42,8 @@ func Setup() error { `CREATE TABLE user_campaigns (uid INTEGER NOT NULL, cid INTEGER NOT NULL, FOREIGN KEY (uid) REFERENCES users(id), FOREIGN KEY (cid) REFERENCES campaigns(id), UNIQUE(uid, cid))`, `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));`, + `CREATE TABLE templates (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, modified_date TIMESTAMP NOT NULL, html TEXT NOT NULL, plaintext TEXT NOT NULL;`, + `CREATE TABLE user_templates (uid INTEGER NOT NULL, tid INTEGER NOT NULL, FOREIGN KEY (uid) REFERENCES users(id), FOREIGN KEY (tid) REFERENCES templates(id), UNIQUE(uid, tid));`, } Logger.Printf("Creating db at %s\n", config.Conf.DBPath) //Create the tables needed @@ -112,6 +114,9 @@ func PutUser(u *models.User) error { func GetCampaigns(uid int64) ([]models.Campaign, error) { cs := []models.Campaign{} _, err := Conn.Select(&cs, "SELECT c.id, name, created_date, completed_date, status, template FROM campaigns c, user_campaigns uc, users u WHERE uc.uid=u.id AND uc.cid=c.id AND u.id=?", uid) + for i, _ := range cs { + _, err = Conn.Select(&cs[i].Results, "SELECT r.email, r.status FROM campaign_results r WHERE r.cid=?", cs[i].Id) + } return cs, err } @@ -299,6 +304,23 @@ func PutGroup(g *models.Group, uid int64) error { return nil } +// GetCampaigns returns the campaigns owned by the given user. +func GetTemplates(uid int64) ([]models.Template, error) { + ts := []models.Template{} + _, err := Conn.Select(&ts, "SELECT t.id, t.name, t.modified_date, t.text, t.html FROM templates t, user_templates ut, users u WHERE ut.uid=u.id AND ut.tid=c.id AND u.id=?", uid) + return ts, err +} + +// GetCampaign returns the campaign, if it exists, specified by the given id and user_id. +func GetTemplate(id int64, uid int64) (models.Template, error) { + t := models.Template{} + err := Conn.SelectOne(&t, "SELECT t.id, t.name, t.modified_date, t.text, t.html FROM templates t, user_templates ut, users u WHERE ut.uid=u.id AND ut.tid=t.id AND t.id=? AND u.id=?", id, uid) + if err != nil { + return t, err + } + return t, err +} + func insertTargetIntoGroup(t models.Target, gid int64) error { if _, err = mail.ParseAddress(t.Email); err != nil { Logger.Printf("Invalid email %s\n", t.Email) diff --git a/models/models.go b/models/models.go index bb08cdf5..ca4abaa3 100644 --- a/models/models.go +++ b/models/models.go @@ -60,3 +60,11 @@ type Target struct { Id int64 `json:"-"` Email string `json:"email"` } + +type Template struct { + Id int64 `json:"-"` + Name string `json:"name"` + Text string `json:"text"` + Html string `json:"html"` + ModifiedDate time.Time `json:"modified_date" db:"modified_date"` +}