🐛 fix the casbin invalidation bug/adjust routing strategy
This commit is contained in:
@@ -1,27 +1,40 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
ginI18n "github.com/gin-contrib/i18n"
|
||||
"github.com/gin-gonic/gin"
|
||||
"schisandra-cloud-album/common/result"
|
||||
"schisandra-cloud-album/global"
|
||||
)
|
||||
|
||||
func CasbinMiddleware() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
userId, ok := c.Get("userId")
|
||||
if !ok {
|
||||
userIdAny, exists := c.Get("userId")
|
||||
if !exists {
|
||||
global.LOG.Error("casbin middleware: userId not found")
|
||||
result.FailWithMessage(ginI18n.MustGetMessage(c, "PermissionDenied"), c)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
userId, ok := userIdAny.(*string)
|
||||
if !ok {
|
||||
result.FailWithMessage(ginI18n.MustGetMessage(c, "PermissionDenied"), c)
|
||||
global.LOG.Error("casbin middleware: userId is not string")
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
userIdStr := *userId
|
||||
method := c.Request.Method
|
||||
path := c.Request.URL.Path
|
||||
ok, err := global.Casbin.Enforce(userId.(string), path, method)
|
||||
correct, err := global.Casbin.Enforce(userIdStr, path, method)
|
||||
if err != nil {
|
||||
result.FailWithMessage(ginI18n.MustGetMessage(c, "PermissionDenied"), c)
|
||||
global.LOG.Error("casbin middleware: ", err)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
if !ok {
|
||||
if !correct {
|
||||
result.FailWithMessage(ginI18n.MustGetMessage(c, "PermissionDenied"), c)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
Reference in New Issue
Block a user