Cleaned up old gorp code

Bug fix in settings template
pull/24/head
Jordan 2014-03-26 15:01:49 -05:00
parent 5740ee3273
commit 776f591f68
4 changed files with 28 additions and 70 deletions

View File

@ -29,18 +29,15 @@ type Result struct {
// GetCampaigns returns the campaigns owned by the given user. // GetCampaigns returns the campaigns owned by the given user.
func GetCampaigns(uid int64) ([]Campaign, error) { func GetCampaigns(uid int64) ([]Campaign, error) {
cs := []Campaign{} cs := []Campaign{}
err := db.Debug().Model(&User{Id: uid}).Related(&cs).Error err := db.Model(&User{Id: uid}).Related(&cs).Error
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
/* _, 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 { */for i, _ := range cs {
err := db.Debug().Model(&cs[i]).Related(&cs[i].Results).Error err := db.Model(&cs[i]).Related(&cs[i].Results).Error
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
/* _, err = Conn.Select(&cs[i].Results, "SELECT r.email, r.status FROM campaign_results r WHERE r.cid=?", cs[i].Id)
*/
} }
fmt.Printf("%v", cs) fmt.Printf("%v", cs)
return cs, err return cs, err
@ -49,13 +46,11 @@ func GetCampaigns(uid int64) ([]Campaign, error) {
// GetCampaign returns the campaign, if it exists, specified by the given id and user_id. // GetCampaign returns the campaign, if it exists, specified by the given id and user_id.
func GetCampaign(id int64, uid int64) (Campaign, error) { func GetCampaign(id int64, uid int64) (Campaign, error) {
c := Campaign{} c := Campaign{}
err := db.Debug().Where("id = ?", id).Where("user_id = ?", uid).Find(&c).Error err := db.Where("id = ?", id).Where("user_id = ?", uid).Find(&c).Error
/* err := Conn.SelectOne(&c, "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 c.id=? AND u.id=?", id, uid)
*/if err != nil { */if err != nil {
return c, err return c, err
} }
err = db.Debug().Model(&c).Related(&c.Results).Error err = db.Model(&c).Related(&c.Results).Error
/* _, err = Conn.Select(&c.Results, "SELECT r.email, r.status FROM campaign_results r WHERE r.cid=?", c.Id)
*/return c, err */return c, err
} }
@ -73,8 +68,7 @@ func PostCampaign(c *Campaign, uid int64) error {
} }
} }
// Insert into the DB // Insert into the DB
/*err = Conn.Insert(c)*/ err = db.Save(c).Error
err = db.Debug().Save(c).Error
if err != nil { if err != nil {
Logger.Println(err) Logger.Println(err)
return err return err
@ -86,37 +80,29 @@ func PostCampaign(c *Campaign, uid int64) error {
r := Result{Email: t.Email, Status: "Unknown", CampaignId: c.Id} r := Result{Email: t.Email, Status: "Unknown", CampaignId: c.Id}
c.Results = append(c.Results, r) c.Results = append(c.Results, r)
fmt.Printf("%v", c.Results) fmt.Printf("%v", c.Results)
err := db.Debug().Save(&r).Error err := db.Save(&r).Error
/*_, err = Conn.Exec("INSERT INTO campaign_results VALUES (?,?,?)", c.Id, r.Email, r.Status)*/
if err != nil { if err != nil {
Logger.Printf("Error adding result record for target %s\n", t.Email) Logger.Printf("Error adding result record for target %s\n", t.Email)
Logger.Println(err) Logger.Println(err)
} }
} }
} }
/*_, err = Conn.Exec("INSERT OR IGNORE INTO user_campaigns VALUES (?,?)", uid, c.Id)
if err != nil {
Logger.Printf("Error adding many-many mapping for campaign %s\n", c.Name)
}*/
return nil return nil
} }
//DeleteCampaign deletes the specified campaign
func DeleteCampaign(id int64) error { func DeleteCampaign(id int64) error {
// Delete all the campaign_results entries for this group // Delete all the campaign results
err := db.Debug().Delete(&Result{CampaignId: id}).Error err := db.Delete(&Result{CampaignId: id}).Error
/*_, err := Conn.Exec("DELETE FROM campaign_results WHERE cid=?", id)*/
if err != nil { if err != nil {
Logger.Println(err) Logger.Println(err)
return err return err
} }
// Delete the reference to the campaign in the user_campaigns table // Delete the campaign
err = db.Debug().Delete(&Campaign{Id: id}).Error err = db.Delete(&Campaign{Id: id}).Error
/*_, err = Conn.Exec("DELETE FROM user_campaigns WHERE cid=?", id)*/
if err != nil { if err != nil {
Logger.Panicln(err) Logger.Panicln(err)
return err return err
} }
// Delete the campaign itself
/*_, err = Conn.Exec("DELETE FROM campaigns WHERE id=?", id)*/
return err return err
} }

