mirror of https://github.com/gophish/gophish
parent
5740ee3273
commit
776f591f68
|
@ -29,18 +29,15 @@ type Result struct {
|
|||
// GetCampaigns returns the campaigns owned by the given user.
|
||||
func GetCampaigns(uid int64) ([]Campaign, error) {
|
||||
cs := []Campaign{}
|
||||
err := db.Debug().Model(&User{Id: uid}).Related(&cs).Error
|
||||
err := db.Model(&User{Id: uid}).Related(&cs).Error
|
||||
if err != nil {
|
||||
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 {
|
||||
err := db.Debug().Model(&cs[i]).Related(&cs[i].Results).Error
|
||||
err := db.Model(&cs[i]).Related(&cs[i].Results).Error
|
||||
if err != nil {
|
||||
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)
|
||||
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.
|
||||
func GetCampaign(id int64, uid int64) (Campaign, error) {
|
||||
c := Campaign{}
|
||||
err := db.Debug().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)
|
||||
err := db.Where("id = ?", id).Where("user_id = ?", uid).Find(&c).Error
|
||||
*/if err != nil {
|
||||
return c, err
|
||||
}
|
||||
err = db.Debug().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)
|
||||
err = db.Model(&c).Related(&c.Results).Error
|
||||
*/return c, err
|
||||
}
|
||||
|
||||
|
@ -73,8 +68,7 @@ func PostCampaign(c *Campaign, uid int64) error {
|
|||
}
|
||||
}
|
||||
// Insert into the DB
|
||||
/*err = Conn.Insert(c)*/
|
||||
err = db.Debug().Save(c).Error
|
||||
err = db.Save(c).Error
|
||||
if err != nil {
|
||||
Logger.Println(err)
|
||||
return err
|
||||
|
@ -86,37 +80,29 @@ func PostCampaign(c *Campaign, uid int64) error {
|
|||
r := Result{Email: t.Email, Status: "Unknown", CampaignId: c.Id}
|
||||
c.Results = append(c.Results, r)
|
||||
fmt.Printf("%v", c.Results)
|
||||
err := db.Debug().Save(&r).Error
|
||||
/*_, err = Conn.Exec("INSERT INTO campaign_results VALUES (?,?,?)", c.Id, r.Email, r.Status)*/
|
||||
err := db.Save(&r).Error
|
||||
if err != nil {
|
||||
Logger.Printf("Error adding result record for target %s\n", t.Email)
|
||||
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
|
||||
}
|
||||
|
||||
//DeleteCampaign deletes the specified campaign
|
||||
func DeleteCampaign(id int64) error {
|
||||
// Delete all the campaign_results entries for this group
|
||||
err := db.Debug().Delete(&Result{CampaignId: id}).Error
|
||||
/*_, err := Conn.Exec("DELETE FROM campaign_results WHERE cid=?", id)*/
|
||||
// Delete all the campaign results
|
||||
err := db.Delete(&Result{CampaignId: id}).Error
|
||||
if err != nil {
|
||||
Logger.Println(err)
|
||||
return err
|
||||
}
|
||||
// Delete the reference to the campaign in the user_campaigns table
|
||||
err = db.Debug().Delete(&Campaign{Id: id}).Error
|
||||
/*_, err = Conn.Exec("DELETE FROM user_campaigns WHERE cid=?", id)*/
|
||||
// Delete the campaign
|
||||
err = db.Delete(&Campaign{Id: id}).Error
|
||||
if err != nil {
|
||||
Logger.Panicln(err)
|
||||
return err
|
||||
}
|
||||
// Delete the campaign itself
|
||||
/*_, err = Conn.Exec("DELETE FROM campaigns WHERE id=?", id)*/
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -32,8 +32,7 @@ type Target struct {
|
|||
// GetGroups returns the groups owned by the given user.
|
||||
func GetGroups(uid int64) ([]Group, error) {
|
||||
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 := 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)*/
|
||||
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
|
||||
if err != nil {
|
||||
Logger.Println(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.
|
||||
func GetGroup(id int64, uid int64) (Group, error) {
|
||||
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 := 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)*/
|
||||
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
|
||||
if err != nil {
|
||||
Logger.Println(err)
|
||||
return g, err
|
||||
}
|
||||
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 {
|
||||
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.
|
||||
func GetGroupByName(n string, uid int64) (Group, error) {
|
||||
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 := 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)*/
|
||||
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
|
||||
if err != nil {
|
||||
Logger.Println(err)
|
||||
return g, err
|
||||
}
|
||||
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 {
|
||||
Logger.Println(err)
|
||||
}
|
||||
|
@ -84,7 +79,6 @@ func GetGroupByName(n string, uid int64) (Group, error) {
|
|||
// PostGroup creates a new group in the database.
|
||||
func PostGroup(g *Group, uid int64) error {
|
||||
// Insert into the DB
|
||||
/*err = Conn.Insert(g)*/
|
||||
err = db.Save(g).Error
|
||||
if err != nil {
|
||||
Logger.Println(err)
|
||||
|
@ -96,10 +90,6 @@ func PostGroup(g *Group, uid int64) error {
|
|||
Logger.Println(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 {
|
||||
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
|
||||
ts := []Target{}
|
||||
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 {
|
||||
Logger.Printf("Error getting targets from group ID: %d", g.Id)
|
||||
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 !tExists {
|
||||
err = db.Debug().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)*/
|
||||
err = db.Where("group_id=? and target_id=?", g.Id, t.Id).Delete(&GroupTarget{}).Error
|
||||
if err != nil {
|
||||
Logger.Printf("Error deleting email %s\n", t.Email)
|
||||
}
|
||||
|
@ -158,7 +146,7 @@ func PutGroup(g *Group, uid int64) error {
|
|||
}
|
||||
// Update the group
|
||||
g.ModifiedDate = time.Now()
|
||||
err = db.Debug().Save(g).Error
|
||||
err = db.Save(g).Error
|
||||
/*_, err = Conn.Update(g)*/
|
||||
if err != nil {
|
||||
Logger.Println(err)
|
||||
|
@ -173,28 +161,20 @@ func insertTargetIntoGroup(t Target, gid int64) error {
|
|||
return err
|
||||
}
|
||||
trans := db.Begin()
|
||||
trans.Debug().Where(t).FirstOrCreate(&t)
|
||||
trans.Where(t).FirstOrCreate(&t)
|
||||
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 {
|
||||
Logger.Printf("Error adding target: %s\n", t.Email)
|
||||
return err
|
||||
}
|
||||
/*// Bug: res.LastInsertId() does not work for this, so we need to select it manually (how frustrating.)
|
||||
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
|
||||
err = trans.Where("group_id=? and target_id=?", gid, t.Id).Find(&GroupTarget{}).Error
|
||||
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 {
|
||||
Logger.Println(err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
/*_, err = trans.Exec("INSERT OR IGNORE INTO group_targets VALUES (?,?)", gid, t.Id)*/
|
||||
if err != nil {
|
||||
Logger.Printf("Error adding many-many mapping for %s\n", t.Email)
|
||||
return err
|
||||
|
@ -210,22 +190,19 @@ func insertTargetIntoGroup(t Target, gid int64) error {
|
|||
// DeleteGroup deletes a given group by group ID and user ID
|
||||
func DeleteGroup(id int64) error {
|
||||
// Delete all the group_targets entries for this group
|
||||
err := db.Debug().Where("group_id=?", id).Delete(&GroupTarget{}).Error
|
||||
/*_, err := Conn.Exec("DELETE FROM group_targets WHERE gid=?", id)*/
|
||||
err := db.Where("group_id=?", id).Delete(&GroupTarget{}).Error
|
||||
if err != nil {
|
||||
Logger.Println(err)
|
||||
return err
|
||||
}
|
||||
// Delete the reference to the group in the user_group table
|
||||
err = db.Debug().Where("group_id=?", id).Delete(&UserGroup{}).Error
|
||||
/*_, err = Conn.Exec("DELETE FROM user_groups WHERE gid=?", id)*/
|
||||
err = db.Where("group_id=?", id).Delete(&UserGroup{}).Error
|
||||
if err != nil {
|
||||
Logger.Println(err)
|
||||
return err
|
||||
}
|
||||
// Delete the group itself
|
||||
err = db.Debug().Delete(&Group{Id: id}).Error
|
||||
/*_, err = Conn.Exec("DELETE FROM groups WHERE id=?", id)*/
|
||||
err = db.Delete(&Group{Id: id}).Error
|
||||
if err != nil {
|
||||
Logger.Println(err)
|
||||
return err
|
||||
|
@ -235,7 +212,6 @@ func DeleteGroup(id int64) error {
|
|||
|
||||
func GetTargets(gid int64) ([]Target, error) {
|
||||
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 := 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)*/
|
||||
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
|
||||
return ts, err
|
||||
}
|
||||
|
|
|
@ -5,16 +5,15 @@ import "database/sql"
|
|||
// User represents the user model for gophish.
|
||||
type User struct {
|
||||
Id int64 `json:"id"`
|
||||
Username string `json:"username"` /* sql:"not null;unique"`*/
|
||||
Username string `json:"username" sql:"not null;unique"`
|
||||
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
|
||||
// error is thrown.
|
||||
func GetUser(id int64) (User, error) {
|
||||
u := User{}
|
||||
/* err := Conn.SelectOne(&u, "SELECT * FROM Users WHERE id=?", id)*/
|
||||
err := db.Where("id=?", id).First(&u).Error
|
||||
if err != nil {
|
||||
return u, err
|
||||
|
@ -26,8 +25,7 @@ func GetUser(id int64) (User, error) {
|
|||
// error is thrown.
|
||||
func GetUserByAPIKey(key string) (User, error) {
|
||||
u := User{}
|
||||
/* err := Conn.SelectOne(&u, "SELECT id, username, api_key FROM Users WHERE apikey=?", key)*/
|
||||
err := db.Debug().Where("api_key = ?", key).First(&u).Error
|
||||
err := db.Where("api_key = ?", key).First(&u).Error
|
||||
if err != nil {
|
||||
return u, err
|
||||
}
|
||||
|
@ -38,7 +36,6 @@ func GetUserByAPIKey(key string) (User, error) {
|
|||
// error is thrown.
|
||||
func GetUserByUsername(username string) (User, error) {
|
||||
u := User{}
|
||||
/* err := Conn.SelectOne(&u, "SELECT * FROM Users WHERE username=?", username)*/
|
||||
err := db.Where("username = ?", username).First(&u).Error
|
||||
if err != sql.ErrNoRows {
|
||||
return u, ErrUsernameTaken
|
||||
|
@ -50,7 +47,6 @@ func GetUserByUsername(username string) (User, error) {
|
|||
|
||||
// PutUser updates the given user
|
||||
func PutUser(u *User) error {
|
||||
err := db.Debug().Update(&u).Error
|
||||
/*_, err := Conn.Update(u)*/
|
||||
err := db.Update(&u).Error
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -27,7 +27,7 @@
|
|||
<div class="row">
|
||||
<label for="api_key" class="col-sm-2 control-label form-label">API Key:</label>
|
||||
<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>
|
||||
<form action="/api/reset" method="POST">
|
||||
<button class="btn btn-primary"><i class="fa fa-refresh" type="submit"></i> Reset</button>
|
||||
|
|
Loading…
Reference in New Issue