create a function in helper package to extract the ID of an element from a path or query and use this function in middleware

This commit is contained in:
Sonja Happ 2019-09-10 17:17:53 +02:00
parent 55f81f1309
commit d160103fcf
8 changed files with 45 additions and 72 deletions

31
helper/utilities.go Normal file
View file

@ -0,0 +1,31 @@
package helper
import (
"fmt"
"github.com/gin-gonic/gin"
"strconv"
)
func GetIDOfElement(c *gin.Context, elementName string, source string, providedID int) (int, error) {
if source == "path" {
id, err := strconv.Atoi(c.Param(elementName))
if err != nil {
BadRequestError(c, fmt.Sprintf("No or incorrect format of path parameter"))
return -1, err
}
return id, nil
} else if source == "query" {
id, err := strconv.Atoi(c.Request.URL.Query().Get(elementName))
if err != nil {
BadRequestError(c, fmt.Sprintf("No or incorrect format of query parameter"))
return -1, err
}
return id, nil
} else if source == "body" {
id := providedID
return id, nil
} else {
return -1, fmt.Errorf("invalid source of element ID")
}
}

View file

@ -4,8 +4,6 @@ import (
"fmt" "fmt"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/helper" "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/helper"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/scenario" "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/scenario"
"strconv"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/database" "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/database"
@ -21,21 +19,9 @@ func CheckPermissions(c *gin.Context, operation database.CRUD, dabIDSource strin
return false, dab return false, dab
} }
var dabID int dabID, err := helper.GetIDOfElement(c, "dashboardID", dabIDSource, dabIDBody)
if dabIDSource == "path" { if err != nil {
dabID, err = strconv.Atoi(c.Param("dashboardID")) return false, dab
if err != nil {
helper.BadRequestError(c, fmt.Sprintf("No or incorrect format of dashboardID path parameter"))
return false, dab
}
} else if dabIDSource == "query" {
dabID, err = strconv.Atoi(c.Request.URL.Query().Get("dashboardID"))
if err != nil {
helper.BadRequestError(c, fmt.Sprintf("No or incorrect format of dashboardID query parameter"))
return false, dab
}
} else if dabIDSource == "body" {
dabID = dabIDBody
} }
err = dab.ByID(uint(dabID)) err = dab.ByID(uint(dabID))

View file

