api 增加 GET PUT DELETE POST 四种可选声明方式

This commit is contained in:
pixel
2020-01-18 11:08:54 +08:00
parent baaf84bce0
commit 4960e269c0
5 changed files with 93 additions and 33 deletions

View File

@@ -7,23 +7,18 @@ import (
"github.com/gin-gonic/gin"
)
type CasbinInReceive struct {
AuthorityId string `json:"authorityId"`
Paths []string `json:paths`
}
// @Tags casbin
// @Summary 更改角色api权限
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body api.CreateAuthorityParams true "更改角色api权限"
// @Param data body sysModel.CasbinInReceive true "更改角色api权限"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /casbin/casbinPUpdata [post]
func CasbinPUpdata(c *gin.Context) {
var cmr CasbinInReceive
var cmr sysModel.CasbinInReceive
_ = c.ShouldBind(&cmr)
err := new(sysModel.CasbinModel).CasbinPUpdata(cmr.AuthorityId, cmr.Paths)
err := new(sysModel.CasbinModel).CasbinPUpdata(cmr.AuthorityId, cmr.CasbinInfos)
if err != nil {
servers.ReportFormat(c, false, fmt.Sprintf("添加规则失败,%v", err), gin.H{})
} else {
@@ -40,7 +35,7 @@ func CasbinPUpdata(c *gin.Context) {
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /casbin/getPolicyPathByAuthorityId [post]
func GetPolicyPathByAuthorityId(c *gin.Context) {
var cmr CasbinInReceive
var cmr sysModel.CasbinInReceive
_ = c.ShouldBind(&cmr)
paths := new(sysModel.CasbinModel).GetPolicyPathByAuthorityId(cmr.AuthorityId)
servers.ReportFormat(c, true, "获取规则成功", gin.H{"paths": paths})

View File

@@ -13,6 +13,7 @@ type SysApi struct {
Path string `json:"path"`
Description string `json:"description"`
Group string `json:"group"`
Method string `json:"method" gorm:"default:'POST'"`
}
func (a *SysApi) CreateApi() (err error) {

View File

@@ -17,16 +17,28 @@ type CasbinModel struct {
Method string `json:"method" gorm:"column:v2"`
}
// 供入参使用
type CasbinInfo struct {
Path string `json:"path"`
Method string `json:"method"`
}
// 供入参使用
type CasbinInReceive struct {
AuthorityId string `json:"authorityId"`
CasbinInfos []CasbinInfo `json:"casbinInfos"`
}
// 更新权限
func (c *CasbinModel) CasbinPUpdata(AuthorityId string, Paths []string) error {
func (c *CasbinModel) CasbinPUpdata(AuthorityId string, casbinInfos []CasbinInfo) error {
c.clearCasbin(0, AuthorityId)
for _, v := range Paths {
for _, v := range casbinInfos {
cm := CasbinModel{
ID: 0,
Ptype: "p",
AuthorityId: AuthorityId,
Path: v,
Method: "POST",
Path: v.Path,
Method: v.Method,
}
addflag := c.AddCasbin(cm)
if addflag == false {