mirror of
https://github.com/go-admin-team/go-admin.git
synced 2026-10-08 15:22:53 +00:00
Every import of the module changes, not only the seven packages that
moved out of sdk/pkg: Go requires the major version in the path from v2
on. Both happen in one pass —
go run github.com/go-admin-team/go-admin-core/tools/coreupgrade@v2.0.0 -w -v2 .
go mod tidy
— which is the command the release notes give, run here as a consumer
would run it. 210 imports across 95 files.
The compatibility shims this used are gone in v2, so the paths that
moved had to move: sdk/pkg/captcha, sdk/pkg/jwtauth and its user
package, sdk/pkg/response and sdk/pkg/casbin.
The count of unformatted files is unchanged at 34, none of them touched
by this: the tool reformats a file only if it was already gofmt clean,
so a migration cannot disappear into whitespace.
62 lines
1.7 KiB
Go
62 lines
1.7 KiB
Go
package middleware
|
|
|
|
import (
|
|
"github.com/casbin/casbin/v3/util"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/go-admin-team/go-admin-core/v2/jwtauth"
|
|
"github.com/go-admin-team/go-admin-core/v2/response"
|
|
"github.com/go-admin-team/go-admin-core/v2/sdk"
|
|
"github.com/go-admin-team/go-admin-core/v2/sdk/api"
|
|
)
|
|
|
|
// AuthCheckRole 权限检查中间件
|
|
func AuthCheckRole() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
log := api.GetRequestLogger(c)
|
|
data, _ := c.Get(jwtauth.JwtPayloadKey)
|
|
v := data.(jwtauth.MapClaims)
|
|
e := sdk.Runtime.GetCasbinByTenant(c.Request.Host)
|
|
var res, casbinExclude bool
|
|
var err error
|
|
//检查权限
|
|
if v["rolekey"] == "admin" {
|
|
res = true
|
|
c.Next()
|
|
return
|
|
}
|
|
for _, i := range CasbinExclude {
|
|
if util.KeyMatch2(c.Request.URL.Path, i.Url) && c.Request.Method == i.Method {
|
|
casbinExclude = true
|
|
break
|
|
}
|
|
}
|
|
if casbinExclude {
|
|
log.Infof("Casbin exclusion, no validation method:%s path:%s", c.Request.Method, c.Request.URL.Path)
|
|
c.Next()
|
|
return
|
|
}
|
|
res, err = e.Enforce(v["rolekey"], c.Request.URL.Path, c.Request.Method)
|
|
if err != nil {
|
|
log.Errorf("AuthCheckRole error:%s method:%s path:%s", err, c.Request.Method, c.Request.URL.Path)
|
|
response.Error(c, 500, err, "")
|
|
return
|
|
}
|
|
|
|
if res {
|
|
log.Infof("isTrue: %v role: %s method: %s path: %s", res, v["rolekey"], c.Request.Method, c.Request.URL.Path)
|
|
c.Next()
|
|
} else {
|
|
log.Warnf("isTrue: %v role: %s method: %s path: %s message: %s", res, v["rolekey"], c.Request.Method, c.Request.URL.Path, "当前request无权限,请管理员确认!")
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 403,
|
|
"msg": "对不起,您没有该接口访问权限,请联系管理员",
|
|
})
|
|
c.Abort()
|
|
return
|
|
}
|
|
|
|
}
|
|
}
|