diff --git a/controller/test/casbin.go b/controller/test/casbin.go index 68589fe..14e36a8 100644 --- a/controller/test/casbin.go +++ b/controller/test/casbin.go @@ -40,3 +40,22 @@ func testCasbin(ctx *fiber.Ctx) error { 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("重载成功")) +} diff --git a/controller/test/test.go b/controller/test/test.go index 7442adc..5970b7f 100644 --- a/controller/test/test.go +++ b/controller/test/test.go @@ -5,4 +5,5 @@ import "github.com/gofiber/fiber/v2" func RegisterController(app *fiber.App) { group := app.Group("/test") group.Add(fiber.MethodGet, "/casbin", testCasbin) + group.Add(fiber.MethodPost, "/casbin", reloadCasbin) } diff --git a/docs/docs.go b/docs/docs.go index 2429eee..bb05693 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -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": { diff --git a/docs/swagger.json b/docs/swagger.json index 571df3b..f1f61da 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -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": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 31123fb..d1c112f 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -238,6 +238,29 @@ paths: summary: casbin 鉴权测试 tags: - 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: get: consumes: diff --git a/global/datasources.go b/global/datasources.go index 51b3c21..2018ad8 100644 --- a/global/datasources.go +++ b/global/datasources.go @@ -2,6 +2,7 @@ package global import ( "gofiber.study.skcks.cn/common/logger" + models2 "gofiber.study.skcks.cn/model/casbin_model/models" "gofiber.study.skcks.cn/model/user/models" "xorm.io/xorm" "xorm.io/xorm/names" @@ -10,7 +11,7 @@ import ( var DataSources *xorm.EngineGroup func SyncModelToDataSource() { - modelArr := []interface{ names.TableName }{&models.User{}} + modelArr := []interface{ names.TableName }{&models.User{}, &models2.CasbinModel{}} logger.Log.Infof("[*] 同步数据库/表结构") for _, model := range modelArr { diff --git a/model/casbin_model/generate/generate_test.go b/model/casbin_model/generate/generate_test.go index 3d341d7..a2c2d4f 100644 --- a/model/casbin_model/generate/generate_test.go +++ b/model/casbin_model/generate/generate_test.go @@ -14,7 +14,7 @@ import ( // 从结构体 同步表结构到 数据库 func TestSync(t *testing.T) { fmt.Println("同步数据库/表结构") - err := utils.SyncStructs("./config.yml", models.Model{}) + err := utils.SyncStructs("./config.yml", models.CasbinModel{}) if err != nil { t.Fatal(err) }