Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
126 changes: 126 additions & 0 deletions Casbin.UnitTests/PersistTests/AdapterTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.IO;
using System.Threading.Tasks;
using Casbin.Model;
using Casbin.Persist;
using Casbin.Persist.Adapter.File;
using Casbin.Persist.Adapter.Stream;
using Casbin.Persist.Adapter.Text;
Expand Down Expand Up @@ -49,6 +51,46 @@ public async Task TestFileAdapterAsync()
Assert.True(await e.EnforceAsync("bob", "data2", "write"));
}

[Fact]
public void TestFilteredFileAdapter()
{
Enforcer e = new("Examples/basic_model.conf");
Assert.False(e.Enforce("alice", "data1", "read"));

FileAdapter a = new("Examples/basic_policy.csv");
e.SetAdapter(a);
e.LoadFilteredPolicy(new PolicyFilter(PermConstants.DefaultPolicyType, 0, Policy.ValuesFrom(["bob"])));

Assert.False(e.Enforce("alice", "data1", "read")); // because "alice" is not in the filtered policy
Assert.False(e.Enforce("alice", "data1", "write"));
Assert.False(e.Enforce("alice", "data2", "read"));
Assert.False(e.Enforce("alice", "data2", "write"));
Assert.False(e.Enforce("bob", "data1", "read"));
Assert.False(e.Enforce("bob", "data1", "write"));
Assert.False(e.Enforce("bob", "data2", "read"));
Assert.True(e.Enforce("bob", "data2", "write"));
}

[Fact]
public async Task TestFilteredFileAdapterAsync()
{
Enforcer e = new("Examples/basic_model.conf");
Assert.False(await e.EnforceAsync("alice", "data1", "read"));

FileAdapter a = new("Examples/basic_policy.csv");
e.SetAdapter(a);
await e.LoadFilteredPolicyAsync(new PolicyFilter(PermConstants.DefaultPolicyType, 0, Policy.ValuesFrom(["bob"])));

Assert.False(await e.EnforceAsync("alice", "data1", "read")); // because "alice" is not in the filtered policy
Assert.False(await e.EnforceAsync("alice", "data1", "write"));
Assert.False(await e.EnforceAsync("alice", "data2", "read"));
Assert.False(await e.EnforceAsync("alice", "data2", "write"));
Assert.False(await e.EnforceAsync("bob", "data1", "read"));
Assert.False(await e.EnforceAsync("bob", "data1", "write"));
Assert.False(await e.EnforceAsync("bob", "data2", "read"));
Assert.True(await e.EnforceAsync("bob", "data2", "write"));
}

[Fact]
public void TestStreamAdapter()
{
Expand Down Expand Up @@ -89,6 +131,46 @@ public async Task TestStreamAdapterAsync()
Assert.True(await e.EnforceAsync("bob", "data2", "write"));
}

[Fact]
public void TestFilteredStreamAdapter()
{
Enforcer e = new("Examples/basic_model.conf");
Assert.False(e.Enforce("alice", "data1", "read"));

StreamAdapter a = new(File.OpenRead("Examples/basic_policy.csv"));
Comment thread
seniorquico marked this conversation as resolved.
Outdated
e.SetAdapter(a);
e.LoadFilteredPolicy(new PolicyFilter(PermConstants.DefaultPolicyType, 0, Policy.ValuesFrom(["bob"])));

Assert.False(e.Enforce("alice", "data1", "read")); // because "alice" is not in the filtered policy
Assert.False(e.Enforce("alice", "data1", "write"));
Assert.False(e.Enforce("alice", "data2", "read"));
Assert.False(e.Enforce("alice", "data2", "write"));
Assert.False(e.Enforce("bob", "data1", "read"));
Assert.False(e.Enforce("bob", "data1", "write"));
Assert.False(e.Enforce("bob", "data2", "read"));
Assert.True(e.Enforce("bob", "data2", "write"));
}

