Skip to content

Commit

Permalink
feat: Add WatcherEx support
Browse files Browse the repository at this point in the history
Signed-off-by: sagilio <sagilio@outlook.com>
  • Loading branch information
sagilio committed Sep 12, 2022
1 parent eb28952 commit bd9ff64
Show file tree
Hide file tree
Showing 28 changed files with 709 additions and 349 deletions.
93 changes: 55 additions & 38 deletions Casbin.UnitTests/PersistTests/WatcherMessageTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class WatcherMessageTest

public WatcherMessageTest(TestModelFixture testModelFixture) => _testModelFixture = testModelFixture;

private void MessageEquals(IWatcherMessage message, IWatcherMessage message2)
private void MessageEquals(PolicyChangedMessage message, PolicyChangedMessage message2)
{
Assert.Equal(message.Operation, message2.Operation);
Assert.Equal(message.Section, message2.Section);
Expand All @@ -36,14 +36,14 @@ public void TestAddPolicy()
Enforcer enforcer = new(_testModelFixture.GetNewRbacTestModel(),
new FileAdapter(TestModelFixture.GetTestFile("rbac_policy_for_watcher_test.csv")));

enforcer.SetWatcher(sampleWatcher, false);
enforcer.SetWatcher(sampleWatcher);

enforcer.AddPolicy("alice", "data1", "read");
Assert.True(sampleWatcher.WatcherMessage is null);

enforcer.AddPolicy("alice", "book1", "write");
MessageEquals(sampleWatcher.WatcherMessage,
WatcherMessage.CreateAddPolicyMessage("p", "p", Policy.ValuesFrom(new[] { "alice", "book1", "write" })));
PolicyChangedMessage.CreateAddPolicy("p", "p", Policy.ValuesFrom(new[] { "alice", "book1", "write" })));
}

[Fact]
Expand All @@ -54,14 +54,14 @@ public void TestRemovePolicy()
Enforcer enforcer = new(_testModelFixture.GetNewRbacTestModel(),
new FileAdapter(TestModelFixture.GetTestFile("rbac_policy_for_watcher_test.csv")));

enforcer.SetWatcher(sampleWatcher, false);
enforcer.SetWatcher(sampleWatcher);

enforcer.RemovePolicy("alice", "data1", "write");
Assert.True(sampleWatcher.WatcherMessage is null);

enforcer.RemovePolicy("alice", "data1", "read");
MessageEquals(sampleWatcher.WatcherMessage,
WatcherMessage.CreateRemovePolicyMessage("p", "p", Policy.ValuesFrom(new[] { "alice", "data1", "read" })));
PolicyChangedMessage.CreateRemovePolicy("p", "p", Policy.ValuesFrom(new[] { "alice", "data1", "read" })));
}

[Fact]
Expand All @@ -72,14 +72,14 @@ public void TestUpdatePolicy()
Enforcer enforcer = new(_testModelFixture.GetNewRbacTestModel(),
new FileAdapter(TestModelFixture.GetTestFile("rbac_policy_for_watcher_test.csv")));

enforcer.SetWatcher(sampleWatcher, false);
enforcer.SetWatcher(sampleWatcher);

enforcer.UpdatePolicy(new[] { "alice", "book1", "read" }, "alice", "book2", "write");
Assert.True(sampleWatcher.WatcherMessage is null);

enforcer.UpdatePolicy(new[] { "alice", "data1", "read" }, "alice", "book2", "write");
MessageEquals(sampleWatcher.WatcherMessage,
WatcherMessage.CreateUpdatePolicyMessage("p", "p",
PolicyChangedMessage.CreateUpdatePolicy("p", "p",
Policy.ValuesFrom(new[] { "alice", "data1", "read" }),
Policy.ValuesFrom(new[] { "alice", "book2", "write" })));
}
Expand All @@ -92,9 +92,9 @@ public void TestSavePolicy()
Enforcer enforcer = new(_testModelFixture.GetNewRbacTestModel(),
new FileAdapter(TestModelFixture.GetTestFile("rbac_policy_for_watcher_test.csv")));

