91 lines
2.5 KiB
Go
91 lines
2.5 KiB
Go
package system
|
|
|
|
import (
|
|
"kra/pkg/response"
|
|
"kra/pkg/utils"
|
|
|
|
"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
|
|
// @Tags Casbin
|
|
// @Summary 更新角色api权限
|
|
// @Security ApiKeyAuth
|
|
// @accept application/json
|
|
// @Produce application/json
|
|
// @Param data body UpdateCasbinRequest true "权限id, 权限模型列表"
|
|
// @Success 200 {object} response.Response{msg=string} "更新角色api权限"
|
|
// @Router /casbin/updateCasbin [post]
|
|
func (ca *CasbinApi) UpdateCasbin(c *gin.Context) {
|
|
var req UpdateCasbinRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
// 获取当前用户的角色ID
|
|
adminAuthorityID := utils.GetUserAuthorityId(c)
|
|
|
|
// 转换为业务层格式
|
|
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(adminAuthorityID, req.AuthorityId, rules); err != nil {
|
|
response.FailWithMessage("更新失败: "+err.Error(), c)
|
|
return
|
|
}
|
|
|
|
response.OkWithMessage("更新成功", c)
|
|
}
|
|
|
|
// GetPolicyPathByAuthorityId
|
|
// @Tags Casbin
|
|
// @Summary 获取权限列表
|
|
// @Security ApiKeyAuth
|
|
// @accept application/json
|
|
// @Produce application/json
|
|
// @Param data body GetPolicyPathByAuthorityIdRequest true "权限id, 权限模型列表"
|
|
// @Success 200 {object} response.Response{data=map[string]interface{},msg=string} "获取权限列表,返回包括casbin详情列表"
|
|
// @Router /casbin/getPolicyPathByAuthorityId [post]
|
|
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)
|
|
}
|