Skip to content

Commit

Permalink
fix: add enforce command (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
liewstar authored Sep 22, 2024
1 parent eee4fe9 commit 49b01e1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ mvn clean install
- Check whether Alice has read permission on data1

```shell
./casbin -m "examples/rbac_model.conf" -p "examples/rbac_policy.csv" -e "alice, data1, read"
./casbin enforce -m "examples/rbac_model.conf" -p "examples/rbac_policy.csv" "alice" "data1" "read"
```
> Allow
> Allowed
```shell
./casbin -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"
./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"
```
> Allow
> Allowed

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

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/casbin/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class Client {
private static final String RBAC_WITH_DOMAINS_COMMAND = "rbac_with_domains";
private static final String ROLEMANAGER_COMMAND = "role_manager";
private static final String MANAGEMENT_COMMAND = "management";
private static final String ENFORCE_COMMAND = "enforce";

private static final Map<String, AbstractCommand> COMMANDS = new HashMap<>();

Expand All @@ -29,6 +30,7 @@ public class Client {
COMMANDS.put(RBAC_WITH_DOMAINS_COMMAND, new RBACWithDomainsCommand());
COMMANDS.put(ROLEMANAGER_COMMAND, new RoleManagerCommand());
COMMANDS.put(MANAGEMENT_COMMAND, new ManagementCommand());
COMMANDS.put(ENFORCE_COMMAND, new EnforceCommand());
}

public static String run(String... args) {
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/org/casbin/command/EnforceCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.casbin.command;

import org.casbin.NewEnforcer;

public class EnforceCommand extends AbstractCommand {
@Override
public String run(NewEnforcer enforcer, String... args) throws Exception {
String subject = args[0];
String object = args[1];
String action = args[2];
boolean res = enforcer.enforce(subject, object, action);
System.out.println(res ? "Allowed" : "Denied");
return String.valueOf(res);
}
}
5 changes: 5 additions & 0 deletions src/test/java/org/casbin/ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,9 @@ public void testCustomFunction() throws ParseException {

}

@Test
public void testEnforce() {
assertEquals(Client.run(new String[]{"enforce","-m","examples/rbac_model.conf","-p","examples/rbac_policy.csv", "alice", "data1", "read"}), "true");
}

}

0 comments on commit 49b01e1

Please sign in to comment.