enforcer.SetWatcher(sampleWatcher, false);
enforcer.SetWatcher(sampleWatcher);
enforcer.SavePolicy();
MessageEquals(sampleWatcher.WatcherMessage, WatcherMessage.CreateSavePolicyMessage());
MessageEquals(sampleWatcher.WatcherMessage, PolicyChangedMessage.CreateSavePolicy());
}

[Fact]
Expand All @@ -105,7 +105,7 @@ public void TestAddPolicies()
Enforcer enforcer = new(_testModelFixture.GetNewRbacTestModel(),
new FileAdapter(TestModelFixture.GetTestFile("rbac_policy_for_watcher_test.csv")));

enforcer.SetWatcher(sampleWatcher, false);
enforcer.SetWatcher(sampleWatcher);

enforcer.AddPolicies(new[] { new[] { "data2_admin", "data2", "read" }, new[] { "frank", "book4", "read" } });
Assert.True(sampleWatcher.WatcherMessage is null);
Expand All @@ -116,7 +116,7 @@ public void TestAddPolicies()
};
enforcer.AddPolicies(rules);
MessageEquals(sampleWatcher.WatcherMessage,
WatcherMessage.CreateAddPoliciesMessage("p", "p", Policy.ValuesListFrom(rules)));
PolicyChangedMessage.CreateAddPolicies("p", "p", Policy.ValuesListFrom(rules)));
}

[Fact]
Expand All @@ -127,7 +127,7 @@ public void TestRemovePolicies()
Enforcer enforcer = new(_testModelFixture.GetNewRbacTestModel(),
new FileAdapter(TestModelFixture.GetTestFile("rbac_policy_for_watcher_test.csv")));

enforcer.SetWatcher(sampleWatcher, false);
enforcer.SetWatcher(sampleWatcher);

IEnumerable<IEnumerable<string>> rules = new[]
{
Expand All @@ -139,7 +139,7 @@ public void TestRemovePolicies()
rules = new[] { new[] { "data2_admin", "data2", "read" }, new[] { "alice", "book3", "read" } };
enforcer.RemovePolicies(rules);
MessageEquals(sampleWatcher.WatcherMessage,
WatcherMessage.CreateRemovePoliciesMessage("p", "p", Policy.ValuesListFrom(rules)));
PolicyChangedMessage.CreateRemovePolicies("p", "p", Policy.ValuesListFrom(rules)));
}

[Fact]
Expand All @@ -150,7 +150,7 @@ public void TestUpdatePolicies()
Enforcer enforcer = new(_testModelFixture.GetNewRbacTestModel(),
new FileAdapter(TestModelFixture.GetTestFile("rbac_policy_for_watcher_test.csv")));

enforcer.SetWatcher(sampleWatcher, false);
enforcer.SetWatcher(sampleWatcher);

enforcer.UpdatePolicies(new[] { new[] { "data2_admin", "data2", "read" }, new[] { "frank", "book4", "read" } },
new[] { new[] { "data3_admin", "data200", "read" }, new[] { "frank", "book6", "read" } });
Expand All @@ -166,7 +166,7 @@ public void TestUpdatePolicies()
};
enforcer.UpdatePolicies(oldRules, newRules);
MessageEquals(sampleWatcher.WatcherMessage,
WatcherMessage.CreateUpdatePoliciesMessage("p", "p", Policy.ValuesListFrom(oldRules),
PolicyChangedMessage.CreateUpdatePolicies("p", "p", Policy.ValuesListFrom(oldRules),
Policy.ValuesListFrom(newRules)));
}

Expand All @@ -178,7 +178,7 @@ public void TestRemoveFilteredPolicy()
Enforcer enforcer = new(_testModelFixture.GetNewRbacTestModel(),
new FileAdapter(TestModelFixture.GetTestFile("rbac_policy_for_watcher_test.csv")));

enforcer.SetWatcher(sampleWatcher, false);
enforcer.SetWatcher(sampleWatcher);

