Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add DistributedEnforcer auto-configuration via useDistributedEnforcer #102

Merged
merged 1 commit into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading