diff --git a/README.md b/README.md index bf6ac07..08d93d2 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/pom.xml b/pom.xml index 2789e22..499b422 100644 --- a/pom.xml +++ b/pom.xml @@ -45,7 +45,7 @@ UTF-8 1.7.25 3.5.2 - 1.7.4 + 1.31.2 1.2.76 3.2.2 diff --git a/src/main/java/org/casbin/adapter/RedisAdapter.java b/src/main/java/org/casbin/adapter/RedisAdapter.java index ee4358d..5c2b0b9 100644 --- a/src/main/java/org/casbin/adapter/RedisAdapter.java +++ b/src/main/java/org/casbin/adapter/RedisAdapter.java @@ -103,8 +103,27 @@ public void removePolicy(String sec, String ptype, List rule) { @Override public void removeFilteredPolicy(String sec, String ptype, int fieldIndex, String... fieldValues) { List 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 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); + } + }); } /** diff --git a/src/test/java/org/casbin/adapter/RedisAdapterTest.java b/src/test/java/org/casbin/adapter/RedisAdapterTest.java index a8494b9..e7a3f29 100644 --- a/src/test/java/org/casbin/adapter/RedisAdapterTest.java +++ b/src/test/java/org/casbin/adapter/RedisAdapterTest.java @@ -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");