Skip to content

Commit 2d718e5

Browse files
authored
fix: remove multiple domains in gFunction and simplify cache (casbin#265)
Signed-off-by: tangyang9464 <[email protected]>
1 parent 92bf9a4 commit 2d718e5

File tree

6 files changed

+20
-88
lines changed

6 files changed

+20
-88
lines changed

examples/rbac_with_multiple_domains_model.conf

-14
This file was deleted.

examples/rbac_with_multiple_domains_policy.csv

-9
This file was deleted.

src/main/java/org/casbin/jcasbin/main/CoreEnforcer.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -427,10 +427,8 @@ private boolean enforce(String matcher, Object... rvals) {
427427
Assertion ast = entry.getValue();
428428

429429
RoleManager rm = ast.rm;
430-
AviatorFunction aviatorFunction = BuiltInFunctions.generateGFunctionClass.generateGFunction(key, rm);
430+
AviatorFunction aviatorFunction = BuiltInFunctions.GenerateGFunctionClass.generateGFunction(key, rm);
431431
gFunctions.put(key, aviatorFunction);
432-
433-
BuiltInFunctions.generateGFunctionClass.updateGFunctionCache(key);
434432
}
435433
}
436434
for (AviatorFunction f : gFunctions.values()) {

src/main/java/org/casbin/jcasbin/util/BuiltInFunctions.java

+8-53
Original file line numberDiff line numberDiff line change
@@ -334,15 +334,10 @@ public static boolean allMatch(String key1, String key2) {
334334
}
335335

336336

337-
public static class generateGFunctionClass{
337+
public static class GenerateGFunctionClass{
338338
// key:name such as g,g2 value:user-role mapping
339339
private static Map<String, Map<String, AviatorBoolean>> memorizedMap = new HashMap<>();
340340

341-
public static void updateGFunctionCache(String name){
342-
Map<String, AviatorBoolean> memorized = memorizedMap.get(name);
343-
memorized = new HashMap<>();
344-
}
345-
346341
/**
347342
* generateGFunction is the factory method of the g(_, _) function.
348343
*
@@ -361,66 +356,26 @@ public AviatorObject variadicCall(Map<String, Object> env, AviatorObject... args
361356
if(len < 2){
362357
return AviatorBoolean.valueOf(false);
363358
}
364-
Object name1Obj = FunctionUtils.getJavaObject(args[0], env);
359+
String name1 = FunctionUtils.getStringValue(args[0], env);
365360
String name2 = FunctionUtils.getStringValue(args[1], env);
366-
Sequence name1List = null;
367-
String name1 = null;
368-
if (name1Obj instanceof java.util.List) {
369-
name1List = RuntimeUtils.seq(name1Obj,env);
370-
} else{
371-
name1 = (String) name1Obj;
372-
}
373361

374362
String key = "";
375-
for (int i = 0; i < len; i++) {
376-
Object nameObj = FunctionUtils.getJavaObject(args[i], env);
377-
if (nameObj instanceof java.util.List) {
378-
Sequence nameList = RuntimeUtils.seq(name, env);
379-
for (Object obj : nameList) {
380-
key += ";" + obj;
381-
}
382-
} else {
383-
key += ";" + nameObj;
384-
}
363+
for (AviatorObject arg : args) {
364+
String name = FunctionUtils.getStringValue(arg, env);
365+
key += ";" + name;
385366
}
386-
387-
AviatorBoolean value = memorized.get(key);
388-
if (value != null) {
389-
return value;
367+
if (memorized.containsKey(key)) {
368+
return memorized.get(key);
390369
}
391370

371+
AviatorBoolean value;
392372
if (rm == null) {
393373
value = AviatorBoolean.valueOf(name1.equals(name2));
394374
} else if (len == 2) {
395-
if (name1List!=null) {
396-
boolean res = false;
397-
for (Object obj : name1List) {
398-
if (rm.hasLink((String) obj, name2)){
399-
res = true;
400-
break;
401-
}
402-
}
403-
value = AviatorBoolean.valueOf(res);
404-
}
405375
value = AviatorBoolean.valueOf(rm.hasLink(name1, name2));
406376
} else if (len == 3) {
407377
String domain = FunctionUtils.getStringValue(args[2], env);
408378
value = AviatorBoolean.valueOf(rm.hasLink(name1, name2, domain));
409-
} else if (len == 4) {
410-
String p_dom = FunctionUtils.getStringValue(args[3], env);
411-
Object domainObj = FunctionUtils.getJavaObject(args[2], env);
412-
413-
boolean res = false;
414-
if (domainObj instanceof java.util.List){
415-
Sequence domainSeq = RuntimeUtils.seq(domainObj,env);
416-
for (Object r_dom : domainSeq) {
417-
if (r_dom.equals(p_dom) && rm.hasLink(name1, name2, (String) r_dom)){
418-
res = true;
419-
break;
420-
}
421-
}
422-
}
423-
value = AviatorBoolean.valueOf(res);
424379
} else {
425380
value = AviatorBoolean.valueOf(false);
426381
}

src/test/java/org/casbin/jcasbin/main/ModelUnitTest.java

-9
Original file line numberDiff line numberDiff line change
@@ -151,15 +151,6 @@ public void testRBACModelWithDomains() {
151151
testDomainEnforce(e, "bob", "domain2", "data2", "write", true);
152152
}
153153

154-
@Test
155-
public void testRBACModelWithMultiDomains() {
156-
Enforcer e = new Enforcer("examples/rbac_with_multiple_domains_model.conf", "examples/rbac_with_multiple_domains_policy.csv");
157-
158-
testDomainEnforce(e, "alice", "data1", "edit", Arrays.asList("dom1","dom2","dom3"),false);
159-
testDomainEnforce(e, "alice", "data1", "read", Arrays.asList("dom1","dom2","dom3"),true);
160-
testDomainEnforce(e, "alice", "data1", "write", Arrays.asList("dom1","dom2","dom3"),true);
161-
}
162-
163154
@Test
164155
public void testRBACModelWithDomainsAtRuntime() {
165156
Enforcer e = new Enforcer("examples/rbac_with_domains_model.conf");

src/test/java/org/casbin/jcasbin/main/RbacAPIUnitTest.java

+11
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.casbin.jcasbin.util.Util;
1818
import org.junit.Test;
1919

20+
import java.util.Arrays;
2021
import java.util.Comparator;
2122
import java.util.List;
2223

@@ -86,6 +87,16 @@ public void testRoleAPI() {
8687
testEnforce(e, "bob", "data2", "write", true);
8788
}
8889

90+
@Test
91+
public void testGFunctionCache() {
92+
Enforcer e = new Enforcer("examples/rbac_model.conf", "examples/rbac_policy.csv");
93+
94+
testEnforce(e, "alice", "data2", "read", true);
95+
e.removeGroupingPolicy(Arrays.asList("alice","data2_admin"));
96+
// Ensure that the gFunction cache is different for each enforce
97+
testEnforce(e, "alice", "data2", "read", false);
98+
}
99+
89100
@Test
90101
public void testPermissionAPI() {
91102
Enforcer e = new Enforcer("examples/basic_without_resources_model.conf", "examples/basic_without_resources_policy.csv");

0 commit comments

Comments
 (0)