71 lines
1.6 KiB
Go
71 lines
1.6 KiB
Go
package system
|
|
|
|
import (
|
|
"kra/pkg/response"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type CasbinApi struct{}
|
|
|
|
// CasbinRule Casbin规则
|
|
type CasbinRule struct {
|
|
Path string `json:"path"`
|
|
Method string `json:"method"`
|
|
}
|
|
|
|
// UpdateCasbinRequest 更新Casbin请求
|
|
type UpdateCasbinRequest struct {
|
|
AuthorityId uint `json:"authorityId" binding:"required"`
|
|
CasbinInfos []CasbinRule `json:"casbinInfos"`
|
|
}
|
|
|
|
// GetPolicyPathByAuthorityIdRequest 获取权限路径请求
|
|
type GetPolicyPathByAuthorityIdRequest struct {
|
|
AuthorityId uint `json:"authorityId" binding:"required"`
|
|
}
|
|
|
|
// UpdateCasbin 更新Casbin权限
|
|
func (ca *CasbinApi) UpdateCasbin(c *gin.Context) {
|
|
var req UpdateCasbinRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
// 转换为业务层格式
|
|
rules := make([]struct {
|
|
Path string
|
|
Method string
|
|
}, len(req.CasbinInfos))
|
|
for i, r := range req.CasbinInfos {
|
|
rules[i] = struct {
|
|
Path string
|
|
Method string
|
|
}{
|
|
Path: r.Path,
|
|
Method: r.Method,
|
|
}
|
|
}
|
|
|
|
if err := casbinUsecase.UpdateCasbin(0, req.AuthorityId, rules); err != nil {
|
|
response.FailWithMessage("更新失败: "+err.Error(), c)
|
|
return
|
|
}
|
|
|
|
response.OkWithMessage("更新成功", c)
|
|
}
|
|
|
|
// GetPolicyPathByAuthorityId 获取权限路径
|
|
func (ca *CasbinApi) GetPolicyPathByAuthorityId(c *gin.Context) {
|
|
var req GetPolicyPathByAuthorityIdRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
paths := casbinUsecase.GetPolicyPathByAuthorityId(req.AuthorityId)
|
|
|
|
response.OkWithDetailed(gin.H{"paths": paths}, "获取成功", c)
|
|
}
|