mirror of https://github.com/gophish/gophish
Finished implementing first version of GET, POST /api/groups
parent
fb6cdb5caf
commit
0bb9dc186c
|
@ -127,7 +127,12 @@ func API_Campaigns_Id_Launch(w http.ResponseWriter, r *http.Request) {
|
||||||
/*
|
/*
|
||||||
POST /api/groups
|
POST /api/groups
|
||||||
{ "name" : "Test Group",
|
{ "name" : "Test Group",
|
||||||
"targets" : ["test@example.com", "test2@example.com"]
|
"targets" : [
|
||||||
|
{
|
||||||
|
"email" : "test@example.com"
|
||||||
|
},
|
||||||
|
{ "email" : test2@example.com"
|
||||||
|
}]
|
||||||
}
|
}
|
||||||
|
|
||||||
RESULT { "name" : "Test Group",
|
RESULT { "name" : "Test Group",
|
||||||
|
@ -139,12 +144,12 @@ func API_Groups(w http.ResponseWriter, r *http.Request) {
|
||||||
switch {
|
switch {
|
||||||
case r.Method == "GET":
|
case r.Method == "GET":
|
||||||
gs := []models.Group{}
|
gs := []models.Group{}
|
||||||
_, err := db.Conn.Select(&gs, "SELECT g.id, g.name, g.modified_date FROM groups g, users u WHERE g.uid=u.id AND u.api_key=?", ctx.Get(r, "api_key"))
|
_, err := db.Conn.Select(&gs, "SELECT g.id, g.name, g.modified_date FROM groups g, users u, user_groups ug WHERE ug.uid=u.id AND ug.gid=g.id AND u.api_key=?", ctx.Get(r, "api_key"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
for _, g := range gs {
|
for i, _ := range gs {
|
||||||
_, err := db.Conn.Select(&g.Targets, "SELECT t.id t.email FROM targets t, groups g, group_targets gt WHERE gt.gid=? AND gt.tid=t.id", g.Id)
|
_, err := db.Conn.Select(&gs[i].Targets, "SELECT t.id, t.email FROM targets t, groups g, group_targets gt WHERE gt.gid=? AND gt.tid=t.id", gs[i].Id)
|
||||||
if checkError(err, w, "Error looking up groups") {
|
if checkError(err, w, "Error looking up groups") {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -179,6 +184,10 @@ func API_Groups(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// Now, let's add the user->user_groups->group mapping
|
// Now, let's add the user->user_groups->group mapping
|
||||||
|
_, err = db.Conn.Exec("INSERT OR IGNORE INTO user_groups VALUES (?,?)", ctx.Get(r, "user_id").(int64), g.Id)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error adding many-many mapping for group %s\n", g.Name)
|
||||||
|
}
|
||||||
// TODO
|
// TODO
|
||||||
for _, t := range g.Targets {
|
for _, t := range g.Targets {
|
||||||
if _, err = mail.ParseAddress(t.Email); err != nil {
|
if _, err = mail.ParseAddress(t.Email); err != nil {
|
||||||
|
|
2
db/db.go
2
db/db.go
|
@ -32,7 +32,7 @@ func Setup() error {
|
||||||
//Create tables
|
//Create tables
|
||||||
`CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT NOT NULL, hash VARCHAR(60) NOT NULL, api_key VARCHAR(32), UNIQUE(username), UNIQUE(api_key));`,
|
`CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT NOT NULL, hash VARCHAR(60) NOT NULL, api_key VARCHAR(32), UNIQUE(username), UNIQUE(api_key));`,
|
||||||
`CREATE TABLE campaigns (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, created_date TIMESTAMP NOT NULL, completed_date TIMESTAMP, template TEXT, status TEXT NOT NULL, uid INTEGER, FOREIGN KEY (uid) REFERENCES users(id));`,
|
`CREATE TABLE campaigns (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, created_date TIMESTAMP NOT NULL, completed_date TIMESTAMP, template TEXT, status TEXT NOT NULL, uid INTEGER, FOREIGN KEY (uid) REFERENCES users(id));`,
|
||||||
`CREATE TABLE targets (id INTEGER PRIMARY KEY AUTOINCREMENT, address TEXT NOT NULL, UNIQUE(address));`,
|
`CREATE TABLE targets (id INTEGER PRIMARY KEY AUTOINCREMENT, email TEXT NOT NULL, UNIQUE(email));`,
|
||||||
`CREATE TABLE groups (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, modified_date TIMESTAMP NOT NULL);`,
|
`CREATE TABLE groups (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, modified_date TIMESTAMP NOT NULL);`,
|
||||||
`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 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 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));`,
|
||||||
|
|
|
@ -55,7 +55,6 @@ type Group struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
ModifiedDate time.Time `json:"modified_date" db:"modified_date"`
|
ModifiedDate time.Time `json:"modified_date" db:"modified_date"`
|
||||||
Targets []Target `json:"targets" db:"-"`
|
Targets []Target `json:"targets" db:"-"`
|
||||||
Uid int64 `json:"-" db:"-"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Target struct {
|
type Target struct {
|
||||||
|
|
Loading…
Reference in New Issue