98 lines
2.5 KiB
Go
98 lines
2.5 KiB
Go
package server
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"kra/internal/biz"
|
|
"kra/internal/service"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func registerPolicyRoutes(group *gin.RouterGroup, svc *service.AccessService) {
|
|
casbin := group.Group("/casbin")
|
|
casbin.POST("/updateCasbin", func(c *gin.Context) {
|
|
var req struct {
|
|
AuthorityID uint `json:"authorityId"`
|
|
Infos []struct {
|
|
Path string `json:"path"`
|
|
Method string `json:"method"`
|
|
} `json:"casbinInfos"`
|
|
}
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
paths := make([]*biz.API, 0, len(req.Infos))
|
|
for _, v := range req.Infos {
|
|
paths = append(paths, &biz.API{Path: v.Path, Method: v.Method})
|
|
}
|
|
if err := svc.SetPolicyPaths(c.Request.Context(), req.AuthorityID, paths); err != nil {
|
|
fail(c, "更新失败")
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
casbin.POST("/getPolicyPathByAuthorityId", func(c *gin.Context) {
|
|
var req struct {
|
|
AuthorityID uint `json:"authorityId"`
|
|
}
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
paths, err := svc.PolicyPaths(c.Request.Context(), req.AuthorityID)
|
|
if err != nil {
|
|
fail(c, "获取失败")
|
|
return
|
|
}
|
|
writeResult(c, codeSuccess, gin.H{"paths": paths}, "获取成功")
|
|
})
|
|
buttons := group.Group("/authorityBtn")
|
|
buttons.POST("/getAuthorityBtn", func(c *gin.Context) {
|
|
var req struct {
|
|
MenuID uint `json:"menuID"`
|
|
AuthorityID uint `json:"authorityId"`
|
|
}
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
selected, err := svc.SelectedButtons(c.Request.Context(), req.AuthorityID, req.MenuID)
|
|
if err != nil {
|
|
fail(c, "查询失败")
|
|
return
|
|
}
|
|
writeResult(c, codeSuccess, gin.H{"selected": selected}, "查询成功")
|
|
})
|
|
buttons.POST("/setAuthorityBtn", func(c *gin.Context) {
|
|
var req struct {
|
|
MenuID uint `json:"menuID"`
|
|
AuthorityID uint `json:"authorityId"`
|
|
Selected []uint `json:"selected"`
|
|
}
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := svc.SetSelectedButtons(c.Request.Context(), req.AuthorityID, req.MenuID, req.Selected); err != nil {
|
|
fail(c, "分配失败")
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
buttons.POST("/canRemoveAuthorityBtn", func(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Query("id"), 10, 64)
|
|
allowed, err := svc.CanRemoveButton(c.Request.Context(), uint(id))
|
|
if err != nil {
|
|
fail(c, "检查失败")
|
|
return
|
|
}
|
|
if !allowed {
|
|
fail(c, "此按钮正在被使用无法删除")
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
}
|