@ -7,7 +7,6 @@ import (
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/simulationmodel" "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/simulationmodel"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/widget" "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/widget"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"strconv"
) )
func checkPermissions(c *gin.Context, operation database.CRUD) (bool, File) { func checkPermissions(c *gin.Context, operation database.CRUD) (bool, File) {
@ -20,9 +19,8 @@ func checkPermissions(c *gin.Context, operation database.CRUD) (bool, File) {
return false, f return false, f
} }
fileID, err := strconv.Atoi(c.Param("fileID")) fileID, err := helper.GetIDOfElement(c, "fileID", "path", -1)
if err != nil { if err != nil {
helper.BadRequestError(c, fmt.Sprintf("No or incorrect format of fileID path parameter"))
return false, f return false, f
} }

View file

@ -3,8 +3,6 @@ package scenario
import ( import (
"fmt" "fmt"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/helper" "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/helper"
"strconv"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/database" "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/database"
@ -24,24 +22,8 @@ func CheckPermissions(c *gin.Context, operation database.CRUD, screnarioIDSource
return true, so return true, so
} }
var scenarioID int scenarioID, err := helper.GetIDOfElement(c, "scenarioID", screnarioIDSource, scenarioIDbody)
if screnarioIDSource == "path" { if err != nil {
scenarioID, err = strconv.Atoi(c.Param("scenarioID"))
if err != nil {
helper.BadRequestError(c, fmt.Sprintf("No or incorrect format of scenarioID path parameter"))
return false, so
}
} else if screnarioIDSource == "query" {
scenarioID, err = strconv.Atoi(c.Request.URL.Query().Get("scenarioID"))
if err != nil {
helper.BadRequestError(c, fmt.Sprintf("No or incorrect format of scenarioID query parameter"))
return false, so
}
} else if screnarioIDSource == "body" {
scenarioID = scenarioIDbody
} else {
helper.BadRequestError(c, fmt.Sprintf("The following source of scenario ID is not valid: %s", screnarioIDSource))
return false, so return false, so
} }

View file

@ -3,8 +3,6 @@ package signal
import ( import (
"fmt" "fmt"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/helper" "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/helper"
"strconv"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/database" "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/database"
@ -21,9 +19,8 @@ func checkPermissions(c *gin.Context, operation database.CRUD) (bool, Signal) {
return false, sig return false, sig
} }
signalID, err := strconv.Atoi(c.Param("signalID")) signalID, err := helper.GetIDOfElement(c, "signalID", "path", -1)
if err != nil { if err != nil {
helper.BadRequestError(c, fmt.Sprintf("No or incorrect format of signalID path parameter"))
return false, sig return false, sig
} }

View file

@ -3,8 +3,6 @@ package simulationmodel
import ( import (
"fmt" "fmt"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/helper" "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/helper"
"strconv"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/database" "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/database"
@ -21,21 +19,9 @@ func CheckPermissions(c *gin.Context, operation database.CRUD, modelIDSource str
return false, m return false, m
} }
var modelID int modelID, err := helper.GetIDOfElement(c, "modelID", modelIDSource, modelIDBody)
if modelIDSource == "path" { if err != nil {
modelID, err = strconv.Atoi(c.Param("modelID")) return false, m
if err != nil {
helper.BadRequestError(c, fmt.Sprintf("No or incorrect format of modelID path parameter"))
return false, m
}
} else if modelIDSource == "query" {
modelID, err = strconv.Atoi(c.Request.URL.Query().Get("modelID"))
if err != nil {
helper.BadRequestError(c, fmt.Sprintf("No or incorrect format of modelID query parameter"))
return false, m
}
} else if modelIDSource == "body" {
modelID = modelIDBody
} }
err = m.ByID(uint(modelID)) err = m.ByID(uint(modelID))

View file

@ -1,11 +1,9 @@
package simulator package simulator
import ( import (
"fmt"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/database" "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/database"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/helper" "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/helper"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"strconv"
) )
func checkPermissions(c *gin.Context, modeltype database.ModelName, operation database.CRUD, hasID bool) (bool, Simulator) { func checkPermissions(c *gin.Context, modeltype database.ModelName, operation database.CRUD, hasID bool) (bool, Simulator) {
@ -20,9 +18,8 @@ func checkPermissions(c *gin.Context, modeltype database.ModelName, operation da
if hasID { if hasID {
// Get the ID of the simulator from the context // Get the ID of the simulator from the context
simulatorID, err := strconv.Atoi(c.Param("simulatorID")) simulatorID, err := helper.GetIDOfElement(c, "simulatorID", "path", -1)
if err != nil { if err != nil {
helper.BadRequestError(c, fmt.Sprintf("Could not get simulator's ID from context"))
return false, s return false, s
} }
@ -30,7 +27,6 @@ func checkPermissions(c *gin.Context, modeltype database.ModelName, operation da
if helper.DBError(c, err) { if helper.DBError(c, err) {
return false, s return false, s
} }
} }
return true, s return true, s

View file

@ -3,8 +3,6 @@ package widget
import ( import (
"fmt" "fmt"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/helper" "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/helper"
"strconv"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/database" "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/database"
@ -14,8 +12,8 @@ import (
func CheckPermissions(c *gin.Context, operation database.CRUD, widgetIDBody int) (bool, Widget) { func CheckPermissions(c *gin.Context, operation database.CRUD, widgetIDBody int) (bool, Widget) {
var w Widget var w Widget
var err error
err := database.ValidateRole(c, database.ModelWidget, operation) err = database.ValidateRole(c, database.ModelWidget, operation)
if err != nil { if err != nil {
helper.UnprocessableEntityError(c, fmt.Sprintf("Access denied (role validation failed): %v", err.Error())) helper.UnprocessableEntityError(c, fmt.Sprintf("Access denied (role validation failed): %v", err.Error()))
return false, w return false, w
@ -23,9 +21,8 @@ func CheckPermissions(c *gin.Context, operation database.CRUD, widgetIDBody int)
var widgetID int var widgetID int
if widgetIDBody < 0 { if widgetIDBody < 0 {
widgetID, err = strconv.Atoi(c.Param("widgetID")) widgetID, err = helper.GetIDOfElement(c, "widgetID", "path", -1)
if err != nil { if err != nil {
helper.BadRequestError(c, fmt.Sprintf("No or incorrect format of widgetID path parameter"))
return false, w return false, w
} }
} else { } else {