Skip to content

Commit

Permalink
feat: upgrade jcasbin version to 1.21.0 and fix the bugs about filter…
Browse files Browse the repository at this point in the history
… and redis-watcher (#58)

Signed-off-by: Yixiang Zhao <[email protected]>
  • Loading branch information
seriouszyx authored Jan 4, 2022
1 parent 207d741 commit 5564a62
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/casbin/adapter/JdbcAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/casbin/watcher/RedisWatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.UUID;
import java.util.function.Consumer;

/**
* @author fangzhengjin
Expand All @@ -18,6 +19,7 @@
public class RedisWatcher implements Watcher {

private Runnable updateCallback;
private Consumer<String> consumer;
private final String policyTopic;
private final StringRedisTemplate stringRedisTemplate;
private final static String REDIS_WATCHER_UUID = UUID.randomUUID().toString();
Expand All @@ -34,6 +36,11 @@ public void setUpdateCallback(Runnable runnable) {
updateCallback = runnable;
}

@Override
public void setUpdateCallback(Consumer<String> func) {
this.consumer = func;
}

@Override
public void update() {
stringRedisTemplate.convertAndSend(
Expand All @@ -51,6 +58,9 @@ public void updatePolicy(String message) {
}

updateCallback.run();
if (consumer != null) {
consumer.accept(message);
}
logger.info("Casbin policy updated.");
}
}

0 comments on commit 5564a62

Please sign in to comment.