mirror of https://github.com/gophish/gophish
Campaigns: Refactored user assignment
parent
8e79294413
commit
63f0549efb
|
@ -536,77 +536,8 @@ func PostCampaign(c *Campaign, uid int64) error {
|
|||
if err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
// Insert all the results
|
||||
resultMap := make(map[string]bool)
|
||||
recipientIndex := 0
|
||||
tx := db.Begin()
|
||||
for _, g := range c.Groups {
|
||||
// Insert a result for each target in the group
|
||||
for _, t := range g.Targets {
|
||||
// Remove duplicate results - we should only
|
||||
// send emails to unique email addresses.
|
||||
if _, ok := resultMap[t.Email]; ok {
|
||||
continue
|
||||
}
|
||||
resultMap[t.Email] = true
|
||||
sendDate := c.generateSendDate(recipientIndex, totalRecipients)
|
||||
r := &Result{
|
||||
BaseRecipient: BaseRecipient{
|
||||
Email: t.Email,
|
||||
Position: t.Position,
|
||||
FirstName: t.FirstName,
|
||||
LastName: t.LastName,
|
||||
},
|
||||
Status: StatusScheduled,
|
||||
CampaignId: c.Id,
|
||||
UserId: c.UserId,
|
||||
SendDate: sendDate,
|
||||
Reported: false,
|
||||
ModifiedDate: c.CreatedDate,
|
||||
}
|
||||
err = r.GenerateId(tx)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
processing := false
|
||||
if r.SendDate.Before(c.CreatedDate) || r.SendDate.Equal(c.CreatedDate) {
|
||||
r.Status = StatusSending
|
||||
processing = true
|
||||
}
|
||||
err = tx.Save(r).Error
|
||||
if err != nil {
|
||||
log.WithFields(logrus.Fields{
|
||||
"email": t.Email,
|
||||
}).Errorf("error creating result: %v", err)
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
c.Results = append(c.Results, *r)
|
||||
log.WithFields(logrus.Fields{
|
||||
"email": r.Email,
|
||||
"send_date": sendDate,
|
||||
}).Debug("creating maillog")
|
||||
m := &MailLog{
|
||||
UserId: c.UserId,
|
||||
CampaignId: c.Id,
|
||||
RId: r.RId,
|
||||
SendDate: sendDate,
|
||||
Processing: processing,
|
||||
}
|
||||
err = tx.Save(m).Error
|
||||
if err != nil {
|
||||
log.WithFields(logrus.Fields{
|
||||
"email": t.Email,
|
||||
}).Errorf("error creating maillog entry: %v", err)
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
recipientIndex++
|
||||
}
|
||||
}
|
||||
return tx.Commit().Error
|
||||
// Import the users
|
||||
return UpdateUsers(c, totalRecipients)
|
||||
}
|
||||
|
||||
//DeleteCampaign deletes the specified campaign
|
||||
|
@ -668,3 +599,78 @@ func CompleteCampaign(id int64, uid int64) error {
|
|||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// UpdateUsers updates the specified campaign's user assignments.
|
||||
func UpdateUsers(c *Campaign, totalRecipients int) error {
|
||||
resultMap := make(map[string]bool)
|
||||
recipientIndex := 0
|
||||
tx := db.Begin()
|
||||
for _, g := range c.Groups {
|
||||
// Insert a result for each target in the group
|
||||
for _, t := range g.Targets {
|
||||
// Remove duplicate results - we should only
|
||||
// send emails to unique email addresses.
|
||||
if _, ok := resultMap[t.Email]; ok {
|
||||
continue
|
||||
}
|
||||
resultMap[t.Email] = true
|
||||
sendDate := c.generateSendDate(recipientIndex, totalRecipients)
|
||||
r := &Result{
|
||||
BaseRecipient: BaseRecipient{
|
||||
Email: t.Email,
|
||||
Position: t.Position,
|
||||
FirstName: t.FirstName,
|
||||
LastName: t.LastName,
|
||||
},
|
||||
Status: StatusScheduled,
|
||||
CampaignId: c.Id,
|
||||
UserId: c.UserId,
|
||||
SendDate: sendDate,
|
||||
Reported: false,
|
||||
ModifiedDate: c.CreatedDate,
|
||||
}
|
||||
err := r.GenerateId(tx)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
processing := false
|
||||
if r.SendDate.Before(c.CreatedDate) || r.SendDate.Equal(c.CreatedDate) {
|
||||
r.Status = StatusSending
|
||||
processing = true
|
||||
}
|
||||
err = tx.Save(r).Error
|
||||
if err != nil {
|
||||
log.WithFields(logrus.Fields{
|
||||
"email": t.Email,
|
||||
}).Errorf("error creating result: %v", err)
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
c.Results = append(c.Results, *r)
|
||||
log.WithFields(logrus.Fields{
|
||||
"email": r.Email,
|
||||
"send_date": sendDate,
|
||||
}).Debug("creating maillog")
|
||||
m := &MailLog{
|
||||
UserId: c.UserId,
|
||||
CampaignId: c.Id,
|
||||
RId: r.RId,
|
||||
SendDate: sendDate,
|
||||
Processing: processing,
|
||||
}
|
||||
err = tx.Save(m).Error
|
||||
if err != nil {
|
||||
log.WithFields(logrus.Fields{
|
||||
"email": t.Email,
|
||||
}).Errorf("error creating maillog entry: %v", err)
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
recipientIndex++
|
||||
}
|
||||
}
|
||||
|
||||
return tx.Commit().Error
|
||||
}
|
Loading…
Reference in New Issue