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: support CLI usage for enforceEx #406

Merged
merged 1 commit into from
Jul 29, 2024
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
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));
}
}

}
Loading