diff --git a/pom.xml b/pom.xml
index 82eb2120..7c030fbd 100644
--- a/pom.xml
+++ b/pom.xml
@@ -164,6 +164,27 @@
+
+
+ org.apache.maven.plugins
+ maven-shade-plugin
+ 3.2.4
+
+
+ package
+
+ shade
+
+
+
+
+ org.casbin.jcasbin.cli.Client
+
+
+
+
+
+
@@ -177,7 +198,7 @@
org.slf4j
slf4j-simple
${slf4j.version}
- test
+ compile
junit
@@ -234,6 +255,11 @@
jackson-databind
2.16.1
+
+ commons-cli
+ commons-cli
+ 1.4
+
diff --git a/src/main/java/org/casbin/jcasbin/cli/Client.java b/src/main/java/org/casbin/jcasbin/cli/Client.java
new file mode 100644
index 00000000..40cffc99
--- /dev/null
+++ b/src/main/java/org/casbin/jcasbin/cli/Client.java
@@ -0,0 +1,33 @@
+package org.casbin.jcasbin.cli;
+
+import org.apache.commons.cli.*;
+import org.casbin.jcasbin.main.Enforcer;
+
+public class Client {
+
+ public static void main(String[] args) {
+ try {
+ boolean res = clientEnforce(args);
+ System.out.println(res);
+ } catch (ParseException e) {
+ e.printStackTrace();
+ }
+ }
+
+ public static boolean 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);
+ 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(","));
+ }
+}
diff --git a/src/test/java/org/casbin/jcasbin/main/ClientTest.java b/src/test/java/org/casbin/jcasbin/main/ClientTest.java
new file mode 100644
index 00000000..97328de6
--- /dev/null
+++ b/src/test/java/org/casbin/jcasbin/main/ClientTest.java
@@ -0,0 +1,35 @@
+package org.casbin.jcasbin.main;
+
+import org.apache.commons.cli.ParseException;
+import org.casbin.jcasbin.cli.Client;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class ClientTest {
+
+ @Test
+ public void testRBAC() throws ParseException {
+ assertEquals(Client.clientEnforce(new String[]{"-m","examples/rbac_model.conf","-p","examples/rbac_policy.csv","-e","alice,data1,read"}), true);
+ assertEquals(Client.clientEnforce(new String[]{"-m","examples/rbac_model.conf","-p","examples/rbac_policy.csv","-e","alice,data1,write"}), false);
+ assertEquals(Client.clientEnforce(new String[]{"-m","examples/rbac_model.conf","-p","examples/rbac_policy.csv","-e","alice,data2,read"}), true);
+ assertEquals(Client.clientEnforce(new String[]{"-m","examples/rbac_model.conf","-p","examples/rbac_policy.csv","-e","alice,data2,write"}), true);
+ assertEquals(Client.clientEnforce(new String[]{"-m","examples/rbac_model.conf","-p","examples/rbac_policy.csv","-e","bob,data1,read"}), false);
+ assertEquals(Client.clientEnforce(new String[]{"-m","examples/rbac_model.conf","-p","examples/rbac_policy.csv","-e","bob,data1,write"}), false);
+ assertEquals(Client.clientEnforce(new String[]{"-m","examples/rbac_model.conf","-p","examples/rbac_policy.csv","-e","bob,data2,read"}), false);
+ assertEquals(Client.clientEnforce(new String[]{"-m","examples/rbac_model.conf","-p","examples/rbac_policy.csv","-e","bob,data2,write"}), true);
+ }
+
+ @Test
+ 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","alice,domain1,data1,read"}), true);
+ assertEquals(Client.clientEnforce(new String[]{"-m","examples/abac_rule_with_domains_model.conf","-p","examples/abac_rule_with_domains_policy.csv","-e","alice,domain1,data1,write"}), true);
+ assertEquals(Client.clientEnforce(new String[]{"-m","examples/abac_rule_with_domains_model.conf","-p","examples/abac_rule_with_domains_policy.csv","-e","alice,domain2,data1,read"}), false);
+ assertEquals(Client.clientEnforce(new String[]{"-m","examples/abac_rule_with_domains_model.conf","-p","examples/abac_rule_with_domains_policy.csv","-e","alice,domain2,data1,write"}), false);
+ assertEquals(Client.clientEnforce(new String[]{"-m","examples/abac_rule_with_domains_model.conf","-p","examples/abac_rule_with_domains_policy.csv","-e","bob,domain1,data2,read"}), false);
+ assertEquals(Client.clientEnforce(new String[]{"-m","examples/abac_rule_with_domains_model.conf","-p","examples/abac_rule_with_domains_policy.csv","-e","bob,domain1,data2,write"}), false);
+ 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);
+ 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);
+ }
+
+}