Skip to content

Commit 34669b4

Browse files
committed
feat: modify the request part to one string
1 parent 49231e0 commit 34669b4

File tree

3 files changed

+148
-129
lines changed

3 files changed

+148
-129
lines changed

README.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,47 +18,48 @@ mvn clean install
1818
```
1919

2020
## Options
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-
| `-AF, --addFuntion` | Add custom funtion | n | Please wrap it with `""` and separate each line with `\|` |
28-
| `-ap, --addPolicy` | Add a policy rule to the policy file | n | Please wrap it with `""` |
29-
| `-rp, --removePolicy` | Remove a policy rule from the policy file | n | Please wrap it with `""` |
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+
| `-r, --request` | The parameters of corresponding request | y | Please wrap it with `""` and separate each parameter with `\n`,which corresponds to the line break of the terminal |
26+
| `-e, --enforce` | Check permissions | n | Please wrap it with `""` |
27+
| `-ex, --enforceEx` | Check permissions and get which policy it is | n | Please wrap it with `""` |
28+
| `-AF, --addFuntion` | Add custom funtion | n | Please wrap it with `""` and separate each line with `\|` |
29+
| `-ap, --addPolicy` | Add a policy rule to the policy file | n | Please wrap it with `""` |
30+
| `-rp, --removePolicy` | Remove a policy rule from the policy file | n | Please wrap it with `""` |
3031

3132
## Get started
3233

3334
- Check whether Alice has read permission on data1
3435

3536
```shell
36-
./casbin enforce -m "examples/rbac_model.conf" -p "examples/rbac_policy.csv" "alice" "data1" "read"
37+
./casbin enforce -m "examples/rbac_model.conf" -p "examples/rbac_policy.csv" -r "alice, data1, read"
3738
```
3839
> {"allow":true,"explain":null}
3940
```shell
40-
./casbin enforce -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" "alice" "data1" "read"
41+
./casbin enforce -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" -r "alice, data1, read"
4142
```
4243
> {"allow":true,"explain":null}
4344

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

4647
```shell
47-
./casbin enforceEx -m "examples/rbac_model.conf" -p "examples/rbac_policy.csv" "alice" "data2" "write"
48+
./casbin enforceEx -m "examples/rbac_model.conf" -p "examples/rbac_policy.csv" -r "alice, data2, write"
4849
```
4950
> {"allow":true,"explain":["data2_admin","data2","write"]}
5051

5152
- Add a policy to the policy file
5253

5354
```shell
54-
./casbin addPolicy -m "examples/rbac_model.conf" -p "examples/rbac_policy.csv" "alice" "data2" "write"
55+
./casbin addPolicy -m "examples/rbac_model.conf" -p "examples/rbac_policy.csv" -r "alice, data2, write"
5556
```
5657
> {"allow":true,"explain":null}
5758

5859
- Delete a policy from the policy file
5960

6061
```shell
61-
./casbin removePolicy -m "examples/rbac_model.conf" -p "examples/rbac_policy.csv" "alice" "data2" "write"
62+
./casbin removePolicy -m "examples/rbac_model.conf" -p "examples/rbac_policy.csv" -r "alice, data2, write"
6263
```
6364
> {"allow":true,"explain":null}
6465

src/main/java/org/casbin/Client.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,21 @@ public static String run(String... args) {
3737
CustomFunction customFunction = DynamicClassGenerator.generateClass(methodName, codes);
3838
enforcer.addFunction(methodName, customFunction);
3939
}
40-
CommandExecutor commandExecutor = new CommandExecutor(enforcer, commandName, cmd.getArgs());
40+
41+
String[] requestParts = new String[0];
42+
if (cmd.hasOption("r")) {
43+
String requestStr = cmd.getOptionValue("r");
44+
if (requestStr.contains("\n")) {
45+
requestParts = requestStr.split("\n");
46+
} else {
47+
requestParts = requestStr.split(",");
48+
}
49+
for (int i = 0; i < requestParts.length; i++) {
50+
requestParts[i] = requestParts[i].trim();
51+
}
52+
}
53+
54+
CommandExecutor commandExecutor = new CommandExecutor(enforcer, commandName, requestParts);
4155
Object o = commandExecutor.outputResult();
4256
System.out.println(o);
4357
return o.toString();
@@ -79,6 +93,10 @@ private static CommandLine getCmd(String[] args) throws ParseException {
7993
option.hasArg();
8094
options.addOption(option);
8195

96+
option = new Option("r", "request", true, "the request");
97+
option.hasArg();
98+
options.addOption(option);
99+
82100
CommandLineParser parser = new DefaultParser();
83101
return parser.parse(options, args);
84102
}

0 commit comments

Comments
 (0)