github.com/8samaka/gophish@v0.9.0/controllers/api/group.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  // Groups returns a list of groups if requested via GET.
    17  // If requested via POST, APIGroups creates a new group and returns a reference to it.
    18  func (as *Server) Groups(w http.ResponseWriter, r *http.Request) {
    19  	switch {
    20  	case r.Method == "GET":
    21  		gs, err := models.GetGroups(ctx.Get(r, "user_id").(int64))
    22  		if err != nil {
    23  			JSONResponse(w, models.Response{Success: false, Message: "No groups found"}, http.StatusNotFound)
    24  			return
    25  		}
    26  		JSONResponse(w, gs, http.StatusOK)
    27  	//POST: Create a new group and return it as JSON
    28  	case r.Method == "POST":
    29  		g := models.Group{}
    30  		// Put the request into a group
    31  		err := json.NewDecoder(r.Body).Decode(&g)
    32  		if err != nil {
    33  			JSONResponse(w, models.Response{Success: false, Message: "Invalid JSON structure"}, http.StatusBadRequest)
    34  			return
    35  		}
    36  		_, err = models.GetGroupByName(g.Name, ctx.Get(r, "user_id").(int64))
    37  		if err != gorm.ErrRecordNotFound {
    38  			JSONResponse(w, models.Response{Success: false, Message: "Group name already in use"}, http.StatusConflict)
    39  			return
    40  		}
    41  		g.ModifiedDate = time.Now().UTC()
    42  		g.UserId = ctx.Get(r, "user_id").(int64)
    43  		err = models.PostGroup(&g)
    44  		if err != nil {
    45  			JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusBadRequest)
    46  			return
    47  		}
    48  		JSONResponse(w, g, http.StatusCreated)
    49  	}
    50  }
    51  
    52  // GroupsSummary returns a summary of the groups owned by the current user.
    53  func (as *Server) GroupsSummary(w http.ResponseWriter, r *http.Request) {
    54  	switch {
    55  	case r.Method == "GET":
    56  		gs, err := models.GetGroupSummaries(ctx.Get(r, "user_id").(int64))
    57  		if err != nil {
    58  			log.Error(err)
    59  			JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusInternalServerError)
    60  			return
    61  		}
    62  		JSONResponse(w, gs, http.StatusOK)
    63  	}
    64  }
    65  
    66  // Group returns details about the requested group.
    67  // If the group is not valid, Group returns null.
    68  func (as *Server) Group(w http.ResponseWriter, r *http.Request) {
    69  	vars := mux.Vars(r)
    70  	id, _ := strconv.ParseInt(vars["id"], 0, 64)
    71  	g, err := models.GetGroup(id, ctx.Get(r, "user_id").(int64))
    72  	if err != nil {
    73  		JSONResponse(w, models.Response{Success: false, Message: "Group not found"}, http.StatusNotFound)
    74  		return
    75  	}
    76  	switch {
    77  	case r.Method == "GET":
    78  		JSONResponse(w, g, http.StatusOK)
    79  	case r.Method == "DELETE":
    80  		err = models.DeleteGroup(&g)
    81  		if err != nil {
    82  			JSONResponse(w, models.Response{Success: false, Message: "Error deleting group"}, http.StatusInternalServerError)
    83  			return
    84  		}
    85  		JSONResponse(w, models.Response{Success: true, Message: "Group deleted successfully!"}, http.StatusOK)
    86  	case r.Method == "PUT":
    87  		// Change this to get from URL and uid (don't bother with id in r.Body)
    88  		g = models.Group{}
    89  		err = json.NewDecoder(r.Body).Decode(&g)
    90  		if g.Id != id {
    91  			JSONResponse(w, models.Response{Success: false, Message: "Error: /:id and group_id mismatch"}, http.StatusInternalServerError)
    92  			return
    93  		}
    94  		g.ModifiedDate = time.Now().UTC()
    95  		g.UserId = ctx.Get(r, "user_id").(int64)
    96  		err = models.PutGroup(&g)
    97  		if err != nil {
    98  			JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusBadRequest)
    99  			return
   100  		}
   101  		JSONResponse(w, g, http.StatusOK)
   102  	}
   103  }
   104  
   105  // GroupSummary returns a summary of the groups owned by the current user.
   106  func (as *Server) GroupSummary(w http.ResponseWriter, r *http.Request) {
   107  	switch {
   108  	case r.Method == "GET":
   109  		vars := mux.Vars(r)
   110  		id, _ := strconv.ParseInt(vars["id"], 0, 64)
   111  		g, err := models.GetGroupSummary(id, ctx.Get(r, "user_id").(int64))
   112  		if err != nil {
   113  			JSONResponse(w, models.Response{Success: false, Message: "Group not found"}, http.StatusNotFound)
   114  			return
   115  		}
   116  		JSONResponse(w, g, http.StatusOK)
   117  	}
   118  }