feat: add get-filtered-policies API, improve Swagger docs (#4006)

This commit is contained in:
Robin Ye
2025-07-29 23:51:01 +08:00
committed by GitHub
parent f3af2a26aa
commit f9264f700b
5 changed files with 2009 additions and 44 deletions

View File

@@ -17,6 +17,7 @@ package controllers
import (
"encoding/json"
"fmt"
"strings"
"github.com/beego/beego/utils/pagination"
"github.com/casdoor/casdoor/object"
@@ -154,6 +155,14 @@ func (c *ApiController) DeleteEnforcer() {
c.ServeJSON()
}
// GetPolicies
// @Title GetPolicies
// @Tag Enforcer API
// @Description get policies
// @Param id query string true "The id ( owner/name ) of enforcer"
// @Param adapterId query string false "The adapter id"
// @Success 200 {array} xormadapter.CasbinRule
// @router /get-policies [get]
func (c *ApiController) GetPolicies() {
id := c.Input().Get("id")
adapterId := c.Input().Get("adapterId")
@@ -188,6 +197,50 @@ func (c *ApiController) GetPolicies() {
c.ResponseOk(policies)
}
// GetFilteredPolicies
// @Title GetFilteredPolicies
// @Tag Enforcer API
// @Description get filtered policies
// @Param id query string true "The id ( owner/name ) of enforcer"
// @Param ptype query string false "Policy type, default is 'p'"
// @Param fieldIndex query int false "Field index for filtering"
// @Param fieldValues query string false "Field values for filtering, comma-separated"
// @Success 200 {array} xormadapter.CasbinRule
// @router /get-filtered-policies [get]
func (c *ApiController) GetFilteredPolicies() {
id := c.Input().Get("id")
ptype := c.Input().Get("ptype")
fieldIndexStr := c.Input().Get("fieldIndex")
fieldValuesStr := c.Input().Get("fieldValues")
if ptype == "" {
ptype = "p"
}
fieldIndex := util.ParseInt(fieldIndexStr)
var fieldValues []string
if fieldValuesStr != "" {
fieldValues = strings.Split(fieldValuesStr, ",")
}
policies, err := object.GetFilteredPolicies(id, ptype, fieldIndex, fieldValues...)
if err != nil {
c.ResponseError(err.Error())
return
}
c.ResponseOk(policies)
}
// UpdatePolicy
// @Title UpdatePolicy
// @Tag Enforcer API
// @Description update policy
// @Param id query string true "The id ( owner/name ) of enforcer"
// @Param body body []xormadapter.CasbinRule true "Array containing old and new policy"
// @Success 200 {object} Response
// @router /update-policy [post]
func (c *ApiController) UpdatePolicy() {
id := c.Input().Get("id")
@@ -207,6 +260,14 @@ func (c *ApiController) UpdatePolicy() {
c.ServeJSON()
}
// AddPolicy
// @Title AddPolicy
// @Tag Enforcer API
// @Description add policy
// @Param id query string true "The id ( owner/name ) of enforcer"
// @Param body body xormadapter.CasbinRule true "The policy to add"
// @Success 200 {object} Response
// @router /add-policy [post]
func (c *ApiController) AddPolicy() {
id := c.Input().Get("id")
@@ -226,6 +287,14 @@ func (c *ApiController) AddPolicy() {
c.ServeJSON()
}
// RemovePolicy
// @Title RemovePolicy
// @Tag Enforcer API
// @Description remove policy
// @Param id query string true "The id ( owner/name ) of enforcer"
// @Param body body xormadapter.CasbinRule true "The policy to remove"
// @Success 200 {object} Response
// @router /remove-policy [post]
func (c *ApiController) RemovePolicy() {
id := c.Input().Get("id")

View File

@@ -206,6 +206,36 @@ func GetPolicies(id string) ([]*xormadapter.CasbinRule, error) {
return res, nil
}
func GetFilteredPolicies(id string, ptype string, fieldIndex int, fieldValues ...string) ([]*xormadapter.CasbinRule, error) {
enforcer, err := GetInitializedEnforcer(id)
if err != nil {
return nil, err
}
var allRules [][]string
if len(fieldValues) == 0 {
if ptype == "g" {
allRules = enforcer.GetFilteredGroupingPolicy(fieldIndex)
} else {
allRules = enforcer.GetFilteredPolicy(fieldIndex)
}
} else {
for _, value := range fieldValues {
if ptype == "g" {
rules := enforcer.GetFilteredGroupingPolicy(fieldIndex, value)
allRules = append(allRules, rules...)
} else {
rules := enforcer.GetFilteredPolicy(fieldIndex, value)
allRules = append(allRules, rules...)
}
}
}
res := util.MatrixToCasbinRules(ptype, allRules)
return res, nil
}
func UpdatePolicy(id string, ptype string, oldPolicy []string, newPolicy []string) (bool, error) {
enforcer, err := GetInitializedEnforcer(id)
if err != nil {

View File

@@ -160,6 +160,7 @@ func initAPI() {
beego.Router("/api/add-adapter", &controllers.ApiController{}, "POST:AddAdapter")
beego.Router("/api/delete-adapter", &controllers.ApiController{}, "POST:DeleteAdapter")
beego.Router("/api/get-policies", &controllers.ApiController{}, "GET:GetPolicies")
beego.Router("/api/get-filtered-policies", &controllers.ApiController{}, "GET:GetFilteredPolicies")
beego.Router("/api/update-policy", &controllers.ApiController{}, "POST:UpdatePolicy")
beego.Router("/api/add-policy", &controllers.ApiController{}, "POST:AddPolicy")
beego.Router("/api/remove-policy", &controllers.ApiController{}, "POST:RemovePolicy")

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff