Adding logic to handle getting the template for a campaign

Added SMTP Model
Added better flash support in controllers.js
Added SMTP Options accordion in campaign modal
pull/24/head
Jordan 2014-06-03 13:27:20 -05:00
parent af44dbb07c
commit db24496fb0
6 changed files with 131 additions and 25 deletions

View File

@ -1,9 +1,10 @@
package models package models
import ( import (
"database/sql"
"fmt" "fmt"
"time" "time"
"github.com/jinzhu/gorm"
) )
//Campaign is a struct representing a created campaign //Campaign is a struct representing a created campaign
@ -13,10 +14,24 @@ type Campaign struct {
Name string `json:"name" sql:"not null"` Name string `json:"name" sql:"not null"`
CreatedDate time.Time `json:"created_date"` CreatedDate time.Time `json:"created_date"`
CompletedDate time.Time `json:"completed_date"` CompletedDate time.Time `json:"completed_date"`
TemplateId int64 `json:"-"`
Template Template `json:"template"` //This may change Template Template `json:"template"` //This may change
Status string `json:"status"` Status string `json:"status"`
Results []Result `json:"results,omitempty"` Results []Result `json:"results,omitempty"`
Groups []Group `json:"groups,omitempty"` Groups []Group `json:"groups,omitempty"`
SMTP SMTP `json:"options,omitempty"`
}
func (c *Campaign) Validate() (string, bool) {
switch {
case c.Name == "":
return "Must specify campaign name", false
case len(c.Groups) == 0:
return "No groups specified", false
case c.Template.Name == "":
return "No template specified", false
}
return "", true
} }
type Result struct { type Result struct {
@ -58,7 +73,7 @@ func PostCampaign(c *Campaign, uid int64) error {
// Check to make sure all the groups already exist // Check to make sure all the groups already exist
for i, g := range c.Groups { for i, g := range c.Groups {
c.Groups[i], err = GetGroupByName(g.Name, uid) c.Groups[i], err = GetGroupByName(g.Name, uid)
if err == sql.ErrNoRows { if err == gorm.RecordNotFound {
Logger.Printf("Error - Group %s does not exist", g.Name) Logger.Printf("Error - Group %s does not exist", g.Name)
return err return err
} else if err != nil { } else if err != nil {
@ -66,6 +81,17 @@ func PostCampaign(c *Campaign, uid int64) error {
return err return err
} }
} }
// Check to make sure the template exists
t, err := GetTemplateByName(c.Template.Name, uid)
if err == gorm.RecordNotFound {
Logger.Printf("Error - Template %s does not exist", t.Name)
return err
} else if err != nil {
Logger.Println(err)
return err
}
c.TemplateId = t.Id
// Insert into the DB // Insert into the DB
err = db.Save(c).Error err = db.Save(c).Error
if err != nil { if err != nil {
@ -109,13 +135,3 @@ func DeleteCampaign(id int64) error {
} }
return err return err
} }
func ValidateCampaign(c *Campaign) (string, bool) {
if c.Name == "" {
return "Must specify campaign name", false
}
if len(c.Groups) == 0 {
return "No groups specified", false
}
return "", true
}

View File

@ -56,6 +56,7 @@ func Setup() error {
db.CreateTable(Group{}) db.CreateTable(Group{})
db.CreateTable(GroupTarget{}) db.CreateTable(GroupTarget{})
db.CreateTable(Template{}) db.CreateTable(Template{})
db.CreateTable(SMTP{})
db.CreateTable(Campaign{}) db.CreateTable(Campaign{})
//Create the default user //Create the default user
init_user := User{ init_user := User{

26
models/smtp.go Normal file
View File

@ -0,0 +1,26 @@
package models
type SMTP struct {
SMTPId int64 `json:"-"`
CampaignId int64 `json:"-"`
Hostname string `json:"hostname"`
Port int `json:"port"`
UseAuth bool `json:"use_auth"`
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty" sql:"-"`
FromAddress string `json:"from_address"`
}
func (s *SMTP) Validate() (string, bool) {
switch {
case s.UseAuth == false && (s.Username == "" && s.Password == ""):
return "Auth requested, but username or password blank", false
case s.FromAddress == "":
return "No from address specified", false
case s.Hostname == "":
return "No hostname specified", false
case s.Port == 0:
return "No port specified", false
}
return "", true
}

View File

@ -98,6 +98,7 @@ app.controller('CampaignCtrl', function($scope, $modal, CampaignService, GroupSe
deleteCampaign.$delete({ deleteCampaign.$delete({
id: deleteCampaign.id id: deleteCampaign.id
}, function() { }, function() {
$scope.successFlash("Campaign deleted successfully")
$scope.mainTableParams.reload(); $scope.mainTableParams.reload();
}); });
} }
@ -157,6 +158,23 @@ app.controller('CampaignResultsCtrl', function($scope, CampaignService, GroupSer
}); });
app.controller('GroupCtrl', function($scope, $modal, GroupService, ngTableParams) { app.controller('GroupCtrl', function($scope, $modal, GroupService, ngTableParams) {
$scope.errorFlash = function(message) {
$scope.flashes = [];
$scope.flashes.push({
"type": "danger",
"message": message,
"icon": "fa-exclamation-circle"
})
}
$scope.successFlash = function(message) {
$scope.flashes = [];
$scope.flashes.push({
"type": "success",
"message": message,
"icon": "fa-check-circle"
})
}
$scope.mainTableParams = new ngTableParams({ $scope.mainTableParams = new ngTableParams({
page: 1, // show first page page: 1, // show first page
count: 10, // count per page count: 10, // count per page
@ -254,9 +272,31 @@ var GroupModalCtrl = function($scope, $modalInstance) {
$scope.cancel = function() { $scope.cancel = function() {
$modalInstance.dismiss('cancel'); $modalInstance.dismiss('cancel');
}; };
$scope.ok = function(group) {
$modalInstance.dismiss('')
$scope.saveGroup(group)
};
} }
app.controller('TemplateCtrl', function($scope, $modal, TemplateService, ngTableParams) { app.controller('TemplateCtrl', function($scope, $modal, TemplateService, ngTableParams) {
$scope.errorFlash = function(message) {
$scope.flashes = [];
$scope.flashes.push({
"type": "danger",
"message": message,
"icon": "fa-exclamation-circle"
})
}
$scope.successFlash = function(message) {
$scope.flashes = [];
$scope.flashes.push({
"type": "success",
"message": message,
"icon": "fa-check-circle"
})
}
$scope.mainTableParams = new ngTableParams({ $scope.mainTableParams = new ngTableParams({
page: 1, // show first page page: 1, // show first page
count: 10, // count per page count: 10, // count per page
@ -322,7 +362,12 @@ app.controller('TemplateCtrl', function($scope, $modal, TemplateService, ngTable
var deleteTemplate = new TemplateService(template); var deleteTemplate = new TemplateService(template);
deleteTemplate.$delete({ deleteTemplate.$delete({
id: deleteTemplate.id id: deleteTemplate.id
}, function() { }, function(response) {
if (response.success) {
$scope.successFlash(response.message)
} else {
$scope.errorFlash(response.message)
}
$scope.mainTableParams.reload(); $scope.mainTableParams.reload();
}); });
} }
@ -367,10 +412,12 @@ app.controller('SettingsCtrl', function($scope, $http, $window) {
'Content-Type': 'application/x-www-form-urlencoded' 'Content-Type': 'application/x-www-form-urlencoded'
} // set the headers so angular passing info as form data (not request payload) } // set the headers so angular passing info as form data (not request payload)
}) })
.success(function(data) { .success(function(response) {
$scope.user.api_key = data; if (response.success) {
$window.user.api_key = data; $scope.user.api_key = response.data;
$scope.successFlash("API Key Successfully Reset") $window.user.api_key = response.data;
$scope.successFlash(response.message)
}
}) })
} }
$scope.save_settings = function(){ $scope.save_settings = function(){

View File

@ -9,14 +9,30 @@
<input type="text" class="form-control" ng-model="campaign.name" id="name" placeholder="Campaign name" autofocus> <input type="text" class="form-control" ng-model="campaign.name" id="name" placeholder="Campaign name" autofocus>
<br /> <br />
<label class="control-label" for="template">Template:</label> <label class="control-label" for="template">Template:</label>
<input type="text" class="form-control" placeholder="Template Name" id="template" typeahead="template.name for template in templates | filter:{name:$viewValue}" typeahead-editable="false" ng-model="template.name" /> <input type="text" class="form-control" placeholder="Template Name" id="template" typeahead="template.name for template in templates | filter:{name:$viewValue}" typeahead-editable="false" ng-model="campaign.template.name" />
<br /> <br />
<accordian>
<accordion-group>
<accordion-heading>
SMTP Options <i class="pull-right glyphicon" ng-class="{'glyphicon-chevron-down': isOpen, 'glyphicon-chevron-right': !isOpen}"></i>
</accordion-heading>
<label class="control-label" for="from">From:</label> <label class="control-label" for="from">From:</label>
<input type="text" class="form-control" placeholder="First Last <test@example.com>" id="form"> <input type="text" class="form-control" placeholder="First Last <test@example.com>" id="form">
<br /> <br />
<label class="control-label" for="smtp_server">SMTP Server:</label> <label class="control-label" for="smtp_server">Hostname:</label>
<input type="text" class="form-control" placeholder="host:port" id="smtp_server"> <input type="text" class="form-control" ng-model="campaign.smtp.hostname" placeholder="host:port" id="smtp_server">
<br /> <br />
<label class="control-label" for="smtp_server">Port:</label>
<input type="text" class="form-control" ng-model="campaign.smtp.port" placeholder="host:port" id="smtp_server">
<br />
<label class="control-label" for="smtp_server">Username:</label>
<input type="text" class="form-control" ng-model="campaign.smtp.username" placeholder="host:port" id="smtp_server">
<br />
<label class="control-label" for="smtp_server">Password:</label>
<input type="password" class="form-control" ng-model="campaign.smtp.password" id="smtp_server">
<br />
</accordion-group>
</accordion>
<label class="control-label" for="users">Groups:</label> <label class="control-label" for="users">Groups:</label>
<form ng-submit="addGroup(group)"> <form ng-submit="addGroup(group)">
<div class="input-group"> <div class="input-group">

View File

@ -38,6 +38,6 @@
</div> </div>
<div class="modal-footer"> <div class="modal-footer">
<button type="button" class="btn btn-default" ng-click="cancel()">Cancel</button> <button type="button" class="btn btn-default" ng-click="cancel()">Cancel</button>
<button type="button" class="btn btn-primary" ng-click="saveGroup(group)" data-dismiss="modal">Save Group</button> <button type="button" class="btn btn-primary" ng-click="ok(group)" data-dismiss="modal">Save Group</button>
</div> </div>
</div> </div>