github.com/lunarobliq/gophish@v0.8.1-0.20230523153303-93511002234d/controllers/api/template.go (about)

     1  package api
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  	"strconv"
     7  	"time"
     8  
     9  	ctx "github.com/gophish/gophish/context"
    10  	log "github.com/gophish/gophish/logger"
    11  	"github.com/gophish/gophish/models"
    12  	"github.com/gorilla/mux"
    13  	"github.com/jinzhu/gorm"
    14  )
    15  
    16  // Templates handles the functionality for the /api/templates endpoint
    17  func (as *Server) Templates(w http.ResponseWriter, r *http.Request) {
    18  	switch {
    19  	case r.Method == "GET":
    20  		ts, err := models.GetTemplates(ctx.Get(r, "user_id").(int64))
    21  		if err != nil {
    22  			log.Error(err)
    23  		}
    24  		JSONResponse(w, ts, http.StatusOK)
    25  	//POST: Create a new template and return it as JSON
    26  	case r.Method == "POST":
    27  		t := models.Template{}
    28  		// Put the request into a template
    29  		err := json.NewDecoder(r.Body).Decode(&t)
    30  		if err != nil {
    31  			JSONResponse(w, models.Response{Success: false, Message: "Invalid JSON structure"}, http.StatusBadRequest)
    32  			return
    33  		}
    34  		_, err = models.GetTemplateByName(t.Name, ctx.Get(r, "user_id").(int64))
    35  		if err != gorm.ErrRecordNotFound {
    36  			JSONResponse(w, models.Response{Success: false, Message: "Template name already in use"}, http.StatusConflict)
    37  			return
    38  		}
    39  		t.ModifiedDate = time.Now().UTC()
    40  		t.UserId = ctx.Get(r, "user_id").(int64)
    41  		err = models.PostTemplate(&t)
    42  		if err == models.ErrTemplateNameNotSpecified {
    43  			JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusBadRequest)
    44  			return
    45  		}
    46  		if err == models.ErrTemplateMissingParameter {
    47  			JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusBadRequest)
    48  			return
    49  		}
    50  		if err != nil {
    51  			JSONResponse(w, models.Response{Success: false, Message: "Error inserting template into database"}, http.StatusInternalServerError)
    52  			log.Error(err)
    53  			return
    54  		}
    55  		JSONResponse(w, t, http.StatusCreated)
    56  	}
    57  }
    58  
    59  // Template handles the functions for the /api/templates/:id endpoint
    60  func (as *Server) Template(w http.ResponseWriter, r *http.Request) {
    61  	vars := mux.Vars(r)
    62  	id, _ := strconv.ParseInt(vars["id"], 0, 64)
    63  	t, err := models.GetTemplate(id, ctx.Get(r, "user_id").(int64))
    64  	if err != nil {
    65  		JSONResponse(w, models.Response{Success: false, Message: "Template not found"}, http.StatusNotFound)
    66  		return
    67  	}
    68  	switch {
    69  	case r.Method == "GET":
    70  		JSONResponse(w, t, http.StatusOK)
    71  	case r.Method == "DELETE":
    72  		err = models.DeleteTemplate(id, ctx.Get(r, "user_id").(int64))
    73  		if err != nil {
    74  			JSONResponse(w, models.Response{Success: false, Message: "Error deleting template"}, http.StatusInternalServerError)
    75  			return
    76  		}
    77  		JSONResponse(w, models.Response{Success: true, Message: "Template deleted successfully!"}, http.StatusOK)
    78  	case r.Method == "PUT":
    79  		t = models.Template{}
    80  		err = json.NewDecoder(r.Body).Decode(&t)
    81  		if err != nil {
    82  			log.Error(err)
    83  		}
    84  		if t.Id != id {
    85  			JSONResponse(w, models.Response{Success: false, Message: "Error: /:id and template_id mismatch"}, http.StatusBadRequest)
    86  			return
    87  		}
    88  		t.ModifiedDate = time.Now().UTC()
    89  		t.UserId = ctx.Get(r, "user_id").(int64)
    90  		err = models.PutTemplate(&t)
    91  		if err != nil {
    92  			JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusBadRequest)
    93  			return
    94  		}
    95  		JSONResponse(w, t, http.StatusOK)
    96  	}
    97  }