2022-10-05 16:02:09 +08:00
|
|
|
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("授权访问"))
|
|
|
|
}
|
2022-10-05 17:55:39 +08:00
|
|
|
|
|
|
|
// reloadCasbin 重新加载 casbin 策略
|
|
|
|
//
|
|
|
|
// @Summary 重新加载 casbin 策略
|
|
|
|
// @Description 重新加载 casbin 策略
|
|
|
|
// @Tags Test
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} response.Response{data=string}
|
|
|
|
// @Failure default {object} errorx.CodeErrorResponse
|
|
|
|
// @Router /test/casbin [post]
|
|
|
|
func reloadCasbin(ctx *fiber.Ctx) error {
|
|
|
|
err := global.Enforcer.LoadPolicy()
|
|
|
|
if err != nil {
|
|
|
|
return ctx.JSON(errorx.NewErrorWithCode(fiber.StatusForbidden, err.Error()))
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx.JSON(response.NewResponse("重载成功"))
|
|
|
|
}
|