enforcer.RemoveFilteredPolicy(1, "data3");
Assert.True(sampleWatcher.WatcherMessage is null);
Expand All @@ -189,7 +189,7 @@ public void TestRemoveFilteredPolicy()
};
enforcer.RemoveFilteredPolicy(0, "data2_admin");
MessageEquals(sampleWatcher.WatcherMessage,
WatcherMessage.CreateRemoveFilteredPolicyMessage("p", "p", 0, Policy.ValuesListFrom(rules)));
PolicyChangedMessage.CreateRemoveFilteredPolicy("p", "p", 0, Policy.ValuesListFrom(rules)));
}

[Fact]
Expand All @@ -200,14 +200,14 @@ public async Task TestAddPolicyAsync()
Enforcer enforcer = new(_testModelFixture.GetNewRbacTestModel(),
new FileAdapter(TestModelFixture.GetTestFile("rbac_policy_for_watcher_test.csv")));

enforcer.SetWatcher(sampleWatcher, false);
enforcer.SetWatcher(sampleWatcher);

await enforcer.AddPolicyAsync("alice", "data1", "read");
Assert.True(sampleWatcher.AsyncWatcherMessage is null);

await enforcer.AddPolicyAsync("alice", "book1", "write");
MessageEquals(sampleWatcher.AsyncWatcherMessage,
WatcherMessage.CreateAddPolicyMessage("p", "p", Policy.ValuesFrom(new[] { "alice", "book1", "write" })));
PolicyChangedMessage.CreateAddPolicy("p", "p", Policy.ValuesFrom(new[] { "alice", "book1", "write" })));
}

[Fact]
Expand All @@ -218,14 +218,14 @@ public async Task TestRemovePolicyAsync()
Enforcer enforcer = new(_testModelFixture.GetNewRbacTestModel(),
new FileAdapter(TestModelFixture.GetTestFile("rbac_policy_for_watcher_test.csv")));

enforcer.SetWatcher(sampleWatcher, false);
enforcer.SetWatcher(sampleWatcher);

await enforcer.RemovePolicyAsync("alice", "data1", "write");
Assert.True(sampleWatcher.AsyncWatcherMessage is null);

await enforcer.RemovePolicyAsync("alice", "data1", "read");
MessageEquals(sampleWatcher.AsyncWatcherMessage,
WatcherMessage.CreateRemovePolicyMessage("p", "p", Policy.ValuesFrom(new[] { "alice", "data1", "read" })));
PolicyChangedMessage.CreateRemovePolicy("p", "p", Policy.ValuesFrom(new[] { "alice", "data1", "read" })));
}

[Fact]
Expand All @@ -236,14 +236,14 @@ public async Task TestUpdatePolicyAsync()
Enforcer enforcer = new(_testModelFixture.GetNewRbacTestModel(),
new FileAdapter(TestModelFixture.GetTestFile("rbac_policy_for_watcher_test.csv")));

enforcer.SetWatcher(sampleWatcher, false);
enforcer.SetWatcher(sampleWatcher);

await enforcer.UpdatePolicyAsync(new[] { "alice", "book1", "read" }, "alice", "book2", "write");
Assert.True(sampleWatcher.AsyncWatcherMessage is null);

await enforcer.UpdatePolicyAsync(new[] { "alice", "data1", "read" }, "alice", "book2", "write");
MessageEquals(sampleWatcher.AsyncWatcherMessage,
WatcherMessage.CreateUpdatePolicyMessage("p", "p", Policy.ValuesFrom(new[] { "alice", "data1", "read" }),
PolicyChangedMessage.CreateUpdatePolicy("p", "p", Policy.ValuesFrom(new[] { "alice", "data1", "read" }),
Policy.ValuesFrom(new[] { "alice", "book2", "write" })));
}

Expand All @@ -255,11 +255,11 @@ public async Task TestSavePolicyAsync()
Enforcer enforcer = new(_testModelFixture.GetNewRbacTestModel(),
new FileAdapter(TestModelFixture.GetTestFile("rbac_policy_for_watcher_test.csv")));

