Skip to content

Commit

Permalink
feat: add DistributedEnforcer auto-configuration via useDistributedEn…
Browse files Browse the repository at this point in the history
…forcer (jcasbin#102)
  • Loading branch information
jay-babu authored Jun 30, 2024
1 parent aef3b87 commit dbfc31c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ casbin:
enableCasbin: true
#Whether to use thread-synchronized Enforcer, default false
useSyncedEnforcer: false
#Whether to use distributed Enforcer, default false.
#If both useSyncedEnforcer and useDistributedEnforcer are set to true, useDistributedEnforcer will take effect.
useDistributedEnforcer: false
#Whether to enable automatic policy saving, if the adapter supports this function, it is enabled by default.
autoSave: true
#Storage type [file, jdbc], currently supported jdbc database [mysql (mariadb), h2, oracle, postgresql, db2]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.casbin.annotation.CasbinDataSource;
import org.casbin.exception.CasbinAdapterException;
import org.casbin.exception.CasbinModelConfigNotFoundException;
import org.casbin.jcasbin.main.DistributedEnforcer;
import org.casbin.jcasbin.main.Enforcer;
import org.casbin.jcasbin.main.SyncedEnforcer;
import org.casbin.jcasbin.model.Model;
Expand Down Expand Up @@ -147,7 +148,10 @@ public Enforcer enforcer(CasbinProperties properties, Adapter adapter) {
model.addDef("m", "m", "g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act");
}
Enforcer enforcer;
if (properties.isUseSyncedEnforcer()) {
if (properties.isUseDistributedEnforcer()) {
enforcer = new DistributedEnforcer(model, adapter);
logger.info("Casbin use DistributedEnforcer");
} else if (properties.isUseSyncedEnforcer()) {
enforcer = new SyncedEnforcer(model, adapter);
logger.info("Casbin use SyncedEnforcer");
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public class CasbinProperties {
* Whether to use a synchronized Enforcer
*/
private boolean useSyncedEnforcer = false;
/**
* Whether to use a distributed Enforcer
*/
private boolean useDistributedEnforcer = false;
/**
* Local model file
*/
Expand Down Expand Up @@ -111,6 +115,14 @@ public void setUseSyncedEnforcer(boolean useSyncedEnforcer) {
this.useSyncedEnforcer = useSyncedEnforcer;
}

public boolean isUseDistributedEnforcer() {
return useDistributedEnforcer;
}

public void setUseDistributedEnforcer(boolean useDistributedEnforcer) {
this.useDistributedEnforcer = useDistributedEnforcer;
}

public String getModel() {
return model;
}
Expand Down

0 comments on commit dbfc31c

Please sign in to comment.