gofiber-study/controller/test/casbin.go

43 lines
1.2 KiB
Go
Raw Normal View History

package test
import (
"github.com/gofiber/fiber/v2"
"gofiber.study.skcks.cn/common/errorx"
"gofiber.study.skcks.cn/common/response"
"gofiber.study.skcks.cn/global"
)
// testCasbin casbin 鉴权测试
//
// @Summary casbin 鉴权测试
// @Description casbin 鉴权测试
// @Tags Test
// @Accept json
// @Produce json
// @Param identity query string true "身份"
// @Param system query string true "系统"
// @Param api query string true "api"
// @Param act query string true "动作"
// @Success 200 {object} response.Response{data=string}
// @Failure default {object} errorx.CodeErrorResponse
// @Router /test/casbin [get]
func testCasbin(ctx *fiber.Ctx) error {
var identity, system, api, act string
identity = ctx.Query("identity")
system = ctx.Query("system")
api = ctx.Query("api")
act = ctx.Query("act")
enforce, err := global.Enforcer.Enforce(identity, system, api, act)
if err != nil {
return ctx.JSON(errorx.NewDefaultError(err.Error()))
}
if !enforce {
return ctx.JSON(errorx.NewErrorWithCode(fiber.StatusForbidden, "无权访问"))
}
return ctx.JSON(response.NewResponse("授权访问"))
}