Skip to content

Commit 65d7637

Browse files
authored
feat: fix result for "ABAC with policy rule" model (#34)
1 parent b23b514 commit 65d7637

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed

examples/abac_rule_model.conf

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[request_definition]
2+
r = sub, obj, act
3+
4+
[policy_definition]
5+
p = sub_rule, obj, act
6+
7+
[policy_effect]
8+
e = some(where (p.eft == allow))
9+
10+
[matchers]
11+
m = eval(p.sub_rule) && r.obj == p.obj && r.act == p.act

examples/abac_rule_policy.csv

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
p, r.sub.Age > 18 && r.sub.Age < 60, /data1, read

src/main/java/org/casbin/CommandExecutor.java

+51-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.casbin;
22

33
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.core.type.TypeReference;
45
import com.fasterxml.jackson.databind.ObjectMapper;
56
import org.casbin.jcasbin.main.EnforceResult;
67
import org.casbin.jcasbin.main.Enforcer;
@@ -25,6 +26,32 @@ public CommandExecutor(NewEnforcer enforcer, String inputMethodName, String[] in
2526
this.inputVal = inputVal;
2627
}
2728

29+
/***
30+
* Converts a string input into a JSON formatted string.
31+
*
32+
* @param input The input string to be converted to JSON format. It should be enclosed in curly braces {}.
33+
* @return A JSON formatted string representing the key-value pairs from the input string.
34+
*/
35+
public static String convertToJson(String input) {
36+
input = input.trim().substring(1, input.length() - 1).trim();
37+
StringBuilder jsonBuilder = new StringBuilder("{");
38+
String[] pairs = input.split(",");
39+
for (String pair : pairs) {
40+
pair = pair.trim();
41+
String[] keyValue = pair.split(":");
42+
if (keyValue.length == 2) {
43+
String key = keyValue[0].trim();
44+
String value = keyValue[1].trim();
45+
jsonBuilder.append("\"").append(key).append("\":").append(value).append(",");
46+
}
47+
}
48+
if (jsonBuilder.length() > 1) {
49+
jsonBuilder.deleteCharAt(jsonBuilder.length() - 1);
50+
}
51+
jsonBuilder.append("}");
52+
return jsonBuilder.toString();
53+
}
54+
2855
public String outputResult() throws InvocationTargetException, IllegalAccessException, JsonProcessingException {
2956
Class<? extends Enforcer> clazz = enforcer.getClass();
3057
Method[] methods = clazz.getMethods();
@@ -75,7 +102,30 @@ public String outputResult() throws InvocationTargetException, IllegalAccessExce
75102
}
76103
}
77104

78-
Object invoke = method.invoke(enforcer, convertedParams);
105+
Object[] extraConvertedParams = new Object[inputVal.length];
106+
boolean hasJson = false;
107+
try{
108+
ObjectMapper objectMapper = new ObjectMapper();
109+
if(inputVal.length > 0 && inputVal[0].trim().startsWith("{")) {
110+
Map<String, Object> objectMap = objectMapper.readValue(convertToJson(inputVal[0]), new TypeReference<Map<String, Object>>() {
111+
});
112+
extraConvertedParams[0] = objectMap;
113+
if (inputVal.length >= 1) {
114+
System.arraycopy(inputVal, 1, extraConvertedParams, 1, inputVal.length - 1);
115+
}
116+
hasJson = true;
117+
}
118+
} catch (Exception e) {
119+
e.printStackTrace();
120+
hasJson = false;
121+
}
122+
Object invoke;
123+
if(hasJson){
124+
invoke = method.invoke(enforcer, (Object) extraConvertedParams);
125+
} else {
126+
invoke = method.invoke(enforcer, convertedParams);
127+
}
128+
79129
if(returnType == boolean.class) {
80130
responseBody.setAllow((Boolean) invoke);
81131
} else if (returnType == List.class) {

src/test/java/org/casbin/ClientTest.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -319,5 +319,9 @@ public void resetBasicWithResourcesPolicyFile() {
319319
}
320320
}
321321

322-
322+
@Test
323+
public void testABACRule() {
324+
assertEquals(Client.run(new String[]{"enforce", "-m", "examples/abac_rule_model.conf", "-p", "examples/abac_rule_policy.csv", "{Age: 30}", "/data1", "read"}), "{\"allow\":true,\"explain\":null}");
325+
assertEquals(Client.run(new String[]{"enforceEx", "-m", "examples/abac_rule_model.conf", "-p", "examples/abac_rule_policy.csv", "{Age: 30}", "/data1", "read"}), "{\"allow\":true,\"explain\":[\"r.sub.Age > 18 && r.sub.Age < 60\",\"/data1\",\"read\"]}");
326+
}
323327
}

0 commit comments

Comments
 (0)