Skip to content

Commit

Permalink
feat: support CLI usage for enforceEx (#406)
Browse files Browse the repository at this point in the history
  • Loading branch information
JackYifan committed Jul 29, 2024
1 parent 6776f75 commit 4abeb4a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/main/java/org/casbin/jcasbin/cli/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,36 @@ public class Client {

public static void main(String[] args) {
try {
boolean res = clientEnforce(args);
Object res = clientEnforce(args);
System.out.println(res);
} catch (ParseException e) {
e.printStackTrace();
}
}

public static boolean clientEnforce(String[] args) throws ParseException {
public static Object clientEnforce(String[] args) throws ParseException {
Options options = new Options();
Option model = new Option("m", "model", true, "the path of the model file");
options.addOption(model);
Option config = new Option("p", "policy", true, "the path of the policy file");
options.addOption(config);
Option enforceCMD = new Option("e", "enforce", true, "enforce");
options.addOption(enforceCMD);
Option enforceExCMD = new Option("ex", "enforceEx", true, "enforceEx");
options.addOption(enforceExCMD);
CommandLineParser parser = new DefaultParser();
CommandLine cmd = parser.parse(options, args);
String modelPath = cmd.getOptionValue("model");
String policyFile = cmd.getOptionValue("policy");
Enforcer e = new Enforcer(modelPath, policyFile);
String enforce = cmd.getOptionValue("enforce");
return e.enforce(enforce.split(","));
if (cmd.hasOption("enforce")) {
String enforceArgs = cmd.getOptionValue("enforce");
return e.enforce(enforceArgs.split(","));
} else if (cmd.hasOption("enforceEx")) {
String enforceExArgs = cmd.getOptionValue("enforceEx");
return e.enforceEx(enforceExArgs.split(","));
} else {
return null;
}
}
}
16 changes: 16 additions & 0 deletions src/test/java/org/casbin/jcasbin/main/ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,20 @@ public void testABAC() throws ParseException {
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);
}

@Test
public void testEnforceEx() throws ParseException {
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"});
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"});
testEnforceExCli((EnforceResult)Client.clientEnforce(new String[]{"-m", "examples/basic_model.conf", "-p", "examples/basic_policy.csv", "-ex", "root,data2,read"}),false, new String[]{});
testEnforceExCli((EnforceResult)Client.clientEnforce(new String[]{"-m", "examples/basic_model.conf", "-p", "examples/basic_policy.csv", "-ex", "root,data3,read"}),false, new String[]{});
testEnforceExCli((EnforceResult)Client.clientEnforce(new String[]{"-m", "examples/basic_model.conf", "-p", "examples/basic_policy.csv", "-ex", "jack,data3,read"}),false, new String[]{});
}

private void testEnforceExCli(EnforceResult enforceResult, boolean res, String[] explain) {
assertEquals(res, enforceResult.isAllow());
for (int i = 0; i < explain.length; i++) {
assertEquals(explain[i], enforceResult.getExplain().get(i));
}
}

}

0 comments on commit 4abeb4a

Please sign in to comment.