Skip to content

Commit

Permalink
feat: fix result for "ABAC with policy rule" model (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
sukidayou authored Jan 12, 2025
1 parent b23b514 commit 65d7637
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
11 changes: 11 additions & 0 deletions examples/abac_rule_model.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[request_definition]
r = sub, obj, act

[policy_definition]
p = sub_rule, obj, act

[policy_effect]
e = some(where (p.eft == allow))

[matchers]
m = eval(p.sub_rule) && r.obj == p.obj && r.act == p.act
1 change: 1 addition & 0 deletions examples/abac_rule_policy.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
p, r.sub.Age > 18 && r.sub.Age < 60, /data1, read
52 changes: 51 additions & 1 deletion src/main/java/org/casbin/CommandExecutor.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.casbin;

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

/***
* Converts a string input into a JSON formatted string.
*
* @param input The input string to be converted to JSON format. It should be enclosed in curly braces {}.
* @return A JSON formatted string representing the key-value pairs from the input string.
*/
public static String convertToJson(String input) {
input = input.trim().substring(1, input.length() - 1).trim();
StringBuilder jsonBuilder = new StringBuilder("{");
String[] pairs = input.split(",");
for (String pair : pairs) {
pair = pair.trim();
String[] keyValue = pair.split(":");
if (keyValue.length == 2) {
String key = keyValue[0].trim();
String value = keyValue[1].trim();
jsonBuilder.append("\"").append(key).append("\":").append(value).append(",");
}
}
if (jsonBuilder.length() > 1) {
jsonBuilder.deleteCharAt(jsonBuilder.length() - 1);
}
jsonBuilder.append("}");
return jsonBuilder.toString();
}

public String outputResult() throws InvocationTargetException, IllegalAccessException, JsonProcessingException {
Class<? extends Enforcer> clazz = enforcer.getClass();
Method[] methods = clazz.getMethods();
Expand Down Expand Up @@ -75,7 +102,30 @@ public String outputResult() throws InvocationTargetException, IllegalAccessExce
}
}

Object invoke = method.invoke(enforcer, convertedParams);
Object[] extraConvertedParams = new Object[inputVal.length];
boolean hasJson = false;
try{
ObjectMapper objectMapper = new ObjectMapper();
if(inputVal.length > 0 && inputVal[0].trim().startsWith("{")) {
Map<String, Object> objectMap = objectMapper.readValue(convertToJson(inputVal[0]), new TypeReference<Map<String, Object>>() {
});
extraConvertedParams[0] = objectMap;
if (inputVal.length >= 1) {
System.arraycopy(inputVal, 1, extraConvertedParams, 1, inputVal.length - 1);
}
hasJson = true;
}
} catch (Exception e) {
e.printStackTrace();
hasJson = false;
}
Object invoke;
if(hasJson){
invoke = method.invoke(enforcer, (Object) extraConvertedParams);
} else {
invoke = method.invoke(enforcer, convertedParams);
}

if(returnType == boolean.class) {
responseBody.setAllow((Boolean) invoke);
} else if (returnType == List.class) {
Expand Down
6 changes: 5 additions & 1 deletion src/test/java/org/casbin/ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -319,5 +319,9 @@ public void resetBasicWithResourcesPolicyFile() {
}
}


@Test
public void testABACRule() {
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}");
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\"]}");
}
}

0 comments on commit 65d7637

Please sign in to comment.