Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: sync keyMatch5 from Go to Java #449

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading