mirror of
https://gitee.com/shikong-sk/gofiber-study
synced 2025-02-23 23:32:15 +08:00
添加 casbin 策略重载
This commit is contained in:
parent
c5819c1ce6
commit
e1858b1435
@ -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("重载成功"))
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
39
docs/docs.go
39
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": {
|
||||
|
@ -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": {
|
||||
|
@ -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:
|
||||
|
@ -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 {
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user