Skip to content

Commit

Permalink
feat: support removeFilteredPolicy() (#17)
Browse files Browse the repository at this point in the history
* feat: support `removeFilteredPolicy()`

* chore: fix README
  • Loading branch information
imp2002 authored Nov 17, 2022
1 parent f310041 commit b487cc0
Showing 4 changed files with 79 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -3,8 +3,8 @@
[![build](https://github.com/jcasbin/redis-adapter/actions/workflows/maven-ci.yml/badge.svg)](https://github.com/jcasbin/redis-adapter/actions)
[![codebeat badge](https://codebeat.co/badges/560a67fc-53b6-4a10-8e1b-989f3bb4e5cb)](https://codebeat.co/projects/github-com-jcasbin-redis-adapter-master)
[![codecov](https://codecov.io/gh/jcasbin/redis-adapter/branch/master/graph/badge.svg?token=5wzDaTC9UV)](https://codecov.io/gh/jcasbin/redis-adapter)
[![Javadocs](https://www.javadoc.io/badge/org.casbin/shiro-casbin.svg)](https://www.javadoc.io/doc/org.casbin/shiro-casbin)
[![Maven Central](https://img.shields.io/maven-central/v/org.casbin/shiro-casbin.svg)](https://mvnrepository.com/artifact/org.casbin/shiro-casbin/latest)
[![Javadocs](https://www.javadoc.io/badge/org.casbin/redis-adapter.svg)](https://www.javadoc.io/doc/org.casbin/redis-adapter)
[![Maven Central](https://img.shields.io/maven-central/v/org.casbin/redis-adapter.svg)](https://mvnrepository.com/artifact/org.casbin/redis-adapter/latest)
[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/casbin/lobby)

Redis Adapter is the [Redis](https://redis.io/) adapter for [jCasbin](https://github.com/casbin/jcasbin). With this library, Casbin can load policy from Redis or save policy to it.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
@@ -45,7 +45,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<slf4j.version>1.7.25</slf4j.version>
<jedis.version>3.5.2</jedis.version>
<jcasbin.version>1.7.4</jcasbin.version>
<jcasbin.version>1.31.2</jcasbin.version>
<json.version>1.2.76</json.version>
<commons.version>3.2.2</commons.version>

21 changes: 20 additions & 1 deletion src/main/java/org/casbin/adapter/RedisAdapter.java
Original file line number Diff line number Diff line change
@@ -103,8 +103,27 @@ public void removePolicy(String sec, String ptype, List<String> rule) {
@Override
public void removeFilteredPolicy(String sec, String ptype, int fieldIndex, String... fieldValues) {
List<String> values = Optional.of(Arrays.asList(fieldValues)).orElse(new ArrayList<>());
throw new RuntimeException("not implement");
if (CollectionUtils.isEmpty(values)) {
return;
}

String regexRule = "";
for (int i = 0; i < values.size(); ++i) {
regexRule += "v" + fieldIndex + ":" + values.get(i) + (i + 1 == values.size() ? "" : ",");
fieldIndex++;
}
List<String> rulesMatch = jedis.lrange(this.key, 0, -1);
jedis.ltrim(this.key, 1, 0);

String finalRegexRule = ".*" + regexRule + ".*";
rulesMatch.forEach(rule -> {
// "{}" is regex symbol in rule lead to regex throw exception, so remove the char

String tempRule = rule.replaceAll("[\\{ | \\} | \"]", "");
if (!tempRule.matches(finalRegexRule)) {
jedis.rpush(this.key, rule);
}
});
}

/**
56 changes: 56 additions & 0 deletions src/test/java/org/casbin/adapter/RedisAdapterTest.java
Original file line number Diff line number Diff line change
@@ -99,6 +99,62 @@ public void testAdapter() {
redisAdapter.close();
}

@Test
public void testRemoveFilteredPolicy() {
redisAdapter = new RedisAdapter(host, port);
Enforcer enforcer = new Enforcer("examples/rbac_model.conf", redisAdapter);

enforcer.clearPolicy();
redisAdapter.savePolicy(enforcer.getModel());

enforcer.addPolicies(Arrays.asList(
Arrays.asList("alice", "data1", "write"),
Arrays.asList("alice", "data1", "read"),
Arrays.asList("alice", "data2", "read"),
Arrays.asList("alice", "data2", "write"),
Arrays.asList("bob", "data1", "write"),
Arrays.asList("bob", "data1", "read"),
Arrays.asList("bob", "data2", "read"),
Arrays.asList("bob", "data2", "write")));

enforcer.removeFilteredPolicy(1, "data1", "read");
enforcer.clearPolicy();
enforcer.loadPolicy();
testGetPolicy(enforcer, Arrays.asList(
Arrays.asList("alice", "data1", "write"),
Arrays.asList("alice", "data2", "read"),
Arrays.asList("alice", "data2", "write"),
Arrays.asList("bob", "data1", "write"),
Arrays.asList("bob", "data2", "read"),
Arrays.asList("bob", "data2", "write")));

enforcer.removeFilteredPolicy(0, "alice");
enforcer.clearPolicy();
redisAdapter.loadPolicy(enforcer.getModel());
testGetPolicy(enforcer, Arrays.asList(
Arrays.asList("bob", "data1", "write"),
Arrays.asList("bob", "data2", "read"),
Arrays.asList("bob", "data2", "write")));

enforcer.removeFilteredPolicy(0, "bob", "data1", "write");
enforcer.clearPolicy();
redisAdapter.loadPolicy(enforcer.getModel());
testGetPolicy(enforcer, Arrays.asList(
Arrays.asList("bob", "data2", "read"),
Arrays.asList("bob", "data2", "write")));

enforcer.removeFilteredPolicy(2, "read");
enforcer.clearPolicy();
redisAdapter.loadPolicy(enforcer.getModel());
testGetPolicy(enforcer, Arrays.asList(
Arrays.asList("bob", "data2", "write")));

enforcer.removeFilteredPolicy(1, "data2");
enforcer.clearPolicy();
redisAdapter.loadPolicy(enforcer.getModel());
testGetPolicy(enforcer, Arrays.asList());
}

@Test
public void testSelectDb() {
Enforcer enforcer = new Enforcer("examples/rbac_model.conf", "examples/rbac_policy.csv");

0 comments on commit b487cc0

Please sign in to comment.