enforcer.SetWatcher(sampleWatcher, false);
enforcer.SetWatcher(sampleWatcher);

await enforcer.SavePolicyAsync();
MessageEquals(sampleWatcher.AsyncWatcherMessage,
WatcherMessage.CreateSavePolicyMessage());
PolicyChangedMessage.CreateSavePolicy());
}

[Fact]
Expand All @@ -270,7 +270,7 @@ public async Task TestAddPoliciesAsync()
Enforcer enforcer = new(_testModelFixture.GetNewRbacTestModel(),
new FileAdapter(TestModelFixture.GetTestFile("rbac_policy_for_watcher_test.csv")));

enforcer.SetWatcher(sampleWatcher, false);
enforcer.SetWatcher(sampleWatcher);

await enforcer.AddPoliciesAsync(new[]
{
Expand All @@ -284,7 +284,7 @@ await enforcer.AddPoliciesAsync(new[]
};
await enforcer.AddPoliciesAsync(rules);
MessageEquals(sampleWatcher.AsyncWatcherMessage,
WatcherMessage.CreateAddPoliciesMessage("p", "p", Policy.ValuesListFrom(rules)));
PolicyChangedMessage.CreateAddPolicies("p", "p", Policy.ValuesListFrom(rules)));
}

[Fact]
Expand All @@ -295,7 +295,7 @@ public async Task TestRemovePoliciesAsync()
Enforcer enforcer = new(_testModelFixture.GetNewRbacTestModel(),
new FileAdapter(TestModelFixture.GetTestFile("rbac_policy_for_watcher_test.csv")));

enforcer.SetWatcher(sampleWatcher, false);
enforcer.SetWatcher(sampleWatcher);

IEnumerable<IEnumerable<string>> rules = new[]
{
Expand All @@ -307,7 +307,7 @@ public async Task TestRemovePoliciesAsync()
rules = new[] { new[] { "data2_admin", "data2", "read" }, new[] { "alice", "book3", "read" } };
await enforcer.RemovePoliciesAsync(rules);
MessageEquals(sampleWatcher.AsyncWatcherMessage,
WatcherMessage.CreateRemovePoliciesMessage("p", "p", Policy.ValuesListFrom(rules)));
PolicyChangedMessage.CreateRemovePolicies("p", "p", Policy.ValuesListFrom(rules)));
}

[Fact]
Expand All @@ -318,7 +318,7 @@ public async Task TestUpdatePoliciesAsync()
Enforcer enforcer = new(_testModelFixture.GetNewRbacTestModel(),
new FileAdapter(TestModelFixture.GetTestFile("rbac_policy_for_watcher_test.csv")));

enforcer.SetWatcher(sampleWatcher, false);
enforcer.SetWatcher(sampleWatcher);

await enforcer.UpdatePoliciesAsync(
new[] { new[] { "data2_admin", "data2", "read" }, new[] { "frank", "book4", "read" } },
Expand All @@ -335,7 +335,7 @@ await enforcer.UpdatePoliciesAsync(
};
await enforcer.UpdatePoliciesAsync(oldRules, newRules);
MessageEquals(sampleWatcher.AsyncWatcherMessage,
WatcherMessage.CreateUpdatePoliciesMessage("p", "p", Policy.ValuesListFrom(oldRules),
PolicyChangedMessage.CreateUpdatePolicies("p", "p", Policy.ValuesListFrom(oldRules),
Policy.ValuesListFrom(newRules)));
}

Expand All @@ -347,7 +347,7 @@ public async Task TestRemoveFilteredPolicyAsync()
Enforcer enforcer = new(_testModelFixture.GetNewRbacTestModel(),
new FileAdapter(TestModelFixture.GetTestFile("rbac_policy_for_watcher_test.csv")));

enforcer.SetWatcher(sampleWatcher, false);
enforcer.SetWatcher(sampleWatcher);

await enforcer.RemoveFilteredPolicyAsync(1, "data3");
Assert.True(sampleWatcher.AsyncWatcherMessage is null);
Expand All @@ -358,36 +358,53 @@ public async Task TestRemoveFilteredPolicyAsync()
};
await enforcer.RemoveFilteredPolicyAsync(0, "data2_admin");
MessageEquals(sampleWatcher.AsyncWatcherMessage,
WatcherMessage.CreateRemoveFilteredPolicyMessage("p", "p", 0, Policy.ValuesListFrom(rules)));
PolicyChangedMessage.CreateRemoveFilteredPolicy("p", "p", 0, Policy.ValuesListFrom(rules)));
}

