VILLASweb-backend-go/routes/simulationmodel/simulationmodel_middleware.go
Sonja Happ ac9e564bc8 - deleting old obsolete test functions
- move all data and functions solely used for testing to the file test_utilities
- get rid of utilities file
2019-09-09 12:11:55 +02:00

62 lines
1.5 KiB
Go

package simulationmodel
import (
"fmt"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/common"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/scenario"
)
func CheckPermissions(c *gin.Context, operation common.CRUD, modelIDSource string, modelIDBody int) (bool, SimulationModel) {
var m SimulationModel
err := common.ValidateRole(c, common.ModelSimulationModel, operation)
if err != nil {
c.JSON(http.StatusUnprocessableEntity, gin.H{
"success": false,
"message": fmt.Sprintf("Access denied (role validation failed): %v", err),
})
return false, m
}
var modelID int
if modelIDSource == "path" {
modelID, err = strconv.Atoi(c.Param("modelID"))
if err != nil {
errormsg := fmt.Sprintf("Bad request. No or incorrect format of modelID path parameter")
c.JSON(http.StatusBadRequest, gin.H{
"success": false,
"message": errormsg,
})
return false, m
}
} else if modelIDSource == "query" {
modelID, err = strconv.Atoi(c.Request.URL.Query().Get("modelID"))
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"success": false,
"message": fmt.Sprintf("Bad request. No or incorrect format of modelID query parameter"),
})
return false, m
}
} else if modelIDSource == "body" {
modelID = modelIDBody
}
err = m.ByID(uint(modelID))
if common.DBError(c, err) {
return false, m
}
ok, _ := scenario.CheckPermissions(c, operation, "body", int(m.ScenarioID))
if !ok {
return false, m
}
return true, m
}