Skip to content

Commit 4abeb4a

Browse files
authored
feat: support CLI usage for enforceEx (#406)
1 parent 6776f75 commit 4abeb4a

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/main/java/org/casbin/jcasbin/cli/Client.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,36 @@ public class Client {
77

88
public static void main(String[] args) {
99
try {
10-
boolean res = clientEnforce(args);
10+
Object res = clientEnforce(args);
1111
System.out.println(res);
1212
} catch (ParseException e) {
1313
e.printStackTrace();
1414
}
1515
}
1616

17-
public static boolean clientEnforce(String[] args) throws ParseException {
17+
public static Object clientEnforce(String[] args) throws ParseException {
1818
Options options = new Options();
1919
Option model = new Option("m", "model", true, "the path of the model file");
2020
options.addOption(model);
2121
Option config = new Option("p", "policy", true, "the path of the policy file");
2222
options.addOption(config);
2323
Option enforceCMD = new Option("e", "enforce", true, "enforce");
2424
options.addOption(enforceCMD);
25+
Option enforceExCMD = new Option("ex", "enforceEx", true, "enforceEx");
26+
options.addOption(enforceExCMD);
2527
CommandLineParser parser = new DefaultParser();
2628
CommandLine cmd = parser.parse(options, args);
2729
String modelPath = cmd.getOptionValue("model");
2830
String policyFile = cmd.getOptionValue("policy");
2931
Enforcer e = new Enforcer(modelPath, policyFile);
30-
String enforce = cmd.getOptionValue("enforce");
31-
return e.enforce(enforce.split(","));
32+
if (cmd.hasOption("enforce")) {
33+
String enforceArgs = cmd.getOptionValue("enforce");
34+
return e.enforce(enforceArgs.split(","));
35+
} else if (cmd.hasOption("enforceEx")) {
36+
String enforceExArgs = cmd.getOptionValue("enforceEx");
37+
return e.enforceEx(enforceExArgs.split(","));
38+
} else {
39+
return null;
40+
}
3241
}
3342
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,20 @@ public void testABAC() throws ParseException {
3232
assertEquals(Client.clientEnforce(new String[]{"-m","examples/abac_rule_with_domains_model.conf","-p","examples/abac_rule_with_domains_policy.csv","-e","bob,domain2,data2,read"}), true);
3333
}
3434

35+
@Test
36+
public void testEnforceEx() throws ParseException {
37+
testEnforceExCli((EnforceResult)Client.clientEnforce(new String[]{"-m", "examples/basic_model.conf", "-p", "examples/basic_policy.csv", "-ex", "alice,data1,read"}),true, new String[]{"alice", "data1", "read"});
38+
testEnforceExCli((EnforceResult)Client.clientEnforce(new String[]{"-m", "examples/basic_model.conf", "-p", "examples/basic_policy.csv", "-ex", "bob,data2,write"}),true, new String[]{"bob", "data2", "write"});
39+
testEnforceExCli((EnforceResult)Client.clientEnforce(new String[]{"-m", "examples/basic_model.conf", "-p", "examples/basic_policy.csv", "-ex", "root,data2,read"}),false, new String[]{});
40+
testEnforceExCli((EnforceResult)Client.clientEnforce(new String[]{"-m", "examples/basic_model.conf", "-p", "examples/basic_policy.csv", "-ex", "root,data3,read"}),false, new String[]{});
41+
testEnforceExCli((EnforceResult)Client.clientEnforce(new String[]{"-m", "examples/basic_model.conf", "-p", "examples/basic_policy.csv", "-ex", "jack,data3,read"}),false, new String[]{});
42+
}
43+
44+
private void testEnforceExCli(EnforceResult enforceResult, boolean res, String[] explain) {
45+
assertEquals(res, enforceResult.isAllow());
46+
for (int i = 0; i < explain.length; i++) {
47+
assertEquals(explain[i], enforceResult.getExplain().get(i));
48+
}
49+
}
50+
3551
}

0 commit comments

Comments
 (0)