View File

@ -32,8 +32,7 @@ type Target struct {
// GetGroups returns the groups owned by the given user. // GetGroups returns the groups owned by the given user.
func GetGroups(uid int64) ([]Group, error) { func GetGroups(uid int64) ([]Group, error) {
gs := []Group{} gs := []Group{}
err := db.Debug().Table("groups g").Select("g.*").Joins("left join user_groups ug ON g.id = ug.group_id").Where("ug.user_id=?", uid).Scan(&gs).Error err := db.Table("groups g").Select("g.*").Joins("left join user_groups ug ON g.id = ug.group_id").Where("ug.user_id=?", uid).Scan(&gs).Error
/*_, err := Conn.Select(&gs, "SELECT g.id, g.name, g.modified_date FROM groups g, user_groups ug, users u WHERE ug.uid=u.id AND ug.gid=g.id AND u.id=?", uid)*/
if err != nil { if err != nil {
Logger.Println(err) Logger.Println(err)
return gs, err return gs, err
@ -50,14 +49,12 @@ func GetGroups(uid int64) ([]Group, error) {
// GetGroup returns the group, if it exists, specified by the given id and user_id. // GetGroup returns the group, if it exists, specified by the given id and user_id.
func GetGroup(id int64, uid int64) (Group, error) { func GetGroup(id int64, uid int64) (Group, error) {
g := Group{} g := Group{}
err := db.Debug().Table("groups g").Select("g.*").Joins("left join user_groups ug ON g.id = ug.group_id").Where("ug.user_id=? and g.id=?", uid, id).Scan(&g).Error err := db.Table("groups g").Select("g.*").Joins("left join user_groups ug ON g.id = ug.group_id").Where("ug.user_id=? and g.id=?", uid, id).Scan(&g).Error
/*err := Conn.SelectOne(&g, "SELECT g.id, g.name, g.modified_date FROM groups g, user_groups ug, users u WHERE ug.uid=u.id AND ug.gid=g.id AND g.id=? AND u.id=?", id, uid)*/
if err != nil { if err != nil {
Logger.Println(err) Logger.Println(err)
return g, err return g, err
} }
g.Targets, err = GetTargets(g.Id) g.Targets, err = GetTargets(g.Id)
/*_, err = Conn.Select(&g.Targets, "SELECT t.id, t.email FROM targets t, group_targets gt WHERE gt.gid=? AND gt.tid=t.id", g.Id)*/
if err != nil { if err != nil {
Logger.Println(err) Logger.Println(err)
} }
@ -67,14 +64,12 @@ func GetGroup(id int64, uid int64) (Group, error) {
// GetGroupByName returns the group, if it exists, specified by the given name and user_id. // GetGroupByName returns the group, if it exists, specified by the given name and user_id.
func GetGroupByName(n string, uid int64) (Group, error) { func GetGroupByName(n string, uid int64) (Group, error) {
g := Group{} g := Group{}
err := db.Debug().Table("groups g").Select("g.*").Joins("left join user_groups ug ON g.id = ug.group_id").Where("ug.user_id=? and g.name=?", uid, n).Scan(&g).Error err := db.Table("groups g").Select("g.*").Joins("left join user_groups ug ON g.id = ug.group_id").Where("ug.user_id=? and g.name=?", uid, n).Scan(&g).Error
/*err := Conn.SelectOne(&g, "SELECT g.id, g.name, g.modified_date FROM groups g, user_groups ug, users u WHERE ug.uid=u.id AND ug.gid=g.id AND g.name=? AND u.id=?", n, uid)*/
if err != nil { if err != nil {
Logger.Println(err) Logger.Println(err)
return g, err return g, err
} }
g.Targets, err = GetTargets(g.Id) g.Targets, err = GetTargets(g.Id)
/*_, err = Conn.Select(&g.Targets, "SELECT t.id, t.email FROM targets t, group_targets gt WHERE gt.gid=? AND gt.tid=t.id", g.Id)*/
if err != nil { if err != nil {
Logger.Println(err) Logger.Println(err)
} }
@ -84,7 +79,6 @@ func GetGroupByName(n string, uid int64) (Group, error) {
// PostGroup creates a new group in the database. // PostGroup creates a new group in the database.
func PostGroup(g *Group, uid int64) error { func PostGroup(g *Group, uid int64) error {
// Insert into the DB // Insert into the DB
/*err = Conn.Insert(g)*/
err = db.Save(g).Error err = db.Save(g).Error
if err != nil { if err != nil {
Logger.Println(err) Logger.Println(err)
@ -96,10 +90,6 @@ func PostGroup(g *Group, uid int64) error {
Logger.Println(err) Logger.Println(err)
return err return err
} }
/*_, err = Conn.Exec("INSERT OR IGNORE INTO user_groups VALUES (?,?)", uid, g.Id)
if err != nil {
Logger.Printf("Error adding many-many mapping for group %s\n", g.Name)
}*/
for _, t := range g.Targets { for _, t := range g.Targets {
insertTargetIntoGroup(t, g.Id) insertTargetIntoGroup(t, g.Id)
} }
@ -114,7 +104,6 @@ func PutGroup(g *Group, uid int64) error {
// Get all the targets currently in the database for the group // Get all the targets currently in the database for the group
ts := []Target{} ts := []Target{}
ts, err = GetTargets(g.Id) ts, err = GetTargets(g.Id)
/*_, err = Conn.Select(&ts, "SELECT t.id, t.email FROM targets t, group_targets gt WHERE gt.gid=? AND gt.tid=t.id", g.Id)*/
if err != nil { if err != nil {
Logger.Printf("Error getting targets from group ID: %d", g.Id) Logger.Printf("Error getting targets from group ID: %d", g.Id)
return err return err
@ -133,8 +122,7 @@ func PutGroup(g *Group, uid int64) error {
} }
// If the target does not exist in the group any longer, we delete it // If the target does not exist in the group any longer, we delete it
if !tExists { if !tExists {
err = db.Debug().Where("group_id=? and target_id=?", g.Id, t.Id).Delete(&GroupTarget{}).Error err = db.Where("group_id=? and target_id=?", g.Id, t.Id).Delete(&GroupTarget{}).Error
/*_, err = Conn.Exec("DELETE FROM group_targets WHERE gid=? AND tid=?", g.Id, t.Id)*/
if err != nil { if err != nil {
Logger.Printf("Error deleting email %s\n", t.Email) Logger.Printf("Error deleting email %s\n", t.Email)
} }
@ -158,7 +146,7 @@ func PutGroup(g *Group, uid int64) error {
} }
// Update the group // Update the group
g.ModifiedDate = time.Now() g.ModifiedDate = time.Now()
err = db.Debug().Save(g).Error err = db.Save(g).Error
/*_, err = Conn.Update(g)*/ /*_, err = Conn.Update(g)*/
if err != nil { if err != nil {
Logger.Println(err) Logger.Println(err)
@ -173,28 +161,20 @@ func insertTargetIntoGroup(t Target, gid int64) error {
return err return err
} }
trans := db.Begin() trans := db.Begin()
trans.Debug().Where(t).FirstOrCreate(&t) trans.Where(t).FirstOrCreate(&t)
Logger.Printf("ID of Target after FirstOrCreate: %d", t.Id) Logger.Printf("ID of Target after FirstOrCreate: %d", t.Id)
/*_, err = trans.Exec("INSERT OR IGNORE INTO targets VALUES (null, ?)", t.Email)*/
if err != nil { if err != nil {
Logger.Printf("Error adding target: %s\n", t.Email) Logger.Printf("Error adding target: %s\n", t.Email)
return err return err
} }
/*// Bug: res.LastInsertId() does not work for this, so we need to select it manually (how frustrating.) err = trans.Where("group_id=? and target_id=?", gid, t.Id).Find(&GroupTarget{}).Error
t.Id, err = trans.SelectInt("SELECT id FROM targets WHERE email=?", t.Email)
if err != nil {
Logger.Printf("Error getting id for email: %s\n", t.Email)
return err
}*/
err = trans.Debug().Where("group_id=? and target_id=?", gid, t.Id).Find(&GroupTarget{}).Error
if err == gorm.RecordNotFound { if err == gorm.RecordNotFound {
err = trans.Debug().Save(&GroupTarget{GroupId: gid, TargetId: t.Id}).Error err = trans.Save(&GroupTarget{GroupId: gid, TargetId: t.Id}).Error
if err != nil { if err != nil {
Logger.Println(err) Logger.Println(err)
return err return err
} }
} }
/*_, err = trans.Exec("INSERT OR IGNORE INTO group_targets VALUES (?,?)", gid, t.Id)*/
if err != nil { if err != nil {
Logger.Printf("Error adding many-many mapping for %s\n", t.Email) Logger.Printf("Error adding many-many mapping for %s\n", t.Email)
return err return err
@ -210,22 +190,19 @@ func insertTargetIntoGroup(t Target, gid int64) error {
// DeleteGroup deletes a given group by group ID and user ID // DeleteGroup deletes a given group by group ID and user ID
func DeleteGroup(id int64) error { func DeleteGroup(id int64) error {
// Delete all the group_targets entries for this group // Delete all the group_targets entries for this group
err := db.Debug().Where("group_id=?", id).Delete(&GroupTarget{}).Error err := db.Where("group_id=?", id).Delete(&GroupTarget{}).Error
/*_, err := Conn.Exec("DELETE FROM group_targets WHERE gid=?", id)*/
if err != nil { if err != nil {
Logger.Println(err) Logger.Println(err)
return err return err
} }
// Delete the reference to the group in the user_group table // Delete the reference to the group in the user_group table
err = db.Debug().Where("group_id=?", id).Delete(&UserGroup{}).Error err = db.Where("group_id=?", id).Delete(&UserGroup{}).Error
/*_, err = Conn.Exec("DELETE FROM user_groups WHERE gid=?", id)*/
if err != nil { if err != nil {
Logger.Println(err) Logger.Println(err)
return err return err
} }
// Delete the group itself // Delete the group itself
err = db.Debug().Delete(&Group{Id: id}).Error err = db.Delete(&Group{Id: id}).Error
/*_, err = Conn.Exec("DELETE FROM groups WHERE id=?", id)*/
if err != nil { if err != nil {
Logger.Println(err) Logger.Println(err)
return err return err
@ -235,7 +212,6 @@ func DeleteGroup(id int64) error {
func GetTargets(gid int64) ([]Target, error) { func GetTargets(gid int64) ([]Target, error) {
ts := []Target{} ts := []Target{}
err := db.Debug().Table("targets t").Select("t.id, t.email").Joins("left join group_targets gt ON t.id = gt.target_id").Where("gt.group_id=?", gid).Scan(&ts).Error err := db.Table("targets t").Select("t.id, t.email").Joins("left join group_targets gt ON t.id = gt.target_id").Where("gt.group_id=?", gid).Scan(&ts).Error
/*_, err := Conn.Select(&gs[i].Targets, "SELECT t.id, t.email FROM targets t, group_targets gt WHERE gt.gid=? AND gt.tid=t.id", gs[i].Id)*/
return ts, err return ts, err
} }

View File

@ -5,16 +5,15 @@ import "database/sql"
// User represents the user model for gophish. // User represents the user model for gophish.
type User struct { type User struct {
Id int64 `json:"id"` Id int64 `json:"id"`
Username string `json:"username"` /* sql:"not null;unique"`*/ Username string `json:"username" sql:"not null;unique"`
Hash string `json:"-"` Hash string `json:"-"`
ApiKey string `json:"api_key" db:"api_key"` /* sql:"not null;unique"`*/ ApiKey string `json:"api_key" sql:"not null;unique"`
} }
// GetUser returns the user that the given id corresponds to. If no user is found, an // GetUser returns the user that the given id corresponds to. If no user is found, an
// error is thrown. // error is thrown.
func GetUser(id int64) (User, error) { func GetUser(id int64) (User, error) {
u := User{} u := User{}
/* err := Conn.SelectOne(&u, "SELECT * FROM Users WHERE id=?", id)*/
err := db.Where("id=?", id).First(&u).Error err := db.Where("id=?", id).First(&u).Error
if err != nil { if err != nil {
return u, err return u, err
@ -26,8 +25,7 @@ func GetUser(id int64) (User, error) {
// error is thrown. // error is thrown.
func GetUserByAPIKey(key string) (User, error) { func GetUserByAPIKey(key string) (User, error) {
u := User{} u := User{}
/* err := Conn.SelectOne(&u, "SELECT id, username, api_key FROM Users WHERE apikey=?", key)*/ err := db.Where("api_key = ?", key).First(&u).Error
err := db.Debug().Where("api_key = ?", key).First(&u).Error
if err != nil { if err != nil {
return u, err return u, err
} }
@ -38,7 +36,6 @@ func GetUserByAPIKey(key string) (User, error) {
// error is thrown. // error is thrown.
func GetUserByUsername(username string) (User, error) { func GetUserByUsername(username string) (User, error) {
u := User{} u := User{}
/* err := Conn.SelectOne(&u, "SELECT * FROM Users WHERE username=?", username)*/
err := db.Where("username = ?", username).First(&u).Error err := db.Where("username = ?", username).First(&u).Error
if err != sql.ErrNoRows { if err != sql.ErrNoRows {
return u, ErrUsernameTaken return u, ErrUsernameTaken
@ -50,7 +47,6 @@ func GetUserByUsername(username string) (User, error) {
// PutUser updates the given user // PutUser updates the given user
func PutUser(u *User) error { func PutUser(u *User) error {
err := db.Debug().Update(&u).Error err := db.Update(&u).Error
/*_, err := Conn.Update(u)*/
return err return err
} }

View File

@ -27,7 +27,7 @@
<div class="row"> <div class="row">
<label for="api_key" class="col-sm-2 control-label form-label">API Key:</label> <label for="api_key" class="col-sm-2 control-label form-label">API Key:</label>
<div class="col-md-6"> <div class="col-md-6">
<input type="text" id="api_key" onclick="this.select();" value="{{%.User.APIKey%}}" class="form-control" readonly/> <input type="text" id="api_key" onclick="this.select();" value="{{%.User.ApiKey%}}" class="form-control" readonly/>
</div> </div>
<form action="/api/reset" method="POST"> <form action="/api/reset" method="POST">
<button class="btn btn-primary"><i class="fa fa-refresh" type="submit"></i> Reset</button> <button class="btn btn-primary"><i class="fa fa-refresh" type="submit"></i> Reset</button>