Enshi/enshi_back/middleware/postVotesMiddleware.go
2024-12-05 23:11:44 +03:00

39 lines
724 B
Go

package middleware
import (
postvotespolicies "enshi/ABAC/PostVotesPolicies"
"enshi/ABAC/rules"
"strings"
"github.com/gin-gonic/gin"
)
func PostVotesMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
a := strings.Split(c.Request.URL.Path, "/")[1]
switch c.Request.Method {
case "DELETE":
c.Set("target", postvotespolicies.DELETE_VOTE)
case "POST":
c.Set("target", postvotespolicies.CREATE_VOTE)
case "GET":
if a != "post-votes" {
c.Set("target", postvotespolicies.READ_VOTE)
} else {
c.Set("target", "")
}
}
isAllowed, errors := postvotespolicies.PostVotePolicies(c)
if rules.ShouldAbortRequest(c, isAllowed, errors) {
c.Abort()
return
}
c.Next()
}
}