Skip to content

Commit 4ab6346

Browse files
authored
feat: support CLI usage for jCasbin (casbin#402)
* feat: support CLI usage for jCasbin * feat: output the result on the console
1 parent 1dab612 commit 4ab6346

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed

pom.xml

+27-1
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,27 @@
164164
<check />
165165
</configuration>
166166
</plugin>
167+
168+
<plugin>
169+
<groupId>org.apache.maven.plugins</groupId>
170+
<artifactId>maven-shade-plugin</artifactId>
171+
<version>3.2.4</version>
172+
<executions>
173+
<execution>
174+
<phase>package</phase>
175+
<goals>
176+
<goal>shade</goal>
177+
</goals>
178+
<configuration>
179+
<transformers>
180+
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
181+
<mainClass>org.casbin.jcasbin.cli.Client</mainClass>
182+
</transformer>
183+
</transformers>
184+
</configuration>
185+
</execution>
186+
</executions>
187+
</plugin>
167188
</plugins>
168189
</build>
169190

@@ -177,7 +198,7 @@
177198
<groupId>org.slf4j</groupId>
178199
<artifactId>slf4j-simple</artifactId>
179200
<version>${slf4j.version}</version>
180-
<scope>test</scope>
201+
<scope>compile</scope>
181202
</dependency>
182203
<dependency>
183204
<groupId>junit</groupId>
@@ -234,6 +255,11 @@
234255
<artifactId>jackson-databind</artifactId>
235256
<version>2.16.1</version>
236257
</dependency>
258+
<dependency>
259+
<groupId>commons-cli</groupId>
260+
<artifactId>commons-cli</artifactId>
261+
<version>1.4</version>
262+
</dependency>
237263

238264
</dependencies>
239265

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.casbin.jcasbin.cli;
2+
3+
import org.apache.commons.cli.*;
4+
import org.casbin.jcasbin.main.Enforcer;
5+
6+
public class Client {
7+
8+
public static void main(String[] args) {
9+
try {
10+
boolean res = clientEnforce(args);
11+
System.out.println(res);
12+
} catch (ParseException e) {
13+
e.printStackTrace();
14+
}
15+
}
16+
17+
public static boolean clientEnforce(String[] args) throws ParseException {
18+
Options options = new Options();
19+
Option model = new Option("m", "model", true, "the path of the model file");
20+
options.addOption(model);
21+
Option config = new Option("p", "policy", true, "the path of the policy file");
22+
options.addOption(config);
23+
Option enforceCMD = new Option("e", "enforce", true, "enforce");
24+
options.addOption(enforceCMD);
25+
CommandLineParser parser = new DefaultParser();
26+
CommandLine cmd = parser.parse(options, args);
27+
String modelPath = cmd.getOptionValue("model");
28+
String policyFile = cmd.getOptionValue("policy");
29+
Enforcer e = new Enforcer(modelPath, policyFile);
30+
String enforce = cmd.getOptionValue("enforce");
31+
return e.enforce(enforce.split(","));
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.casbin.jcasbin.main;
2+
3+
import org.apache.commons.cli.ParseException;
4+
import org.casbin.jcasbin.cli.Client;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class ClientTest {
10+
11+
@Test
12+
public void testRBAC() throws ParseException {
13+
assertEquals(Client.clientEnforce(new String[]{"-m","examples/rbac_model.conf","-p","examples/rbac_policy.csv","-e","alice,data1,read"}), true);
14+
assertEquals(Client.clientEnforce(new String[]{"-m","examples/rbac_model.conf","-p","examples/rbac_policy.csv","-e","alice,data1,write"}), false);
15+
assertEquals(Client.clientEnforce(new String[]{"-m","examples/rbac_model.conf","-p","examples/rbac_policy.csv","-e","alice,data2,read"}), true);
16+
assertEquals(Client.clientEnforce(new String[]{"-m","examples/rbac_model.conf","-p","examples/rbac_policy.csv","-e","alice,data2,write"}), true);
17+
assertEquals(Client.clientEnforce(new String[]{"-m","examples/rbac_model.conf","-p","examples/rbac_policy.csv","-e","bob,data1,read"}), false);
18+
assertEquals(Client.clientEnforce(new String[]{"-m","examples/rbac_model.conf","-p","examples/rbac_policy.csv","-e","bob,data1,write"}), false);
19+
assertEquals(Client.clientEnforce(new String[]{"-m","examples/rbac_model.conf","-p","examples/rbac_policy.csv","-e","bob,data2,read"}), false);
20+
assertEquals(Client.clientEnforce(new String[]{"-m","examples/rbac_model.conf","-p","examples/rbac_policy.csv","-e","bob,data2,write"}), true);
21+
}
22+
23+
@Test
24+
public void testABAC() throws ParseException {
25+
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);
26+
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);
27+
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);
28+
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);
29+
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);
30+
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);
31+
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);
32+
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);
33+
}
34+
35+
}

0 commit comments

Comments
 (0)