[Fact]
public async Task TestFilteredStreamAdapterAsync()
{
Enforcer e = new("Examples/basic_model.conf");
Assert.False(await e.EnforceAsync("alice", "data1", "read"));

StreamAdapter a = new(File.OpenRead("Examples/basic_policy.csv"));
Comment thread
seniorquico marked this conversation as resolved.
Outdated
e.SetAdapter(a);
await e.LoadFilteredPolicyAsync(new PolicyFilter(PermConstants.DefaultPolicyType, 0, Policy.ValuesFrom(["bob"])));

Assert.False(await e.EnforceAsync("alice", "data1", "read")); // because "alice" is not in the filtered policy
Assert.False(await e.EnforceAsync("alice", "data1", "write"));
Assert.False(await e.EnforceAsync("alice", "data2", "read"));
Assert.False(await e.EnforceAsync("alice", "data2", "write"));
Assert.False(await e.EnforceAsync("bob", "data1", "read"));
Assert.False(await e.EnforceAsync("bob", "data1", "write"));
Assert.False(await e.EnforceAsync("bob", "data2", "read"));
Assert.True(await e.EnforceAsync("bob", "data2", "write"));
}

[Fact]
public void TestTextAdapter()
{
Expand Down Expand Up @@ -132,4 +214,48 @@ public async Task TestTextAdapterAsync()
Assert.False(await e.EnforceAsync("bob", "data2", "read"));
Assert.True(await e.EnforceAsync("bob", "data2", "write"));
}

[Fact]
public void TestFilteredTextAdapter()
{
Enforcer e = new("Examples/basic_model.conf");
Assert.False(e.Enforce("alice", "data1", "read"));

TextAdapter a = new(File.ReadAllText("Examples/basic_policy.csv"));
e.SetAdapter(a);
e.LoadFilteredPolicy(new PolicyFilter(PermConstants.DefaultPolicyType, 0, Policy.ValuesFrom(["bob"])));

Assert.False(e.Enforce("alice", "data1", "read")); // because "alice" is not in the filtered policy
Assert.False(e.Enforce("alice", "data1", "write"));
Assert.False(e.Enforce("alice", "data2", "read"));
Assert.False(e.Enforce("alice", "data2", "write"));
Assert.False(e.Enforce("bob", "data1", "read"));
Assert.False(e.Enforce("bob", "data1", "write"));
Assert.False(e.Enforce("bob", "data2", "read"));
Assert.True(e.Enforce("bob", "data2", "write"));
}

[Fact]
public async Task TestFilteredTextAdapterAsync()
{
Enforcer e = new("Examples/basic_model.conf");
Assert.False(await e.EnforceAsync("alice", "data1", "read"));

#if NET452 || NET461 || NET462
TextAdapter a = new(File.ReadAllText("Examples/basic_policy.csv"));
#else
TextAdapter a = new(await File.ReadAllTextAsync("Examples/basic_policy.csv"));
#endif
e.SetAdapter(a);
await e.LoadFilteredPolicyAsync(new PolicyFilter(PermConstants.DefaultPolicyType, 0, Policy.ValuesFrom(["bob"])));

Assert.False(await e.EnforceAsync("alice", "data1", "read")); // because "alice" is not in the filtered policy
Assert.False(await e.EnforceAsync("alice", "data1", "write"));
Assert.False(await e.EnforceAsync("alice", "data2", "read"));
Assert.False(await e.EnforceAsync("alice", "data2", "write"));
Assert.False(await e.EnforceAsync("bob", "data1", "read"));
Assert.False(await e.EnforceAsync("bob", "data1", "write"));
Assert.False(await e.EnforceAsync("bob", "data2", "read"));
Assert.True(await e.EnforceAsync("bob", "data2", "write"));
}
}
2 changes: 1 addition & 1 deletion Casbin/Persist/PolicyFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Casbin.Persist;

public class PolicyFilter
public class PolicyFilter : IPolicyFilter
{
public static readonly PolicyFilter Empty = new();
private readonly int _fieldIndex;
Expand Down
Loading