From 5564a6232c82892c961b3c5dd44d8c13509474e3 Mon Sep 17 00:00:00 2001 From: Yixiang Zhao <674965440@qq.com> Date: Tue, 4 Jan 2022 20:09:13 +0800 Subject: [PATCH] feat: upgrade jcasbin version to 1.21.0 and fix the bugs about filter and redis-watcher (#58) Signed-off-by: Yixiang Zhao --- build.gradle | 2 +- src/main/java/org/casbin/adapter/JdbcAdapter.java | 6 +++--- src/main/java/org/casbin/watcher/RedisWatcher.java | 10 ++++++++++ 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/build.gradle b/build.gradle index 8f0a77e..802b36a 100644 --- a/build.gradle +++ b/build.gradle @@ -28,7 +28,7 @@ configurations { } dependencies { - compile 'org.casbin:jcasbin:1.9.2' + compile 'org.casbin:jcasbin:1.21.0' compile 'com.h2database:h2' implementation 'org.springframework.boot:spring-boot-starter-jdbc' diff --git a/src/main/java/org/casbin/adapter/JdbcAdapter.java b/src/main/java/org/casbin/adapter/JdbcAdapter.java index 481e7ed..13d46fe 100644 --- a/src/main/java/org/casbin/adapter/JdbcAdapter.java +++ b/src/main/java/org/casbin/adapter/JdbcAdapter.java @@ -54,7 +54,7 @@ public class JdbcAdapter implements FilteredAdapter { protected JdbcTemplate jdbcTemplate; protected CasbinExceptionProperties casbinExceptionProperties; - private volatile boolean isFiltered = true; + private volatile boolean isFiltered = false; public JdbcAdapter(JdbcTemplate jdbcTemplate, CasbinExceptionProperties casbinExceptionProperties, boolean autoCreateTable) { this(jdbcTemplate,casbinExceptionProperties,DEFAULT_TABLE_NAME,autoCreateTable); @@ -154,7 +154,6 @@ public void loadPolicy(Model model) { policies.keySet().forEach( k -> model.model.get(k.substring(0, 1)).get(k).policy.addAll(policies.get(k)) ); - isFiltered = false; } /** @@ -280,9 +279,11 @@ public void removeFilteredPolicy(String sec, String ptype, int fieldIndex, Strin public void loadFilteredPolicy(Model model, Object filter) throws CasbinAdapterException { if (filter == null) { loadPolicy(model); + isFiltered = false; return; } if (!(filter instanceof Filter)) { + isFiltered = false; throw new CasbinAdapterException("Invalid filter type."); } try { @@ -328,7 +329,6 @@ private void loadFilteredPolicyFromJdbc(Model model, Filter filter) { policies.keySet().forEach( k -> model.model.get(k.substring(0, 1)).get(k).policy.addAll(policies.get(k)) ); - isFiltered = false; } /** diff --git a/src/main/java/org/casbin/watcher/RedisWatcher.java b/src/main/java/org/casbin/watcher/RedisWatcher.java index 7f1aa3d..e59b6cd 100644 --- a/src/main/java/org/casbin/watcher/RedisWatcher.java +++ b/src/main/java/org/casbin/watcher/RedisWatcher.java @@ -6,6 +6,7 @@ import org.springframework.data.redis.core.StringRedisTemplate; import java.util.UUID; +import java.util.function.Consumer; /** * @author fangzhengjin @@ -18,6 +19,7 @@ public class RedisWatcher implements Watcher { private Runnable updateCallback; + private Consumer consumer; private final String policyTopic; private final StringRedisTemplate stringRedisTemplate; private final static String REDIS_WATCHER_UUID = UUID.randomUUID().toString(); @@ -34,6 +36,11 @@ public void setUpdateCallback(Runnable runnable) { updateCallback = runnable; } + @Override + public void setUpdateCallback(Consumer func) { + this.consumer = func; + } + @Override public void update() { stringRedisTemplate.convertAndSend( @@ -51,6 +58,9 @@ public void updatePolicy(String message) { } updateCallback.run(); + if (consumer != null) { + consumer.accept(message); + } logger.info("Casbin policy updated."); } }