Skip to content

Commit 623968c

Browse files
authored
feat: fix "ABAC" model enforceEx error (#40)
1 parent cb0fa09 commit 623968c

File tree

4 files changed

+43
-23
lines changed

4 files changed

+43
-23
lines changed

examples/abac_model.conf

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

examples/abac_policy.csv

Whitespace-only changes.

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

+30-23
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,29 @@ public CommandExecutor(NewEnforcer enforcer, String inputMethodName, String[] in
3333
* @return A JSON formatted string representing the key-value pairs from the input string.
3434
*/
3535
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(",");
36+
input = input.trim();
37+
// Handle the simple format {key: value}
38+
if (!input.contains("\"")) {
39+
input = input.substring(1, input.length() - 1).trim();
40+
StringBuilder jsonBuilder = new StringBuilder("{");
41+
String[] pairs = input.split(",");
42+
for (String pair : pairs) {
43+
pair = pair.trim();
44+
String[] keyValue = pair.split(":");
45+
if (keyValue.length == 2) {
46+
String key = keyValue[0].trim();
47+
String value = keyValue[1].trim();
48+
jsonBuilder.append("\"").append(key).append("\":").append(value).append(",");
49+
}
4650
}
51+
if (jsonBuilder.length() > 1) {
52+
jsonBuilder.deleteCharAt(jsonBuilder.length() - 1);
53+
}
54+
jsonBuilder.append("}");
55+
return jsonBuilder.toString();
4756
}
48-
if (jsonBuilder.length() > 1) {
49-
jsonBuilder.deleteCharAt(jsonBuilder.length() - 1);
50-
}
51-
jsonBuilder.append("}");
52-
return jsonBuilder.toString();
57+
58+
return input;
5359
}
5460

5561
public String outputResult() throws InvocationTargetException, IllegalAccessException, JsonProcessingException {
@@ -104,16 +110,17 @@ public String outputResult() throws InvocationTargetException, IllegalAccessExce
104110

105111
Object[] extraConvertedParams = new Object[inputVal.length];
106112
boolean hasJson = false;
107-
try{
113+
try {
108114
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+
for (int i = 0; i < inputVal.length; i++) {
116+
if (inputVal[i].trim().startsWith("{")) {
117+
Map<String, Object> objectMap = objectMapper.readValue(convertToJson(inputVal[i]), new TypeReference<Map<String, Object>>() {
118+
});
119+
extraConvertedParams[i] = objectMap;
120+
hasJson = true;
121+
} else {
122+
extraConvertedParams[i] = inputVal[i];
115123
}
116-
hasJson = true;
117124
}
118125
} catch (Exception e) {
119126
e.printStackTrace();

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

+2
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,8 @@ public void resetBasicWithResourcesPolicyFile() {
323323
public void testABACRule() {
324324
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}");
325325
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+
assertEquals(Client.run(new String[]{"enforce", "-m", "examples/abac_model.conf", "-p", "examples/abac_policy.csv", "alice", "{ \"Owner\" : \"alice\" }"}), "{\"allow\":true,\"explain\":null}");
327+
assertEquals(Client.run(new String[]{"enforceEx", "-m", "examples/abac_model.conf", "-p", "examples/abac_policy.csv", "alice", "{ \"Owner\" : \"alice\" }"}), "{\"allow\":true,\"explain\":[]}");
326328
}
327329

328330
@Test

0 commit comments

Comments
 (0)