Skip to content

Commit

Permalink
feat: Add lazyLoadPolicy option on enforcer constructor
Browse files Browse the repository at this point in the history
Signed-off-by: Sagilio <Sagilio@outlook.com>
  • Loading branch information
sagilio committed Jun 22, 2021
1 parent 252b1a2 commit 25b43dc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
19 changes: 19 additions & 0 deletions NetCasbin.UnitTest/ModelTests/EnforcerTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Casbin.Adapter.File;
using Casbin.Extensions;
Expand Down Expand Up @@ -950,5 +951,23 @@ public void TestEnforceWithMultipleEval()

Assert.True(result);
}

[Fact]
public void TestEnforceWithLazyLoadPolicy()
{
IModel m = DefaultModel.Create();
m.AddDef("r", "r", "sub, obj, act");
m.AddDef("p", "p", "sub, obj, act");
m.AddDef("e", "e", "some(where (p.eft == allow))");
m.AddDef("m", "m", "r.sub == p.sub && keyMatch(r.obj, p.obj) && regexMatch(r.act, p.act)");

IAdapter a = new FileAdapter("examples/keymatch_policy.csv");

IEnforcer e = DefaultEnforcer.Create(m, a, true);
Assert.Empty(e.GetPolicy());

e = DefaultEnforcer.Create(m, a);
Assert.NotEmpty(e.GetPolicy());
}
}
}
16 changes: 8 additions & 8 deletions NetCasbin/DefaultEnforcer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ namespace Casbin
{
public static class DefaultEnforcer
{
public static IEnforcer Create(IAdapter adapter = null)
public static IEnforcer Create(IAdapter adapter = null, bool lazyLoadPolicy = false)
{
return new Enforcer(DefaultModel.Create(), adapter);
return new Enforcer(DefaultModel.Create(), adapter, lazyLoadPolicy);
}

public static IEnforcer Create(string modelPath, string policyPath)
public static IEnforcer Create(string modelPath, string policyPath, bool lazyLoadPolicy = false)
{
return new Enforcer(modelPath, policyPath);
return new Enforcer(modelPath, policyPath, lazyLoadPolicy);
}

public static IEnforcer Create(string modelPath, IAdapter adapter = null)
public static IEnforcer Create(string modelPath, IAdapter adapter = null, bool lazyLoadPolicy = false)
{
return new Enforcer(modelPath, adapter);
return new Enforcer(modelPath, adapter, lazyLoadPolicy);
}

public static IEnforcer Create(IModel model, IAdapter adapter = null)
public static IEnforcer Create(IModel model, IAdapter adapter = null, bool lazyLoadPolicy = false)
{
return new Enforcer(model, adapter);
return new Enforcer(model, adapter, lazyLoadPolicy);
}
}
}
15 changes: 9 additions & 6 deletions NetCasbin/Enforcer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,27 @@ public Enforcer()
{
}

public Enforcer(string modelPath, string policyPath)
: this(modelPath, new FileAdapter(policyPath))
public Enforcer(string modelPath, string policyPath, bool lazyLoadPolicy = false)
: this(modelPath, new FileAdapter(policyPath), lazyLoadPolicy)
{
}

public Enforcer(string modelPath, IAdapter adapter = null)
: this(DefaultModel.CreateFromFile(modelPath), adapter)
public Enforcer(string modelPath, IAdapter adapter = null, bool lazyLoadPolicy = false)
: this(DefaultModel.CreateFromFile(modelPath), adapter, lazyLoadPolicy)
{
}

public Enforcer(IModel model, IAdapter adapter = null)
public Enforcer(IModel model, IAdapter adapter = null, bool lazyLoadPolicy = false)
{
this.SetModel(model);
if (adapter is not null)
{
this.SetAdapter(adapter);
}
this.LoadPolicy();
if (lazyLoadPolicy is false)
{
this.LoadPolicy();
}
}

#region Options
Expand Down

0 comments on commit 25b43dc

Please sign in to comment.