VILLASweb-backend-go/routes/simulator/simulator_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

43 lines
915 B
Go

package simulator
import (
"fmt"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/common"
"github.com/gin-gonic/gin"
"net/http"
"strconv"
)
func checkPermissions(c *gin.Context, modeltype common.ModelName, operation common.CRUD, hasID bool) (bool, Simulator) {
var s Simulator
err := common.ValidateRole(c, modeltype, operation)
if err != nil {
c.JSON(http.StatusUnprocessableEntity, gin.H{
"success": false,
"message": fmt.Sprintf("%v", err),
})
return false, s
}
if hasID {
// Get the ID of the simulator from the context
simulatorID, err := strconv.Atoi(c.Param("simulatorID"))
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"success": false,
"message": fmt.Sprintf("Could not get simulator's ID from context"),
})
return false, s
}
err = s.ByID(uint(simulatorID))
if common.DBError(c, err) {
return false, s
}
}
return true, s
}