Repository: casbin/casbin
Pull request #826 (2021-06-30 03:01:39)

fix: filter not support mutiple ptype
Signed-off-by: closetool <c299999999@qq.com>

Fix: #825 

closetool: @hsluoyz @Zxilly 

Patch:
diff --git a/persist/file-adapter/adapter_filtered.go b/persist/file-adapter/adapter_filtered.go
index 924ef2d..e3c6c0c 100644
--- a/persist/file-adapter/adapter_filtered.go
+++ b/persist/file-adapter/adapter_filtered.go
@@ -34,8 +34,13 @@ type FilteredAdapter struct {
 // Filter defines the filtering rules for a FilteredAdapter's policy. Empty values
 // are ignored, but all others must match the filter.
 type Filter struct {
-	P []string
-	G []string
+	P  []string
+	G  []string
+	G1 []string
+	G2 []string
+	G3 []string
+	G4 []string
+	G5 []string
 }
 
 // NewFilteredAdapter is the constructor for FilteredAdapter.
@@ -119,6 +124,16 @@ func filterLine(line string, filter *Filter) bool {
 		filterSlice = filter.P
 	case "g":
 		filterSlice = filter.G
+	case "g1":
+		filterSlice = filter.G1
+	case "g2":
+		filterSlice = filter.G2
+	case "g3":
+		filterSlice = filter.G3
+	case "g4":
+		filterSlice = filter.G4
+	case "g5":
+		filterSlice = filter.G5
 	}
 	return filterWords(p, filterSlice)
 }


Test patch:
diff --git a/filter_test.go b/filter_test.go
index 005b324..6fa7858 100644
--- a/filter_test.go
+++ b/filter_test.go
@@ -18,6 +18,7 @@ import (
 	"testing"
 
 	fileadapter "github.com/casbin/casbin/v2/persist/file-adapter"
+	"github.com/casbin/casbin/v2/util"
 )
 
 func TestInitFilteredAdapter(t *testing.T) {
@@ -65,6 +66,46 @@ func TestLoadFilteredPolicy(t *testing.T) {
 	}
 }
 
+func TestLoadMoreTypeFilteredPolicy(t *testing.T) {
+	e, _ := NewEnforcer()
+
+	adapter := fileadapter.NewFilteredAdapter("examples/rbac_with_pattern_policy.csv")
+	_ = e.InitWithAdapter("examples/rbac_with_pattern_model.conf", adapter)
+	if err := e.LoadPolicy(); err != nil {
+		t.Errorf("unexpected error in LoadPolicy: %v", err)
+	}
+	e.AddNamedMatchingFunc("g2", "matching func", util.KeyMatch2)
+	_ = e.BuildRoleLinks()
+
+	testEnforce(t, e, "alice", "/book/1", "GET", true)
+
+	// validate initial conditions
+	testHasPolicy(t, e, []string{"book_admin", "book_group", "GET"}, true)
+	testHasPolicy(t, e, []string{"pen_admin", "pen_group", "GET"}, true)
+
+	if err := e.LoadFilteredPolicy(&fileadapter.Filter{
+		P:  []string{"book_admin"},
+		G:  []string{"alice"},
+		G2: []string{"", "book_group"},
+	}); err != nil {
+		t.Errorf("unexpected error in LoadFilteredPolicy: %v", err)
+	}
+	if !e.IsFiltered() {
+		t.Errorf("adapter did not set the filtered flag correctly")
+	}
+
+	testHasPolicy(t, e, []string{"alice", "/pen/1", "GET"}, false)
+	testHasPolicy(t, e, []string{"alice", "/pen2/1", "GET"}, false)
+	testHasPolicy(t, e, []string{"pen_admin", "pen_group", "GET"}, false)
+	testHasGroupingPolicy(t, e, []string{"alice", "book_admin"}, true)
+	testHasGroupingPolicy(t, e, []string{"bob", "pen_admin"}, false)
+	testHasGroupingPolicy(t, e, []string{"cathy", "pen_admin"}, false)
+	testHasGroupingPolicy(t, e, []string{"cathy", "/book/1/2/3/4/5"}, false)
+
+	testEnforce(t, e, "alice", "/book/1", "GET", true)
+	testEnforce(t, e, "alice", "/pen/1", "GET", false)
+}
+
 func TestAppendFilteredPolicy(t *testing.T) {
 	e, _ := NewEnforcer()