From 82fed559fe0a1b291d6d139297c2e88f9f3149af Mon Sep 17 00:00:00 2001 From: Jay Patel <36803168+jay-babu@users.noreply.github.com> Date: Sat, 29 Jun 2024 21:18:57 -0400 Subject: [PATCH] feat: add DistributedEnforcer auto-configuration via useDistributedEnforcer --- .../boot/autoconfigure/CasbinAutoConfiguration.java | 6 +++++- .../autoconfigure/properties/CasbinProperties.java | 12 ++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/casbin/spring/boot/autoconfigure/CasbinAutoConfiguration.java b/src/main/java/org/casbin/spring/boot/autoconfigure/CasbinAutoConfiguration.java index 710d5a4..04dfebb 100644 --- a/src/main/java/org/casbin/spring/boot/autoconfigure/CasbinAutoConfiguration.java +++ b/src/main/java/org/casbin/spring/boot/autoconfigure/CasbinAutoConfiguration.java @@ -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; @@ -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 { diff --git a/src/main/java/org/casbin/spring/boot/autoconfigure/properties/CasbinProperties.java b/src/main/java/org/casbin/spring/boot/autoconfigure/properties/CasbinProperties.java index 3aab9fd..69d9364 100644 --- a/src/main/java/org/casbin/spring/boot/autoconfigure/properties/CasbinProperties.java +++ b/src/main/java/org/casbin/spring/boot/autoconfigure/properties/CasbinProperties.java @@ -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 */ @@ -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; }