mirror of
https://github.com/casdoor/casdoor.git
synced 2025-09-06 18:10:29 +08:00
feat: Refactor GetFilteredPolicies to support multiple filters via POST (#4037)
This commit is contained in:
@@ -206,6 +206,13 @@ func GetPolicies(id string) ([]*xormadapter.CasbinRule, error) {
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// Filter represents filter criteria with optional policy type
|
||||
type Filter struct {
|
||||
Ptype string `json:"ptype,omitempty"`
|
||||
FieldIndex int `json:"fieldIndex"`
|
||||
FieldValues []string `json:"fieldValues"`
|
||||
}
|
||||
|
||||
func GetFilteredPolicies(id string, ptype string, fieldIndex int, fieldValues ...string) ([]*xormadapter.CasbinRule, error) {
|
||||
enforcer, err := GetInitializedEnforcer(id)
|
||||
if err != nil {
|
||||
@@ -236,6 +243,80 @@ func GetFilteredPolicies(id string, ptype string, fieldIndex int, fieldValues ..
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// GetFilteredPoliciesMulti applies multiple filters to policies
|
||||
// Doing this in our loop is more efficient than using GetFilteredGroupingPolicy / GetFilteredPolicy which
|
||||
// iterates over all policies again and again
|
||||
func GetFilteredPoliciesMulti(id string, filters []Filter) ([]*xormadapter.CasbinRule, error) {
|
||||
// Get all policies first
|
||||
allPolicies, err := GetPolicies(id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Filter policies based on multiple criteria
|
||||
var filteredPolicies []*xormadapter.CasbinRule
|
||||
if len(filters) == 0 {
|
||||
// No filters, return all policies
|
||||
return allPolicies, nil
|
||||
} else {
|
||||
for _, policy := range allPolicies {
|
||||
matchesAllFilters := true
|
||||
for _, filter := range filters {
|
||||
// If no policy type is specified, set it to the default policy type
|
||||
if filter.Ptype == "" {
|
||||
filter.Ptype = "p"
|
||||
}
|
||||
// Check policy type
|
||||
if policy.Ptype != filter.Ptype {
|
||||
matchesAllFilters = false
|
||||
break
|
||||
}
|
||||
|
||||
// Check if field index is valid (0-5 for V0-V5)
|
||||
if filter.FieldIndex < 0 || filter.FieldIndex > 5 {
|
||||
matchesAllFilters = false
|
||||
break
|
||||
}
|
||||
|
||||
fieldValue := ""
|
||||
switch filter.FieldIndex {
|
||||
case 0:
|
||||
fieldValue = policy.V0
|
||||
case 1:
|
||||
fieldValue = policy.V1
|
||||
case 2:
|
||||
fieldValue = policy.V2
|
||||
case 3:
|
||||
fieldValue = policy.V3
|
||||
case 4:
|
||||
fieldValue = policy.V4
|
||||
case 5:
|
||||
fieldValue = policy.V5
|
||||
}
|
||||
|
||||
found := false
|
||||
// Check if field value is in the list of expected values
|
||||
for _, expectedValue := range filter.FieldValues {
|
||||
if fieldValue == expectedValue {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
matchesAllFilters = false
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if matchesAllFilters {
|
||||
filteredPolicies = append(filteredPolicies, policy)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return filteredPolicies, nil
|
||||
}
|
||||
|
||||
func UpdatePolicy(id string, ptype string, oldPolicy []string, newPolicy []string) (bool, error) {
|
||||
enforcer, err := GetInitializedEnforcer(id)
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user