Skip to content

Commit

Permalink
refactor(api) new function for entity check in CreateEntity()
Browse files Browse the repository at this point in the history
  • Loading branch information
Cedrok committed Aug 5, 2024
1 parent 7749d6a commit 31ea224
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions API/controllers/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,24 +145,8 @@ func CreateEntity(w http.ResponseWriter, r *http.Request) {
return
}

if entStr == u.HIERARCHYOBJS_ENT {
// Get entity from object's category
entStr = object["category"].(string)
entInt = u.EntityStrToInt(entStr)
if entInt < u.SITE || entInt > u.GROUP {
w.WriteHeader(http.StatusBadRequest)
u.Respond(w, u.Message("Invalid category for a hierarchy object"))
u.ErrLog("Cannot create invalid hierarchy object", "CREATE "+mux.Vars(r)["entity"], "", r)
return
}
} else if u.IsEntityHierarchical(entInt) && entInt != u.STRAYOBJ {
// Check if category and endpoint match, except for non hierarchal entities and strays
if object["category"] != entStr {
w.WriteHeader(http.StatusBadRequest)
u.Respond(w, u.Message("Category in request body does not correspond with desired object in endpoint"))
u.ErrLog("Cannot create invalid object", "CREATE "+mux.Vars(r)["entity"], "", r)
return
}
if ok := checkEntity(w, r, entStr, entInt, object); !ok {
return
}

// Clean the data of 'id' attribute if present
Expand All @@ -182,6 +166,27 @@ func CreateEntity(w http.ResponseWriter, r *http.Request) {
}
}

func checkEntity(w http.ResponseWriter, r *http.Request, entStr string, entInt int, object map[string]interface{}) bool {
if entStr == u.HIERARCHYOBJS_ENT {
// Get entity from object's category
entStr = object["category"].(string)
entInt = u.EntityStrToInt(entStr)
if entInt < u.SITE || entInt > u.GROUP {
w.WriteHeader(http.StatusBadRequest)
u.Respond(w, u.Message("Invalid category for a hierarchy object"))
u.ErrLog("Cannot create invalid hierarchy object", "CREATE "+mux.Vars(r)["entity"], "", r)
return false
}
// Check if category and endpoint match, except for non hierarchal entities and strays
} else if u.IsEntityHierarchical(entInt) && entInt != u.STRAYOBJ && object["category"] != entStr {
w.WriteHeader(http.StatusBadRequest)
u.Respond(w, u.Message("Category in request body does not correspond with desired object in endpoint"))
u.ErrLog("Cannot create invalid object", "CREATE "+mux.Vars(r)["entity"], "", r)
return false
}
return true
}

// swagger:operation POST /api/domains/bulk Organization CreateBulkDomain
// Create multiple domains in a single request.
// An array of domains should be provided in the body.
Expand Down

0 comments on commit 31ea224

Please sign in to comment.