添加 casbin 策略重载

This commit is contained in:
Shikong 2022-10-05 17:55:39 +08:00
parent c5819c1ce6
commit e1858b1435
7 changed files with 124 additions and 2 deletions

View File

@ -40,3 +40,22 @@ func testCasbin(ctx *fiber.Ctx) error {
return ctx.JSON(response.NewResponse("授权访问")) return ctx.JSON(response.NewResponse("授权访问"))
} }
// 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("重载成功"))
}

View File

@ -5,4 +5,5 @@ import "github.com/gofiber/fiber/v2"
func RegisterController(app *fiber.App) { func RegisterController(app *fiber.App) {
group := app.Group("/test") group := app.Group("/test")
group.Add(fiber.MethodGet, "/casbin", testCasbin) group.Add(fiber.MethodGet, "/casbin", testCasbin)
group.Add(fiber.MethodPost, "/casbin", reloadCasbin)
} }

View File

@ -282,6 +282,45 @@ const docTemplate = `{
} }
} }
} }
},
"post": {
"description": "重新加载 casbin 策略",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Test"
],
"summary": "重新加载 casbin 策略",
"responses": {
"200": {
"description": "OK",
"schema": {
"allOf": [
{
"$ref": "#/definitions/response.Response"
},
{
"type": "object",
"properties": {
"data": {
"type": "string"
}
}
}
]
}
},
"default": {
"description": "",
"schema": {
"$ref": "#/definitions/errorx.CodeErrorResponse"
}
}
}
} }
}, },
"/user/account": { "/user/account": {

View File

@ -274,6 +274,45 @@
} }
} }
} }
},
"post": {
"description": "重新加载 casbin 策略",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Test"
],
"summary": "重新加载 casbin 策略",
"responses": {
"200": {
"description": "OK",
"schema": {
"allOf": [
{
"$ref": "#/definitions/response.Response"
},
{
"type": "object",
"properties": {
"data": {
"type": "string"
}
}
}
]
}
},
"default": {
"description": "",
"schema": {
"$ref": "#/definitions/errorx.CodeErrorResponse"
}
}
}
} }
}, },
"/user/account": { "/user/account": {

View File

@ -238,6 +238,29 @@ paths:
summary: casbin 鉴权测试 summary: casbin 鉴权测试
tags: tags:
- Test - Test
post:
consumes:
- application/json
description: 重新加载 casbin 策略
produces:
- application/json
responses:
"200":
description: OK
schema:
allOf:
- $ref: '#/definitions/response.Response'
- properties:
data:
type: string
type: object
default:
description: ""
schema:
$ref: '#/definitions/errorx.CodeErrorResponse'
summary: 重新加载 casbin 策略
tags:
- Test
/user/account: /user/account:
get: get:
consumes: consumes:

View File

@ -2,6 +2,7 @@ package global
import ( import (
"gofiber.study.skcks.cn/common/logger" "gofiber.study.skcks.cn/common/logger"
models2 "gofiber.study.skcks.cn/model/casbin_model/models"
"gofiber.study.skcks.cn/model/user/models" "gofiber.study.skcks.cn/model/user/models"
"xorm.io/xorm" "xorm.io/xorm"
"xorm.io/xorm/names" "xorm.io/xorm/names"
@ -10,7 +11,7 @@ import (
var DataSources *xorm.EngineGroup var DataSources *xorm.EngineGroup
func SyncModelToDataSource() { func SyncModelToDataSource() {
modelArr := []interface{ names.TableName }{&models.User{}} modelArr := []interface{ names.TableName }{&models.User{}, &models2.CasbinModel{}}
logger.Log.Infof("[*] 同步数据库/表结构") logger.Log.Infof("[*] 同步数据库/表结构")
for _, model := range modelArr { for _, model := range modelArr {

View File

@ -14,7 +14,7 @@ import (
// 从结构体 同步表结构到 数据库 // 从结构体 同步表结构到 数据库
func TestSync(t *testing.T) { func TestSync(t *testing.T) {
fmt.Println("同步数据库/表结构") fmt.Println("同步数据库/表结构")
err := utils.SyncStructs("./config.yml", models.Model{}) err := utils.SyncStructs("./config.yml", models.CasbinModel{})
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }