Skip to content

Commit 8d9c1b3

Browse files
authored
feat: support some functions of the editor (#9)
* feat: Support parsing string * feat: simple check model * feat: update README
1 parent 554a825 commit 8d9c1b3

File tree

4 files changed

+164
-48
lines changed

4 files changed

+164
-48
lines changed

README.md

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,46 @@ mvn clean install
1818
```
1919

2020
## Options
21-
| options | description | must |
22-
|-----------------------|----------------------------------------------|------|
23-
| `-m, --model` | The path of the model file | y |
24-
| `-p, --policy` | The path of the policy file | y |
25-
| `-e, --enforce` | Check permissions | n |
26-
| `-ex, --enforceEx` | Check permissions and get which policy it is | n |
27-
| `-ap, --addPolicy` | Add a policy rule to the policy file | n |
28-
| `-rp, --removePolicy` | Remove a policy rule from the policy file | n |
21+
| options | description | must | remark |
22+
|-----------------------|----------------------------------------------|------|-----------------------------------------------------------|
23+
| `-m, --model` | The path of the model file or model text | y | Please wrap it with `""` and separate each line with `\|` |
24+
| `-p, --policy` | The path of the policy file or policy text | y | Please wrap it with `""` and separate each line with `\|` |
25+
| `-e, --enforce` | Check permissions | n | Please wrap it with `""` |
26+
| `-ex, --enforceEx` | Check permissions and get which policy it is | n | Please wrap it with `""` |
27+
| `-ap, --addPolicy` | Add a policy rule to the policy file | n | Please wrap it with `""` |
28+
| `-rp, --removePolicy` | Remove a policy rule from the policy file | n | Please wrap it with `""` |
2929

3030
## Get started
3131

3232
- Check whether Alice has read permission on data1
3333

3434
```shell
35-
java -jar target/casbin-java-cli.jar -m examples/rbac_model.conf -p examples/rbac_policy.csv -e alice,data1,read
35+
java -jar target/casbin-java-cli.jar -m "examples/rbac_model.conf" -p "examples/rbac_policy.csv" -e "alice, data1, read"
3636
```
3737
> Allow
38+
```shell
39+
java -jar target/casbin-java-cli.jar -m "[request_definition]|r = sub, obj, act|[policy_definition]|p = sub, obj, act|[role_definition]|g = _, _|[policy_effect]|e = some(where (p.eft == allow))|[matchers]|m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act" -p "p, alice, data1, read|p, bob, data2, write|p, data2_admin, data2, read|p, data2_admin, data2, write|g, alice, data2_admin" -e "alice, data1, read"
40+
```
41+
> Allow
3842

3943
- Check whether Alice has write permission for data2. If so, display the effective policy.
4044

4145
```shell
42-
java -jar target/casbin-java-cli.jar -m examples/rbac_model.conf -p examples/rbac_policy.csv -ex alice,data2,write
46+
java -jar target/casbin-java-cli.jar -m "examples/rbac_model.conf" -p "examples/rbac_policy.csv" -ex "alice, data2, write"
4347
```
44-
> EnforceResult{allow=true, explain=[data2_admin, data2, write]}
48+
> true Reason: [alice, data2, write]
4549

4650
- Add a policy to the policy file
4751

4852
```shell
49-
java -jar target/casbin-java-cli.jar -m examples/rbac_model.conf -p examples/rbac_policy.csv -ap alice,data2,write
53+
java -jar target/casbin-java-cli.jar -m "examples/rbac_model.conf" -p "examples/rbac_policy.csv" -ap "alice, data2, write"
5054
```
5155
> Add Success
5256

5357
- Delete a policy from the policy file
5458

5559
```shell
56-
java -jar target/casbin-java-cli.jar -m examples/rbac_model.conf -p examples/rbac_policy.csv -rp alice,data1,read
60+
java -jar target/casbin-java-cli.jar -m "examples/rbac_model.conf" -p "examples/rbac_policy.csv" -rp "alice,data1,read"
5761
```
5862
> Remove Success
5963

Lines changed: 50 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package org.casbin;
22

33
import org.apache.commons.cli.*;
4-
import org.casbin.jcasbin.exception.CasbinConfigException;
5-
import org.casbin.jcasbin.main.Enforcer;
4+
import org.casbin.jcasbin.exception.CasbinEffectorException;
5+
import org.casbin.jcasbin.main.EnforceResult;
6+
67

78
public class Client {
89
private static void configureOptions(Options options) {
@@ -29,46 +30,60 @@ public static Object run(String[] args) throws ParseException {
2930
CommandLineParser parser = new DefaultParser();
3031
CommandLine cmd = parser.parse(options, args);
3132

32-
String modelPath = cmd.getOptionValue("model");
33-
String policyPath = cmd.getOptionValue("policy");
34-
Enforcer enforcer = null;
33+
String model = cmd.getOptionValue("model");
34+
String policy = cmd.getOptionValue("policy");
35+
NewEnforcer enforcer = null;
3536
try {
36-
enforcer = new Enforcer(modelPath, policyPath);
37-
} catch (CasbinConfigException ex) {
38-
ex.printStackTrace();
37+
enforcer = new NewEnforcer(model, policy);
38+
} catch (NullPointerException | CasbinEffectorException | UnsupportedOperationException e) {
39+
System.out.println("unsupported effect:" + e.getMessage());
40+
System.exit(0);
41+
} catch (Exception e) {
42+
System.out.println(e.getMessage());
43+
System.exit(0);
3944
}
4045

41-
if(cmd.hasOption("enforce")) {
42-
String enforceArgs = cmd.getOptionValue("enforce").replace(" ","");
43-
boolean result = enforcer.enforce(enforceArgs.split(","));
44-
System.out.println(result ? "Allow" : "Ban");
45-
return result;
46-
} else if (cmd.hasOption("enforceEx")) {
47-
String enforceArgs = cmd.getOptionValue("enforceEx").replace(" ","");
48-
boolean result = enforcer.enforceEx(enforceArgs.split(",")).isAllow();
49-
System.out.println(result ? "Allow" : "Ban");
50-
return result;
51-
}else if (cmd.hasOption("addPolicy")){
52-
String policyArgs = cmd.getOptionValue("addPolicy").replace(" ","");
53-
boolean result = enforcer.addPolicy(policyArgs.split(","));
54-
System.out.println(result ? "Add Success" : "Add Failed");
55-
enforcer.savePolicy();
56-
return result;
57-
}else if (cmd.hasOption("removePolicy")){
58-
String policyArgs = cmd.getOptionValue("removePolicy").replace(" ","");
59-
boolean result = enforcer.removePolicy(policyArgs.split(","));
60-
System.out.println(result ? "Remove Success" : "Remove Failed");
61-
enforcer.savePolicy();
62-
return result;
63-
}else {
64-
System.out.println("Command Error");
65-
return null;
46+
try {
47+
if(cmd.hasOption("enforce")) {
48+
String enforceArgs = cmd.getOptionValue("enforce").replace(" ","");
49+
boolean result = enforcer.enforce(enforceArgs.split(","));
50+
System.out.println(result ? "Allow" : "Ban");
51+
return result;
52+
} else if (cmd.hasOption("enforceEx")) {
53+
String enforceArgs = cmd.getOptionValue("enforceEx").replace(" ","");
54+
EnforceResult enforceResult = enforcer.enforceEx(enforceArgs.split(","));
55+
boolean allow = enforceResult.isAllow();
56+
if(allow) {
57+
System.out.printf("%s Reason: %s", allow, enforceResult.getExplain());
58+
} else {
59+
System.out.println(allow);
60+
}
61+
return allow;
62+
}else if (cmd.hasOption("addPolicy")){
63+
String policyArgs = cmd.getOptionValue("addPolicy").replace(" ","");
64+
boolean result = enforcer.addPolicy(policyArgs.split(","));
65+
System.out.println(result ? "Add Success" : "Add Failed");
66+
enforcer.savePolicy();
67+
return result;
68+
}else if (cmd.hasOption("removePolicy")){
69+
String policyArgs = cmd.getOptionValue("removePolicy").replace(" ","");
70+
boolean result = enforcer.removePolicy(policyArgs.split(","));
71+
System.out.println(result ? "Remove Success" : "Remove Failed");
72+
enforcer.savePolicy();
73+
return result;
74+
}else {
75+
System.out.println("Command Error");
76+
return null;
77+
}
78+
} catch (Exception e) {
79+
System.out.println("unsupported effect:" + e.getMessage());
80+
System.exit(0);
6681
}
82+
return null;
6783
}
6884

6985
public static void main(String[] args) throws ParseException {
7086
Client cli = new Client();
71-
Object run = cli.run(args);
72-
System.out.println(run);
87+
Object run = run(args);
7388
}
7489
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package org.casbin;
2+
3+
import org.casbin.jcasbin.main.Enforcer;
4+
5+
import java.io.BufferedWriter;
6+
import java.io.File;
7+
import java.io.FileWriter;
8+
import java.io.IOException;
9+
import java.nio.charset.StandardCharsets;
10+
import java.nio.file.Files;
11+
import java.nio.file.Paths;
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
import java.util.regex.Matcher;
15+
import java.util.regex.Pattern;
16+
17+
public class NewEnforcer extends Enforcer {
18+
19+
public NewEnforcer(String modelPath, String policyFile) {
20+
super(parse(modelPath, ".conf"), parse(policyFile, ".csv"));
21+
}
22+
23+
public static String parse(String string, String suffix) {
24+
string = string.replace("|","\n");
25+
boolean isFile = string.endsWith(suffix);
26+
if(suffix.equals(".conf")) {
27+
if(isFile) {
28+
try {
29+
simpleCheck(new String(Files.readAllBytes(Paths.get(string)), StandardCharsets.UTF_8));
30+
} catch (IOException e) {
31+
throw new RuntimeException(e);
32+
}
33+
} else {
34+
simpleCheck(string);
35+
}
36+
}
37+
return isFile ? string : writeToTempFile(string, suffix);
38+
}
39+
40+
public static String writeToTempFile(String str, String suffix) {
41+
File tempFile = null;
42+
try {
43+
tempFile = File.createTempFile("default", suffix);
44+
tempFile.deleteOnExit();
45+
try (BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile))) {
46+
writer.write(str);
47+
}
48+
} catch (IOException e) {
49+
e.printStackTrace();
50+
}
51+
return tempFile.getAbsolutePath();
52+
}
53+
54+
private static void simpleCheck(String fileString) {
55+
fileString = fileString.replace(" ","");
56+
String[] requiredSubstrings = {"[request_definition]", "[policy_definition]", "[policy_effect]", "[matchers]", "r=", "p=", "e=", "m="};
57+
List<String> missingSubstrings = new ArrayList<>();
58+
59+
for (String substring : requiredSubstrings) {
60+
Pattern pattern = Pattern.compile(Pattern.quote(substring));
61+
Matcher matcher = pattern.matcher(fileString);
62+
if (!matcher.find()) {
63+
missingSubstrings.add(substring);
64+
}
65+
}
66+
67+
if(!missingSubstrings.isEmpty()) {
68+
throw new RuntimeException("missing required sections: " + String.join(", ", missingSubstrings));
69+
}
70+
}
71+
}

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.junit.Test;
66

77
import static org.junit.Assert.assertEquals;
8+
import static org.junit.Assert.assertTrue;
89

910
public class ClientTest {
1011

@@ -54,4 +55,29 @@ public void testRemovePolicy() throws ParseException {
5455
enforcer.savePolicy();
5556
}
5657

58+
@Test
59+
public void testParseString() {
60+
String model = "[request_definition]\n" +
61+
"r = sub, obj, act\n" +
62+
"\n" +
63+
"[policy_definition]\n" +
64+
"p = sub, obj, act\n" +
65+
"\n" +
66+
"[role_definition]\n" +
67+
"g = _, _\n" +
68+
"\n" +
69+
"[policy_effect]\n" +
70+
"e = some(where (p.eft == allow))\n" +
71+
"\n" +
72+
"[matchers]\n" +
73+
"m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act";
74+
String policy = "p, alice, data1, read\n" +
75+
"p, bob, data2, write\n" +
76+
"p, data2_admin, data2, read\n" +
77+
"p, data2_admin, data2, write\n" +
78+
"g, alice, data2_admin";
79+
NewEnforcer enforce = new NewEnforcer(model, policy);
80+
assertTrue(enforce.enforce("alice", "data1", "read"));
81+
}
82+
5783
}

0 commit comments

Comments
 (0)