Repository: casbin/casbin
Pull request #938 (2021-12-16 16:56:50)

fix: CasbinJsGetPermissionForUser ignore p_eft and test
FIx: https://github.com/casbin/casbin/issues/926

casbin-bot:  @tangyang9464 @closetool @sagilio please review

Patch:
diff --git a/frontend.go b/frontend.go
index 0482b2b..f665095 100644
--- a/frontend.go
+++ b/frontend.go
@@ -19,7 +19,7 @@ import (
 	"encoding/json"
 )
 
-func CasbinJsGetPermissionForUser(e IEnforcer, user string) ([]byte, error) {
+func CasbinJsGetPermissionForUser(e IEnforcer, user string) (string, error) {
 	model := e.GetModel()
 	m := map[string]interface{}{}
 	m["m"] = model.ToText()
@@ -35,5 +35,5 @@ func CasbinJsGetPermissionForUser(e IEnforcer, user string) ([]byte, error) {
 	encoder := json.NewEncoder(result)
 	encoder.SetEscapeHTML(false)
 	err := encoder.Encode(m)
-	return result.Bytes(), err
+	return result.String(), err
 }
diff --git a/model/model.go b/model/model.go
index e72e7dd..2141677 100644
--- a/model/model.go
+++ b/model/model.go
@@ -345,6 +345,9 @@ func (model Model) ToText() string {
 			tokenPatterns[token] = rPattern.ReplaceAllString(pPattern.ReplaceAllString(token, "p."), "r.")
 		}
 	}
+	if strings.Contains(model["e"]["e"].Value, "p_eft") {
+		tokenPatterns["p_eft"] = "p.eft"
+	}
 	s := strings.Builder{}
 	writeString := func(sec string) {
 		for ptype := range model[sec] {


Test patch:
diff --git a/frontend_test.go b/frontend_test.go
index b2aab08..8461003 100644
--- a/frontend_test.go
+++ b/frontend_test.go
@@ -15,16 +15,48 @@
 package casbin
 
 import (
+	"encoding/json"
+	"io/ioutil"
+	"regexp"
+	"strings"
 	"testing"
 )
 
 func TestCasbinJsGetPermissionForUser(t *testing.T) {
-	e, err := NewSyncedEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv")
+	e, err := NewSyncedEnforcer("examples/rbac_model.conf", "examples/rbac_with_hierarchy_policy.csv")
 	if err != nil {
 		panic(err)
 	}
-	_, err = CasbinJsGetPermissionForUser(e, "alice") // make sure CasbinJsGetPermissionForUser can be used with a SyncedEnforcer.
+	receivedString, err := CasbinJsGetPermissionForUser(e, "alice") // make sure CasbinJsGetPermissionForUser can be used with a SyncedEnforcer.
 	if err != nil {
 		t.Errorf("Test error: %s", err)
 	}
+	received := map[string]interface{}{}
+	err = json.Unmarshal([]byte(receivedString), &received)
+	if err != nil {
+		t.Errorf("Test error: %s", err)
+	}
+	expectedModel, err := ioutil.ReadFile("examples/rbac_model.conf")
+	if err != nil {
+		t.Errorf("Test error: %s", err)
+	}
+	expectedModelStr := regexp.MustCompile("\n+").ReplaceAllString(string(expectedModel), "\n")
+	if strings.TrimSpace(received["m"].(string)) != expectedModelStr {
+		t.Errorf("%s supposed to be %s", strings.TrimSpace(received["m"].(string)), expectedModelStr)
+	}
+
+	expectedPolicies, err := ioutil.ReadFile("examples/rbac_with_hierarchy_policy.csv")
+	if err != nil {
+		t.Errorf("Test error: %s", err)
+	}
+	expectedPoliciesItem := regexp.MustCompile(",|\n").Split(string(expectedPolicies), -1)
+	i := 0
+	for _, sArr := range received["p"].([]interface{}) {
+		for _, s := range sArr.([]interface{}) {
+			if strings.TrimSpace(s.(string)) != strings.TrimSpace(expectedPoliciesItem[i]) {
+				t.Errorf("%s supposed to be %s", strings.TrimSpace(s.(string)), strings.TrimSpace(expectedPoliciesItem[i]))
+			}
+			i++
+		}
+	}
 }