private class SampleWatcher : IWatcher
{
private Func<Task> _asyncCallback;
private Action _callback;

public IWatcherMessage WatcherMessage { get; private set; }
public PolicyChangedMessage WatcherMessage { get; private set; }

public IWatcherMessage AsyncWatcherMessage { get; private set; }
public PolicyChangedMessage AsyncWatcherMessage { get; private set; }

public void SetUpdateCallback(Action callback) => _callback = callback;

public void SetUpdateCallback(Func<Task> callback) => _asyncCallback = callback;
public void Update() => _callback?.Invoke();

public void Update(IWatcherMessage watcherMessage)
public async Task UpdateAsync()
{
if (_asyncCallback is not null)
{
await _asyncCallback.Invoke();
}
}

public void SetUpdateCallback(Action<PolicyChangedMessage> callback) => throw new NotImplementedException();

public void SetUpdateCallback(Func<PolicyChangedMessage, Task> callback) => throw new NotImplementedException();

public void Update(PolicyChangedMessage watcherMessage)
{
_callback?.Invoke();
WatcherMessage = watcherMessage;
}

public async Task UpdateAsync(IWatcherMessage watcherMessage)
public async Task UpdateAsync(PolicyChangedMessage watcherMessage)
{
if (!(_asyncCallback is null))
if (_asyncCallback is not null)
{
await _asyncCallback.Invoke();
}

AsyncWatcherMessage = watcherMessage;
}

public void Close()
{
}
}
}
23 changes: 19 additions & 4 deletions Casbin.UnitTests/PersistTests/WatcherTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ public async Task ShouldUpdateAsync()
Assert.True(sampleWatcher.AsyncCalled);
}

public class SampleWatcher : IWatcher
private class SampleWatcher : IWatcher
{
private Func<Task> _asyncCallback;

private Action _callback;

public bool Called { get; private set; }
Expand All @@ -55,20 +56,34 @@ public class SampleWatcher : IWatcher

public void SetUpdateCallback(Func<Task> callback) => _asyncCallback = callback;

public void Update(IWatcherMessage watcherMessage)
public void Update()
{
_callback?.Invoke();
Called = true;
}

public async Task UpdateAsync(IWatcherMessage watcherMessage)
public async Task UpdateAsync()
{
if (!(_asyncCallback is null))
if (_asyncCallback is not null)
{
await _asyncCallback.Invoke();
}

AsyncCalled = true;
}

public void SetUpdateCallback(Action<PolicyChangedMessage> callback) => throw new NotImplementedException();

public void SetUpdateCallback(Func<PolicyChangedMessage, Task> callback) => throw new NotImplementedException();

public void Update(PolicyChangedMessage message) => Update();

public Task UpdateAsync(PolicyChangedMessage message) => UpdateAsync();

public void Close() => _callback = () => { };

public void SetUpdateCallback<TMessage>(Func<TMessage> callback) => throw new NotImplementedException();

public void SetUpdateCallback<TMessage>(Func<TMessage, Task> callback) => throw new NotImplementedException();
}
}
4 changes: 4 additions & 0 deletions Casbin/Abstractions/Effect/IChainEffector.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
namespace Casbin.Effect
{
public interface IChainEffector : IChainEffector<EffectChain>
{
}

public interface IChainEffector<out TChain> where TChain : IEffectChain
{
public TChain CreateChain(string policyEffect);
Expand Down
Loading

0 comments on commit bd9ff64

Please sign in to comment.