Skip to content

Commit 783030c

Browse files
authored
fix: make ecforceEx return a result which include `is (casbin#308)
request allow` and `explain` Signed-off-by: imp2002 <[email protected]> Signed-off-by: imp2002 <[email protected]>
1 parent c61f3e4 commit 783030c

File tree

6 files changed

+110
-50
lines changed

6 files changed

+110
-50
lines changed

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

+11-13
Original file line numberDiff line numberDiff line change
@@ -434,14 +434,13 @@ public void buildRoleLinks() {
434434
* input parameters are usually: (matcher, explain, sub, obj, act), use model matcher by default when matcher is "" or null.
435435
*
436436
* @param matcher the custom matcher.
437-
* @param explain to explain enforcement by informing matched rules
438437
* @param rvals the request needs to be mediated, usually an array
439438
* of strings, can be class instances if ABAC is used.
440439
* @return whether to allow the request.
441440
*/
442-
private boolean enforce(String matcher, List<String> explain, Object... rvals) {
441+
private EnforceResult enforce(String matcher, Object... rvals) {
443442
if (!enabled) {
444-
return true;
443+
return new EnforceResult(true, new ArrayList<>(Collections.singletonList("The enforcer is not enable, allow all request")));
445444
}
446445

447446
boolean compileCached = true;
@@ -608,12 +607,13 @@ private boolean enforce(String matcher, List<String> explain, Object... rvals) {
608607
result = eft.mergeEffects(model.model.get("e").get(eType).value, policyEffects, matcherResults);
609608
}
610609

611-
if (explain != null && explainIndex != -1) {
610+
List<String> explain = new ArrayList<>();
611+
if (explainIndex != -1) {
612612
explain.addAll(model.model.get("p").get(pType).policy.get(explainIndex));
613613
}
614614

615615
Util.logEnforce(rvals, result, explain);
616-
return result;
616+
return new EnforceResult(result, explain);
617617
}
618618

619619
/**
@@ -625,7 +625,7 @@ private boolean enforce(String matcher, List<String> explain, Object... rvals) {
625625
* @return whether to allow the request.
626626
*/
627627
public boolean enforce(Object... rvals) {
628-
return enforce(null, null, rvals);
628+
return enforce(null, rvals).isAllow();
629629
}
630630

631631
/**
@@ -638,7 +638,7 @@ public boolean enforce(Object... rvals) {
638638
* @return whether to allow the request.
639639
*/
640640
public boolean enforceWithMatcher(String matcher, Object... rvals) {
641-
return enforce(matcher, null, rvals);
641+
return enforce(matcher, rvals).isAllow();
642642
}
643643

644644
/**
@@ -650,9 +650,8 @@ public boolean enforceWithMatcher(String matcher, Object... rvals) {
650650
* of strings, can be class instances if ABAC is used.
651651
* @return whether to allow the request.
652652
*/
653-
public boolean enforceEx(Object... rvals) {
654-
List<String> explain = new ArrayList<>();
655-
return enforce("", explain, rvals);
653+
public EnforceResult enforceEx(Object... rvals) {
654+
return enforce(null, rvals);
656655
}
657656

658657
/**
@@ -665,9 +664,8 @@ public boolean enforceEx(Object... rvals) {
665664
* of strings, can be class instances if ABAC is used.
666665
* @return whether to allow the request.
667666
*/
668-
public boolean enforceExWithMatcher(String matcher, Object... rvals) {
669-
List<String> explain = new ArrayList<>();
670-
return enforce(matcher, explain, rvals);
667+
public EnforceResult enforceExWithMatcher(String matcher, Object... rvals) {
668+
return enforce(matcher, rvals);
671669
}
672670

673671
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2022 The casbin Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package org.casbin.jcasbin.main;
16+
17+
import java.util.List;
18+
19+
public class EnforceResult {
20+
private boolean allow;
21+
private List<String> explain;
22+
23+
public boolean isAllow() {
24+
return allow;
25+
}
26+
27+
public void setAllow(boolean allow) {
28+
this.allow = allow;
29+
}
30+
31+
public List<String> getExplain() {
32+
return explain;
33+
}
34+
35+
public void setExplain(List<String> explain) {
36+
this.explain = explain;
37+
}
38+
39+
public EnforceResult() {
40+
}
41+
42+
public EnforceResult(boolean allow, List<String> explain) {
43+
this.allow = allow;
44+
this.explain = explain;
45+
}
46+
47+
@Override
48+
public String toString() {
49+
return "EnforceResult{" +
50+
"allow=" + allow +
51+
", explain=" + explain +
52+
'}';
53+
}
54+
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ public boolean enforceWithMatcher(String matcher, Object... rvals) {
190190
* @return whether to allow the request.
191191
*/
192192
@Override
193-
public boolean enforceEx(Object... rvals) {
193+
public EnforceResult enforceEx(Object... rvals) {
194194
return runSynchronized(() -> super.enforceEx(rvals), READ_WRITE_LOCK.readLock());
195195
}
196196

@@ -205,7 +205,7 @@ public boolean enforceEx(Object... rvals) {
205205
* @return whether to allow the request.
206206
*/
207207
@Override
208-
public boolean enforceExWithMatcher(String matcher, Object... rvals) {
208+
public EnforceResult enforceExWithMatcher(String matcher, Object... rvals) {
209209
return runSynchronized(() -> super.enforceExWithMatcher(matcher, rvals), READ_WRITE_LOCK.readLock());
210210
}
211211

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

+15-15
Original file line numberDiff line numberDiff line change
@@ -312,28 +312,28 @@ public void testEnforceExLog() {
312312

313313
// the previous matcher is
314314
// m = r.sub == p.sub && r.obj == p.obj && r.act == p.act
315-
testEnforceEx(e, "alice", "data1", "read", true);
316-
testEnforceEx(e, "bob", "data2", "write", true);
317-
testEnforceEx(e, "root", "data2", "read", false);
318-
testEnforceEx(e, "root", "data3", "read", false);
319-
testEnforceEx(e, "jack", "data3", "read", false);
315+
testEnforceEx(e, "alice", "data1", "read", true, new String[]{"alice", "data1", "read"});
316+
testEnforceEx(e, "bob", "data2", "write", true, new String[]{"bob", "data2", "write"});
317+
testEnforceEx(e, "root", "data2", "read", false, new String[]{});
318+
testEnforceEx(e, "root", "data3", "read", false, new String[]{});
319+
testEnforceEx(e, "jack", "data3", "read", false, new String[]{});
320320

321321
// custom matcher
322322
String matcher = "m = r.sub == 'root' || r.sub == p.sub && r.obj == p.obj && r.act == p.act";
323-
TestUtil.testEnforceExWithMatcher(e, matcher, "alice", "data1", "read", true);
324-
TestUtil.testEnforceExWithMatcher(e, matcher, "bob", "data2", "write", true);
325-
TestUtil.testEnforceExWithMatcher(e, matcher, "root", "data2", "read", true);
326-
TestUtil.testEnforceExWithMatcher(e, matcher, "root", "data3", "read", true);
327-
TestUtil.testEnforceExWithMatcher(e, matcher, "jack", "data3", "read", false);
323+
TestUtil.testEnforceExWithMatcher(e, matcher, "alice", "data1", "read", true, new String[]{"alice", "data1", "read"});
324+
TestUtil.testEnforceExWithMatcher(e, matcher, "bob", "data2", "write", true, new String[]{"bob", "data2", "write"});
325+
TestUtil.testEnforceExWithMatcher(e, matcher, "root", "data2", "read", true, new String[]{});
326+
TestUtil.testEnforceExWithMatcher(e, matcher, "root", "data3", "read", true, new String[]{});
327+
TestUtil.testEnforceExWithMatcher(e, matcher, "jack", "data3", "read", false, new String[]{});
328328

329329
// the previous matcher is
330330
// m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act
331331
e = new Enforcer("examples/rbac_model.conf", "examples/rbac_policy.csv", true);
332-
testEnforceEx(e, "alice", "data1", "read", true);
333-
testEnforceEx(e, "alice", "data2", "read", true);
334-
testEnforceEx(e, "alice", "data2", "write", true);
335-
testEnforceEx(e, "bob", "data1", "write", false);
336-
testEnforceEx(e, "bob", "data2", "write", true);
332+
testEnforceEx(e, "alice", "data1", "read", true, new String[]{"alice", "data1", "read"});
333+
testEnforceEx(e, "alice", "data2", "read", true, new String[]{"data2_admin", "data2", "read"});
334+
testEnforceEx(e, "alice", "data2", "write", true, new String[]{"data2_admin", "data2", "write"});
335+
testEnforceEx(e, "bob", "data1", "write", false, new String[]{});
336+
testEnforceEx(e, "bob", "data2", "write", true, new String[]{"bob", "data2", "write"});
337337
}
338338

339339
@Test

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

+16-16
Original file line numberDiff line numberDiff line change
@@ -264,28 +264,28 @@ public void testEnforceExLog() {
264264

265265
// the previous matcher is
266266
// m = r.sub == p.sub && r.obj == p.obj && r.act == p.act
267-
testEnforceEx(e, "alice", "data1", "read", true);
268-
testEnforceEx(e, "bob", "data2", "write", true);
269-
testEnforceEx(e, "root", "data2", "read", false);
270-
testEnforceEx(e, "root", "data3", "read", false);
271-
testEnforceEx(e, "jack", "data3", "read", false);
267+
testEnforceEx(e, "alice", "data1", "read", true, new String[]{"alice", "data1", "read"});
268+
testEnforceEx(e, "bob", "data2", "write", true, new String[]{"bob", "data2", "write"});
269+
testEnforceEx(e, "root", "data2", "read", false, new String[]{});
270+
testEnforceEx(e, "root", "data3", "read", false, new String[]{});
271+
testEnforceEx(e, "jack", "data3", "read", false, new String[]{});
272272

273273
// custom matcher
274274
String matcher = "m = r.sub == 'root' || r.sub == p.sub && r.obj == p.obj && r.act == p.act";
275-
TestUtil.testEnforceExWithMatcher(e, matcher, "alice", "data1", "read", true);
276-
TestUtil.testEnforceExWithMatcher(e, matcher, "bob", "data2", "write", true);
277-
TestUtil.testEnforceExWithMatcher(e, matcher, "root", "data2", "read", true);
278-
TestUtil.testEnforceExWithMatcher(e, matcher, "root", "data3", "read", true);
279-
TestUtil.testEnforceExWithMatcher(e, matcher, "jack", "data3", "read", false);
275+
TestUtil.testEnforceExWithMatcher(e, matcher, "alice", "data1", "read", true, new String[]{"alice", "data1", "read"});
276+
TestUtil.testEnforceExWithMatcher(e, matcher, "bob", "data2", "write", true, new String[]{"bob", "data2", "write"});
277+
TestUtil.testEnforceExWithMatcher(e, matcher, "root", "data2", "read", true, new String[]{});
278+
TestUtil.testEnforceExWithMatcher(e, matcher, "root", "data3", "read", true, new String[]{});
279+
TestUtil.testEnforceExWithMatcher(e, matcher, "jack", "data3", "read", false, new String[]{});
280280

281281
// the previous matcher is
282282
// m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act
283-
e = new Enforcer("examples/rbac_model.conf", "examples/rbac_policy.csv", true);
284-
testEnforceEx(e, "alice", "data1", "read", true);
285-
testEnforceEx(e, "alice", "data2", "read", true);
286-
testEnforceEx(e, "alice", "data2", "write", true);
287-
testEnforceEx(e, "bob", "data1", "write", false);
288-
testEnforceEx(e, "bob", "data2", "write", true);
283+
e = new SyncedEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv", true);
284+
testEnforceEx(e, "alice", "data1", "read", true, new String[]{"alice", "data1", "read"});
285+
testEnforceEx(e, "alice", "data2", "read", true, new String[]{"data2_admin", "data2", "read"});
286+
testEnforceEx(e, "alice", "data2", "write", true, new String[]{"data2_admin", "data2", "write"});
287+
testEnforceEx(e, "bob", "data1", "write", false, new String[]{});
288+
testEnforceEx(e, "bob", "data2", "write", true, new String[]{"bob", "data2", "write"});
289289
}
290290

291291
@Test

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

+12-4
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,20 @@ static void testEnforceWithMatcher(Enforcer e, String matcher, Object sub, Objec
3535
assertEquals(res, e.enforceWithMatcher(matcher, sub, obj, act));
3636
}
3737

38-
static void testEnforceEx(Enforcer e, Object sub, Object obj, String act, boolean res) {
39-
assertEquals(res, e.enforceEx(sub, obj, act));
38+
static void testEnforceEx(Enforcer e, Object sub, Object obj, String act, boolean res, String[] explain) {
39+
EnforceResult enforceResult = e.enforceEx(sub, obj, act);
40+
assertEquals(res, enforceResult.isAllow());
41+
for (int i = 0; i < explain.length; i++) {
42+
assertEquals(explain[i], enforceResult.getExplain().get(i));
43+
}
4044
}
4145

42-
static void testEnforceExWithMatcher(Enforcer e, String matcher, Object sub, Object obj, String act, boolean res) {
43-
assertEquals(res, e.enforceExWithMatcher(matcher, sub, obj, act));
46+
static void testEnforceExWithMatcher(Enforcer e, String matcher, Object sub, Object obj, String act, boolean res, String[] explain) {
47+
EnforceResult enforceResult = e.enforceExWithMatcher(matcher, sub, obj, act);
48+
assertEquals(res, enforceResult.isAllow());
49+
for (int i = 0; i < explain.length; i++) {
50+
assertEquals(explain[i], enforceResult.getExplain().get(i));
51+
}
4452
}
4553

4654
static void testEnforceWithoutUsers(Enforcer e, String obj, String act, boolean res) {

0 commit comments

Comments
 (0)