mirror of https://github.com/gophish/gophish
Changed templates to have a 1-1 relationship with user (will implement sharing differently later)
Working on implementing /api/template/:id methods Removed API_Campaigns_Id_Launch method (will just POST using template from angularjs scope later)pull/24/head
parent
6ec45ab927
commit
25cbaf92ce
|
@ -124,10 +124,6 @@ func API_Campaigns_Id(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func API_Campaigns_Id_Launch(w http.ResponseWriter, r *http.Request) {
|
|
||||||
http.Redirect(w, r, "/", 302)
|
|
||||||
}
|
|
||||||
|
|
||||||
// API_Groups returns details about the requested group. If the campaign is not
|
// API_Groups returns details about the requested group. If the campaign is not
|
||||||
// valid, API_Groups returns null.
|
// valid, API_Groups returns null.
|
||||||
// Example:
|
// Example:
|
||||||
|
@ -267,7 +263,8 @@ func API_Templates(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
t.ModifiedDate = time.Now()
|
t.ModifiedDate = time.Now()
|
||||||
err = models.PostTemplate(&t, ctx.Get(r, "user_id").(int64))
|
t.UserId = ctx.Get(r, "user_id").(int64)
|
||||||
|
err = models.PostTemplate(&t)
|
||||||
if checkError(err, w, "Error inserting template", http.StatusInternalServerError) {
|
if checkError(err, w, "Error inserting template", http.StatusInternalServerError) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -280,7 +277,47 @@ func API_Templates(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func API_Templates_Id(w http.ResponseWriter, r *http.Request) {
|
func API_Templates_Id(w http.ResponseWriter, r *http.Request) {
|
||||||
http.Redirect(w, r, "/", 302)
|
vars := mux.Vars(r)
|
||||||
|
id, _ := strconv.ParseInt(vars["id"], 0, 64)
|
||||||
|
switch {
|
||||||
|
case r.Method == "GET":
|
||||||
|
t, err := models.GetTemplate(id, ctx.Get(r, "user_id").(int64))
|
||||||
|
if checkError(err, w, "No template found", http.StatusNotFound) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
tj, err := json.MarshalIndent(t, "", " ")
|
||||||
|
if checkError(err, w, "Error creating JSON response", http.StatusInternalServerError) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
writeJSON(w, tj)
|
||||||
|
case r.Method == "DELETE":
|
||||||
|
err := models.DeleteTemplate(id, ctx.Get(r, "user_id").(int64))
|
||||||
|
if checkError(err, w, "Error deleting group", http.StatusInternalServerError) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
writeJSON(w, []byte("{\"success\" : \"true\"}"))
|
||||||
|
case r.Method == "PUT":
|
||||||
|
_, err := models.GetTemplate(id, ctx.Get(r, "user_id").(int64))
|
||||||
|
if checkError(err, w, "No group found", http.StatusNotFound) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
t := models.Template{}
|
||||||
|
err = json.NewDecoder(r.Body).Decode(&t)
|
||||||
|
if t.Id != id {
|
||||||
|
http.Error(w, "Error: /:id and template_id mismatch", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
t.ModifiedDate = time.Now()
|
||||||
|
err = models.PutTemplate(&t, ctx.Get(r, "user_id").(int64))
|
||||||
|
if checkError(err, w, "Error updating group", http.StatusInternalServerError) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
tj, err := json.MarshalIndent(t, "", " ")
|
||||||
|
if checkError(err, w, "Error creating JSON response", http.StatusInternalServerError) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
writeJSON(w, tj)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeJSON(w http.ResponseWriter, c []byte) {
|
func writeJSON(w http.ResponseWriter, c []byte) {
|
||||||
|
|
|
@ -50,7 +50,6 @@ func Setup() error {
|
||||||
db.CreateTable(Group{})
|
db.CreateTable(Group{})
|
||||||
db.CreateTable(GroupTarget{})
|
db.CreateTable(GroupTarget{})
|
||||||
db.CreateTable(Template{})
|
db.CreateTable(Template{})
|
||||||
db.CreateTable(UserTemplate{})
|
|
||||||
db.CreateTable(Campaign{})
|
db.CreateTable(Campaign{})
|
||||||
//Create the default user
|
//Create the default user
|
||||||
init_user := User{
|
init_user := User{
|
||||||
|
|
|
@ -4,6 +4,7 @@ import "time"
|
||||||
|
|
||||||
type Template struct {
|
type Template struct {
|
||||||
Id int64 `json:"id"`
|
Id int64 `json:"id"`
|
||||||
|
UserId int64 `json:"-"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Text string `json:"text"`
|
Text string `json:"text"`
|
||||||
Html string `json:"html"`
|
Html string `json:"html"`
|
||||||
|
@ -18,32 +19,45 @@ type UserTemplate struct {
|
||||||
// GetTemplates returns the templates owned by the given user.
|
// GetTemplates returns the templates owned by the given user.
|
||||||
func GetTemplates(uid int64) ([]Template, error) {
|
func GetTemplates(uid int64) ([]Template, error) {
|
||||||
ts := []Template{}
|
ts := []Template{}
|
||||||
err := db.Table("templates t").Select("t.*").Joins("left join user_templates ut ON t.id = ut.template_id").Where("ut.user_id=?", uid).Scan(&ts).Error
|
err := db.Where("user_id=?", uid).Find(&ts).Error
|
||||||
|
if err != nil {
|
||||||
|
Logger.Println(err)
|
||||||
|
return ts, err
|
||||||
|
}
|
||||||
return ts, err
|
return ts, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetTemplate returns the template, if it exists, specified by the given id and user_id.
|
// GetTemplate returns the template, if it exists, specified by the given id and user_id.
|
||||||
func GetTemplate(id int64, uid int64) (Template, error) {
|
func GetTemplate(id int64, uid int64) (Template, error) {
|
||||||
t := Template{}
|
t := Template{}
|
||||||
err := db.Table("templates t").Select("t.*").Joins("left join user_templates ut ON t.id = ut.template_id").Where("ut.user_id=? and t.id=?", uid, id).Scan(&t).Error
|
err := db.Where("user_id=? and id=?", uid, id).Find(&t).Error
|
||||||
|
if err != nil {
|
||||||
|
Logger.Println(err)
|
||||||
|
return t, err
|
||||||
|
}
|
||||||
return t, err
|
return t, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// PostTemplate creates a new template in the database.
|
// PostTemplate creates a new template in the database.
|
||||||
func PostTemplate(t *Template, uid int64) error {
|
func PostTemplate(t *Template) error {
|
||||||
// Insert into the DB
|
// Insert into the DB
|
||||||
err := db.Save(t).Error
|
err := db.Save(t).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Logger.Println(err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Now, let's add the user->user_templates->template mapping
|
|
||||||
err = db.Exec("INSERT OR IGNORE INTO user_templates VALUES (?,?)", uid, t.Id).Error
|
|
||||||
if err != nil {
|
|
||||||
Logger.Printf("Error adding many-many mapping for template %s\n", t.Name)
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func PutTemplate(t *Template, uid int64) error {
|
func PutTemplate(t *Template, uid int64) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func DeleteTemplate(id int64, uid int64) error {
|
||||||
|
err := db.Debug().Where("user_id=?", uid).Delete(Template{Id: id}).Error
|
||||||
|
if err != nil {
|
||||||
|
Logger.Println(err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
<!-- New Template Modal -->
|
<!-- New Template Modal -->
|
||||||
<div class="modal-header">
|
<div class="modal-header">
|
||||||
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
|
<button type="button" class="close" ng-click="cancel()">×</button>
|
||||||
<h4 class="modal-title" ng-hide="newTemplate" id="groupModalLabel">Edit Template: {{template.name}}</h4>
|
<h4 class="modal-title" ng-hide="newTemplate" id="groupModalLabel">Edit Template: {{template.name}}</h4>
|
||||||
<h4 class="modal-title" ng-show="newTemplate" id="groupModalLabel">New Template</h4>
|
<h4 class="modal-title" ng-show="newTemplate" id="groupModalLabel">New Template</h4>
|
||||||
</div>
|
</div>
|
||||||
|
@ -34,6 +34,6 @@
|
||||||
</fieldset>
|
</fieldset>
|
||||||
</div>
|
</div>
|
||||||
<div class="modal-footer">
|
<div class="modal-footer">
|
||||||
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
|
<button type="button" class="btn btn-default" ng-click="cancel()">Cancel</button>
|
||||||
<button type="button" class="btn btn-primary" ng-click="saveTemplate(template)" data-dismiss="modal">Save Template</button>
|
<button type="button" class="btn btn-primary" ng-click="saveTemplate(template)" data-dismiss="modal">Save Template</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Reference in New Issue