Repository: casbin/casbin
Pull request #894 (2021-09-28 17:28:15)

fix: model AddPolicy method can't set PolicyMap correctly
Signed-off-by: tangyang9464 tangyang9464@163.com
fix: #895

casbin-bot:  @closetool please review

Patch:
diff --git a/enforcer.go b/enforcer.go
index 62d6288..e0f69f2 100644
--- a/enforcer.go
+++ b/enforcer.go
@@ -52,10 +52,10 @@ type Enforcer struct {
 
 // EnforceContext is used as the first element of the parameter "rvals" in method "enforce"
 type EnforceContext struct {
-	RType string
-	PType string
-	EType string
-	MType string
+	rType string
+	pType string
+	eType string
+	mType string
 }
 
 // NewEnforcer creates an enforcer via file or DB.
@@ -456,10 +456,10 @@ func (e *Enforcer) BuildIncrementalRoleLinks(op model.PolicyOp, ptype string, ru
 // NewEnforceContext Create a default structure based on the suffix
 func NewEnforceContext(suffix string) EnforceContext {
 	return EnforceContext{
-		RType: "r" + suffix,
-		PType: "p" + suffix,
-		EType: "e" + suffix,
-		MType: "m" + suffix,
+		rType: "r" + suffix,
+		pType: "p" + suffix,
+		eType: "e" + suffix,
+		mType: "m" + suffix,
 	}
 }
 
@@ -493,10 +493,10 @@ func (e *Enforcer) enforce(matcher string, explains *[]string, rvals ...interfac
 		switch rvals[0].(type) {
 		case EnforceContext:
 			enforceContext := rvals[0].(EnforceContext)
-			rType = enforceContext.RType
-			pType = enforceContext.PType
-			eType = enforceContext.EType
-			mType = enforceContext.MType
+			rType = enforceContext.rType
+			pType = enforceContext.pType
+			eType = enforceContext.eType
+			mType = enforceContext.mType
 			rvals = rvals[1:]
 		default:
 			break
@@ -667,7 +667,7 @@ func (e *Enforcer) enforce(matcher string, explains *[]string, rvals ...interfac
 		if len(*explains) > 0 {
 			logExplains = append(logExplains, *explains)
 		}
-
+		
 		if explainIndex != -1 && len(e.model["p"][pType].Policy) > explainIndex {
 			*explains = e.model["p"][pType].Policy[explainIndex]
 			logExplains = append(logExplains, *explains)
diff --git a/model/policy.go b/model/policy.go
index 473c380..e0e645b 100644
--- a/model/policy.go
+++ b/model/policy.go
@@ -146,6 +146,7 @@ func (model Model) HasPolicies(sec string, ptype string, rules [][]string) bool
 func (model Model) AddPolicy(sec string, ptype string, rule []string) {
 	assertion := model[sec][ptype]
 	assertion.Policy = append(assertion.Policy, rule)
+	assertion.PolicyMap[strings.Join(rule, DefaultSep)] = len(model[sec][ptype].Policy) - 1
 
 	if sec == "p" && assertion.priorityIndex >= 0 {
 		if idxInsert, err := strconv.Atoi(rule[assertion.priorityIndex]); err == nil {
@@ -157,6 +158,7 @@ func (model Model) AddPolicy(sec string, ptype string, rule []string) {
 				}
 				if idx > idxInsert {
 					assertion.Policy[i] = assertion.Policy[i-1]
+					assertion.PolicyMap[strings.Join(assertion.Policy[i-1], DefaultSep)]++
 				} else {
 					break
 				}
@@ -165,7 +167,6 @@ func (model Model) AddPolicy(sec string, ptype string, rule []string) {
 			assertion.PolicyMap[strings.Join(rule, DefaultSep)] = i
 		}
 	}
-	assertion.PolicyMap[strings.Join(rule, DefaultSep)] = len(model[sec][ptype].Policy) - 1
 }
 
 // AddPolicies adds policy rules to the model.


Test patch:
diff --git a/enforcer_test.go b/enforcer_test.go
index cda3bb3..3673398 100644
--- a/enforcer_test.go
+++ b/enforcer_test.go
@@ -534,7 +534,7 @@ func TestSubjectPriorityWithDomain(t *testing.T) {
 func TestMultiplePolicyDefinitions(t *testing.T) {
 	e, _ := NewEnforcer("examples/multiple_policy_definitions_model.conf", "examples/multiple_policy_definitions_policy.csv")
 	enforceContext := NewEnforceContext("2")
-	enforceContext.EType = "e"
+	enforceContext.eType = "e"
 	testBatchEnforce(t, e, [][]interface{}{
 		{"alice", "data2", "read"},
 		{enforceContext, struct{ Age int }{Age: 70}, "/data1", "read"},