Repository: casbin/casbin
Pull request #1341 (2023-11-22 14:07:10)

feat: support map in ABAC
fixes: #1328

casbin-bot:  @tangyang9464 @JalinWang please review

Patch:
diff --git a/README.md b/README.md
index 2c151b4..d3a931d 100644
--- a/README.md
+++ b/README.md
@@ -116,7 +116,7 @@ m = r.obj == p.obj && r.act == p.act || r.obj in ('data2', 'data3')
 
 But you **SHOULD** make sure that the length of the array is **MORE** than **1**, otherwise there will cause it to panic.
 
-For more operators, you may take a look at [govaluate](https://github.com/Knetic/govaluate)
+For more operators, you may take a look at [govaluate](https://github.com/casbin/govaluate)
 
 ## Features
 
diff --git a/enforcer.go b/enforcer.go
index 5bbf982..7fe73f4 100644
--- a/enforcer.go
+++ b/enforcer.go
@@ -31,7 +31,7 @@ import (
 	defaultrolemanager "github.com/casbin/casbin/v2/rbac/default-role-manager"
 	"github.com/casbin/casbin/v2/util"
 
-	"github.com/Knetic/govaluate"
+	"github.com/casbin/govaluate"
 	"github.com/tidwall/gjson"
 )
 
diff --git a/enforcer_interface.go b/enforcer_interface.go
index bec1409..76a6615 100644
--- a/enforcer_interface.go
+++ b/enforcer_interface.go
@@ -15,11 +15,11 @@
 package casbin
 
 import (
-	"github.com/Knetic/govaluate"
 	"github.com/casbin/casbin/v2/effector"
 	"github.com/casbin/casbin/v2/model"
 	"github.com/casbin/casbin/v2/persist"
 	"github.com/casbin/casbin/v2/rbac"
+	"github.com/casbin/govaluate"
 )
 
 var _ IEnforcer = &Enforcer{}
diff --git a/enforcer_synced.go b/enforcer_synced.go
index db9de5b..16c2c85 100644
--- a/enforcer_synced.go
+++ b/enforcer_synced.go
@@ -19,7 +19,7 @@ import (
 	"sync/atomic"
 	"time"
 
-	"github.com/Knetic/govaluate"
+	"github.com/casbin/govaluate"
 
 	"github.com/casbin/casbin/v2/persist"
 	"github.com/casbin/casbin/v2/rbac"
diff --git a/go.mod b/go.mod
index a766c6d..77e9498 100644
--- a/go.mod
+++ b/go.mod
@@ -1,7 +1,7 @@
 module github.com/casbin/casbin/v2
 
 require (
-	github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible
+	github.com/casbin/govaluate v1.1.0
 	github.com/golang/mock v1.4.4
 	github.com/tidwall/gjson v1.14.4
 )
diff --git a/go.sum b/go.sum
index f747ee0..5766d46 100644
--- a/go.sum
+++ b/go.sum
@@ -1,5 +1,5 @@
-github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible h1:1G1pk05UrOh0NlF1oeaaix1x8XzrfjIDK47TY0Zehcw=
-github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
+github.com/casbin/govaluate v1.1.0 h1:6xdCWIpE9CwHdZhlVQW+froUrCsjb6/ZYNcXODfLT+E=
+github.com/casbin/govaluate v1.1.0/go.mod h1:G/UnbIjZk/0uMNaLwZZmFQrR72tYRZWQkO70si/iR7A=
 github.com/golang/mock v1.4.4 h1:l75CXGRSwbaYNpl/Z2X1XIIAMSCquvXgpVZDhwEIJsc=
 github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4=
 github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM=
diff --git a/management_api.go b/management_api.go
index 0797743..e2c7584 100644
--- a/management_api.go
+++ b/management_api.go
@@ -19,8 +19,8 @@ import (
 	"fmt"
 	"strings"
 
-	"github.com/Knetic/govaluate"
 	"github.com/casbin/casbin/v2/util"
+	"github.com/casbin/govaluate"
 )
 
 // GetAllSubjects gets the list of subjects that show up in the current policy.
diff --git a/model/function.go b/model/function.go
index 0ae1246..1febb6d 100644
--- a/model/function.go
+++ b/model/function.go
@@ -17,8 +17,8 @@ package model
 import (
 	"sync"
 
-	"github.com/Knetic/govaluate"
 	"github.com/casbin/casbin/v2/util"
+	"github.com/casbin/govaluate"
 )
 
 // FunctionMap represents the collection of Function.
diff --git a/util/builtin_operators.go b/util/builtin_operators.go
index 2206d07..80c9e43 100644
--- a/util/builtin_operators.go
+++ b/util/builtin_operators.go
@@ -26,7 +26,7 @@ import (
 
 	"github.com/casbin/casbin/v2/rbac"
 
-	"github.com/Knetic/govaluate"
+	"github.com/casbin/govaluate"
 )
 
 var (


Test patch:
diff --git a/model_test.go b/model_test.go
index b3ff964..8c5264f 100644
--- a/model_test.go
+++ b/model_test.go
@@ -452,6 +452,28 @@ func TestABACModel(t *testing.T) {
 	testEnforce(t, e, "bob", data2, "write", true)
 }
 
+func TestABACMapRequest(t *testing.T) {
+	e, _ := NewEnforcer("examples/abac_model.conf")
+
+	data1 := map[string]interface{}{
+		"Name":  "data1",
+		"Owner": "alice",
+	}
+	data2 := map[string]interface{}{
+		"Name":  "data2",
+		"Owner": "bob",
+	}
+
+	testEnforce(t, e, "alice", data1, "read", true)
+	testEnforce(t, e, "alice", data1, "write", true)
+	testEnforce(t, e, "alice", data2, "read", false)
+	testEnforce(t, e, "alice", data2, "write", false)
+	testEnforce(t, e, "bob", data1, "read", false)
+	testEnforce(t, e, "bob", data1, "write", false)
+	testEnforce(t, e, "bob", data2, "read", true)
+	testEnforce(t, e, "bob", data2, "write", true)
+}
+
 func TestABACJsonRequest(t *testing.T) {
 	e, _ := NewEnforcer("examples/abac_model.conf")
 	e.EnableAcceptJsonRequest(true)