VILLASweb-backend-go/routes/widget/widget_middleware.go
Sonja Happ 387c922059 Revision of (file) testing
- create a guest user and modify function that adds users to the DB for testing
- improve code coverage of file endpoint tests and remove some obsolete code from file package
- add more error info if role validation fails in all endpoints
- change response of GET files to only return the file data and nothing else
- fix some bugs in file endpoints that became visible by the tests
2019-09-11 12:30:01 +02:00

43 lines
1 KiB
Go

package widget
import (
"fmt"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/helper"
"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/routes/dashboard"
)
func CheckPermissions(c *gin.Context, operation database.CRUD, widgetIDBody int) (bool, Widget) {
var w Widget
var err error
err = database.ValidateRole(c, database.ModelWidget, operation)
if err != nil {
helper.UnprocessableEntityError(c, fmt.Sprintf("Access denied (role validation of widget failed): %v", err.Error()))
return false, w
}
var widgetID int
if widgetIDBody < 0 {
widgetID, err = helper.GetIDOfElement(c, "widgetID", "path", -1)
if err != nil {
return false, w
}
} else {
widgetID = widgetIDBody
}
err = w.ByID(uint(widgetID))
if helper.DBError(c, err) {
return false, w
}
ok, _ := dashboard.CheckPermissions(c, operation, "body", int(w.DashboardID))
if !ok {
return false, w
}
return true, w
}