VILLASweb-backend-go/routes/simulator/simulator_middleware.go
Sonja Happ 2ffda7cad8 - revise naming of some common functions
- improve returning of error codes by using common functions
- use a separate file for authentication endpoint to improve clarity of code
2019-09-09 15:30:17 +02:00

36 lines
787 B
Go

package simulator
import (
"fmt"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/common"
"github.com/gin-gonic/gin"
"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 {
common.UnprocessableEntityError(c, err.Error())
return false, s
}
if hasID {
// Get the ID of the simulator from the context
simulatorID, err := strconv.Atoi(c.Param("simulatorID"))
if err != nil {
common.BadRequestError(c, 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
}