Skip to content

Commit d16773c

Browse files
committed
feat: sync keyMatch5 from Go to Java
1 parent bf39736 commit d16773c

File tree

4 files changed

+55
-5
lines changed

4 files changed

+55
-5
lines changed

examples/keymatch5_model.conf

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

examples/keymatch5_policy.csv

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
p, alice, /alice_data/{resource}/.*, GET
2+
p, alice, /alice_data2/{id}/using/{resId}/.*, GET

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

+10-5
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public class BuiltInFunctions {
3737

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

4142
/**
4243
* validate the variadic string parameter size
@@ -148,8 +149,8 @@ public static boolean keyMatch4(String key1, String key2) {
148149
tokens.add(group);
149150
if(group.contains("/")) {
150151
group = group.replace("{", "\\{")
151-
.replace("}", "\\}")
152-
.replace("/", "\\/");
152+
.replace("}", "\\}")
153+
.replace("/", "\\/");
153154
m.appendReplacement(sb, Matcher.quoteReplacement(group));
154155
} else {
155156
m.appendReplacement(sb, "([^/]+)");
@@ -206,10 +207,14 @@ public static boolean keyMatch4(String key1, String key2) {
206207
*/
207208
public static boolean keyMatch5(String key1, String key2) {
208209
int i = key1.indexOf('?');
209-
if (i == -1) {
210-
return key1.equals(key2);
210+
if (i != -1) {
211+
key1 = key1.substring(0,i);
211212
}
212-
return key1.substring(0, i).equals(key2);
213+
214+
key2 = key2.replace("/*", "/.*");
215+
key2 = KEY_MATCH5_PATTERN.matcher(key2).replaceAll("[^/]+");
216+
217+
return regexMatch(key1, "^" + key2 + "$");
213218
}
214219

215220
/**

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

+32
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,43 @@ public void testKeyMatch4Func() {
127127

128128
@Test
129129
public void testKeyMatch5Func() {
130+
testKeyMatch5("/alice_data/hello/123", "/alice_data/{resource}/.*", true);
131+
130132
testKeyMatch5("/parent/child?status=1&type=2", "/parent/child", true);
131133
testKeyMatch5("/parent?status=1&type=2", "/parent/child", false);
134+
132135
testKeyMatch5("/parent/child/?status=1&type=2", "/parent/child/", true);
133136
testKeyMatch5("/parent/child/?status=1&type=2", "/parent/child", false);
134137
testKeyMatch5("/parent/child?status=1&type=2", "/parent/child/", false);
138+
139+
testKeyMatch5("/foo", "/foo", true);
140+
testKeyMatch5("/foo", "/foo*", true);
141+
testKeyMatch5("/foo", "/foo/*", false);
142+
testKeyMatch5("/foo/bar", "/foo", false);
143+
testKeyMatch5("/foo/bar", "/foo*", false);
144+
testKeyMatch5("/foo/bar", "/foo/*", true);
145+
testKeyMatch5("/foobar", "/foo", false);
146+
testKeyMatch5("/foobar", "/foo*", false);
147+
testKeyMatch5("/foobar", "/foo/*", false);
148+
149+
testKeyMatch5("/", "/{resource}", false);
150+
testKeyMatch5("/resource1", "/{resource}", true);
151+
testKeyMatch5("/myid", "/{id}/using/{resId}", false);
152+
testKeyMatch5("/myid/using/myresid", "/{id}/using/{resId}", true);
153+
154+
testKeyMatch5("/proxy/myid", "/proxy/{id}/*", false);
155+
testKeyMatch5("/proxy/myid/", "/proxy/{id}/*", true);
156+
testKeyMatch5("/proxy/myid/res", "/proxy/{id}/*", true);
157+
testKeyMatch5("/proxy/myid/res/res2", "/proxy/{id}/*", true);
158+
testKeyMatch5("/proxy/myid/res/res2/res3", "/proxy/{id}/*", true);
159+
testKeyMatch5("/proxy/", "/proxy/{id}/*", false);
160+
161+
testKeyMatch5("/proxy/myid?status=1&type=2", "/proxy/{id}/*", false);
162+
testKeyMatch5("/proxy/myid/", "/proxy/{id}/*", true);
163+
testKeyMatch5("/proxy/myid/res?status=1&type=2", "/proxy/{id}/*", true);
164+
testKeyMatch5("/proxy/myid/res/res2?status=1&type=2", "/proxy/{id}/*", true);
165+
testKeyMatch5("/proxy/myid/res/res2/res3?status=1&type=2", "/proxy/{id}/*", true);
166+
testKeyMatch5("/proxy/", "/proxy/{id}/*", false);
135167
}
136168

137169
@Test

0 commit comments

Comments
 (0)