From 2c363e14043d493ff082c1487f9fcbe859979256 Mon Sep 17 00:00:00 2001
From: LMay <67900553+LMay001@users.noreply.github.com>
Date: Sun, 4 Feb 2024 01:33:52 +0800
Subject: [PATCH] feat: add ABAC & JSON related test cases (#376)
---
examples/abac_model.conf | 2 +-
examples/abac_rule_policy.csv | 5 +-
pom.xml | 6 +
.../org/casbin/jcasbin/main/CoreEnforcer.java | 35 ++++++
.../java/org/casbin/jcasbin/util/Util.java | 18 +++
.../casbin/jcasbin/main/AbacAPIUnitTest.java | 2 +-
.../casbin/jcasbin/main/ModelUnitTest.java | 110 +++++++++++++++++-
7 files changed, 172 insertions(+), 6 deletions(-)
diff --git a/examples/abac_model.conf b/examples/abac_model.conf
index 50e3281e..44d06046 100644
--- a/examples/abac_model.conf
+++ b/examples/abac_model.conf
@@ -8,4 +8,4 @@ p = sub, obj, act
e = some(where (p.eft == allow))
[matchers]
-m = r.sub == r.obj.owner
\ No newline at end of file
+m = r.sub == r.obj.Owner
diff --git a/examples/abac_rule_policy.csv b/examples/abac_rule_policy.csv
index 2ae26dcb..be29dedf 100644
--- a/examples/abac_rule_policy.csv
+++ b/examples/abac_rule_policy.csv
@@ -1,3 +1,2 @@
-p, r.sub.not_exist_attribute_test == true, /data0, read
-p, r.sub.age > 18 && r.sub.age < 25, /data1, read
-p, r.sub.age < 60, /data2, write
+p, r.sub.Age > 18, /data1, read
+p, r.sub.Age < 60, /data2, write
diff --git a/pom.xml b/pom.xml
index df5a0b9d..86f6d00a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -229,6 +229,12 @@
gson
2.9.0
+
+ com.fasterxml.jackson.core
+ jackson-databind
+ 2.13.1
+
+
diff --git a/src/main/java/org/casbin/jcasbin/main/CoreEnforcer.java b/src/main/java/org/casbin/jcasbin/main/CoreEnforcer.java
index f77f13a0..ad4867d8 100644
--- a/src/main/java/org/casbin/jcasbin/main/CoreEnforcer.java
+++ b/src/main/java/org/casbin/jcasbin/main/CoreEnforcer.java
@@ -14,6 +14,9 @@
package org.casbin.jcasbin.main;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.AviatorEvaluatorInstance;
import com.googlecode.aviator.Expression;
@@ -57,6 +60,7 @@ public class CoreEnforcer {
boolean autoBuildRoleLinks;
boolean autoNotifyWatcher = true;
boolean autoNotifyDispatcher = true;
+ boolean acceptJsonRequest = false;
private AviatorEvaluatorInstance aviatorEval;
@@ -447,6 +451,15 @@ public void enableAutoBuildRoleLinks(boolean autoBuildRoleLinks) {
this.autoBuildRoleLinks = autoBuildRoleLinks;
}
+ /**
+ * EnableAcceptJsonRequest controls whether to accept json as a request parameter
+ *
+ * @param acceptJsonRequest
+ */
+ public void enableAcceptJsonRequest(boolean acceptJsonRequest) {
+ this.acceptJsonRequest = acceptJsonRequest;
+ }
+
/**
* buildRoleLinks manually rebuild the
* role inheritance relations.
@@ -516,6 +529,28 @@ private EnforceResult enforce(String matcher, Object... rvals) {
expString = Util.removeComments(Util.escapeAssertion(matcher));
}
+ // json process
+ if (acceptJsonRequest) {
+ try {
+ List