Skip to content

Commit

Permalink
feat: sync keyMatch5 from Go to Java (#449)
Browse files Browse the repository at this point in the history
  • Loading branch information
sukidayou authored Jan 17, 2025
1 parent bf39736 commit 747234b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
11 changes: 11 additions & 0 deletions examples/keymatch5_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, obj, act

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

[matchers]
m = r.sub == p.sub && keyMatch5(r.obj, p.obj) && regexMatch(r.act, p.act)
2 changes: 2 additions & 0 deletions examples/keymatch5_policy.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
p, alice, /alice_data/{resource}/.*, GET
p, alice, /alice_data2/{id}/using/{resId}/.*, GET
11 changes: 8 additions & 3 deletions src/main/java/org/casbin/jcasbin/util/BuiltInFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class BuiltInFunctions {

private static final Pattern KEY_MATCH2_PATTERN = Pattern.compile(":[^/]+");
private static final Pattern KEY_MATCH3_PATTERN = Pattern.compile("\\{[^/]+\\}");
private static final Pattern KEY_MATCH5_PATTERN = Pattern.compile("\\{[^/]+\\}");

/**
* validate the variadic string parameter size
Expand Down Expand Up @@ -206,10 +207,14 @@ public static boolean keyMatch4(String key1, String key2) {
*/
public static boolean keyMatch5(String key1, String key2) {
int i = key1.indexOf('?');
if (i == -1) {
return key1.equals(key2);
if (i != -1) {
key1 = key1.substring(0,i);
}
return key1.substring(0, i).equals(key2);

key2 = key2.replace("/*", "/.*");
key2 = KEY_MATCH5_PATTERN.matcher(key2).replaceAll("[^/]+");

return regexMatch(key1, "^" + key2 + "$");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,43 @@ public void testKeyMatch4Func() {

@Test
public void testKeyMatch5Func() {
testKeyMatch5("/alice_data/hello/123", "/alice_data/{resource}/.*", true);

testKeyMatch5("/parent/child?status=1&type=2", "/parent/child", true);
testKeyMatch5("/parent?status=1&type=2", "/parent/child", false);

testKeyMatch5("/parent/child/?status=1&type=2", "/parent/child/", true);
testKeyMatch5("/parent/child/?status=1&type=2", "/parent/child", false);
testKeyMatch5("/parent/child?status=1&type=2", "/parent/child/", false);

testKeyMatch5("/foo", "/foo", true);
testKeyMatch5("/foo", "/foo*", true);
testKeyMatch5("/foo", "/foo/*", false);
testKeyMatch5("/foo/bar", "/foo", false);
testKeyMatch5("/foo/bar", "/foo*", false);
testKeyMatch5("/foo/bar", "/foo/*", true);
testKeyMatch5("/foobar", "/foo", false);
testKeyMatch5("/foobar", "/foo*", false);
testKeyMatch5("/foobar", "/foo/*", false);

testKeyMatch5("/", "/{resource}", false);
testKeyMatch5("/resource1", "/{resource}", true);
testKeyMatch5("/myid", "/{id}/using/{resId}", false);
testKeyMatch5("/myid/using/myresid", "/{id}/using/{resId}", true);

testKeyMatch5("/proxy/myid", "/proxy/{id}/*", false);
testKeyMatch5("/proxy/myid/", "/proxy/{id}/*", true);
testKeyMatch5("/proxy/myid/res", "/proxy/{id}/*", true);
testKeyMatch5("/proxy/myid/res/res2", "/proxy/{id}/*", true);
testKeyMatch5("/proxy/myid/res/res2/res3", "/proxy/{id}/*", true);
testKeyMatch5("/proxy/", "/proxy/{id}/*", false);

testKeyMatch5("/proxy/myid?status=1&type=2", "/proxy/{id}/*", false);
testKeyMatch5("/proxy/myid/", "/proxy/{id}/*", true);
testKeyMatch5("/proxy/myid/res?status=1&type=2", "/proxy/{id}/*", true);
testKeyMatch5("/proxy/myid/res/res2?status=1&type=2", "/proxy/{id}/*", true);
testKeyMatch5("/proxy/myid/res/res2/res3?status=1&type=2", "/proxy/{id}/*", true);
testKeyMatch5("/proxy/", "/proxy/{id}/*", false);
}

@Test
Expand Down

0 comments on commit 747234b

Please sign in to comment.