Casbin.NET
Casbin.NET copied to clipboard
IReadonlyAdapter not getting set in preview?
I've been playing with the latest preview version of Casbin.NET, and when I construct an Enforcer like this:
new Enforcer("model.config", myTestAdapter);
where myTestAdapter implements the IReadOnlyAdapter interface, I notice that the Adapter in the Enforcer instance is always set to null.
@sagilio @sociometry @AsakusaRinne
Hey, I could see the Enforcer.Adapter
being set when I tried it. Maybe the myTestAdapter
was not initialized, I guess?
No I passed in an instance of the adapter. Hmmm... which preview are you using?
No I passed in an instance of the adapter. Hmmm... which preview are you using?
I'm using the preview branch of Casbin.NET. Could you plz provide the release version you used?
https://github.com/casbin/Casbin.NET/releases/tag/v2.0.0-preview.4
I tried it with v2.0.0-preview.4
just now with FileAdapter
and got the same result. Would you mind show your code here, or try with FileAdapter
first to narrow the range of the problem? In the constructor of Enforcer
, it simply executes this.Adapter = adapter;
when the given adapter is not null.
FileAdapter worked for me too.
Here is what my TestAdapter looks like. It doesn't matter what is inside of the two methods (I'm using extension methods if you were curious) as they never get called.
So, for some reason the Enforcer doesn't set my adpater, but it does for a file adapter?
public class TestAdapter : Casbin.Persist.IReadOnlyAdapter
{
public void LoadPolicy(IPolicyStore model)
{
model.LoadAuthPolicy(CreateTestAuthPolicy());
}
public Task LoadPolicyAsync(IPolicyStore model)
{
model.LoadAuthPolicy(CreateTestAuthPolicy());
return Task.CompletedTask;
}
}
If I change my TestAdapter to implement IEpochAdapter instead, it works! Strange, eh?
Also worth noting maybe this class needs to change? Maybe LoadPolicy and LoadPolicyAsync should use an IReadOnlyAdapter instead?
public static class AdapterExtension
{
public static void LoadPolicy(this IEpochAdapter adapter, IModel model) =>
adapter.LoadPolicy(model.PolicyStoreHolder.PolicyStore);
public static Task LoadPolicyAsync(this IEpochAdapter adapter, IModel model) =>
adapter.LoadPolicyAsync(model.PolicyStoreHolder.PolicyStore);
public static void SavePolicy(this IEpochAdapter adapter, IModel model) =>
adapter.SavePolicy(model.PolicyStoreHolder.PolicyStore);
public static Task SavePolicyAsync(this IEpochAdapter adapter, IModel model) =>
adapter.SavePolicyAsync(model.PolicyStoreHolder.PolicyStore);
}
Yes, I got the same result on v2.0.0-preview.4
with you. It's quite strange because in the code below, the enforcer.Adapter
does not change after executing enforcer.Adapter = adapter
.
public static IEnforcer SetAdapter(this IEnforcer enforcer, IReadOnlyAdapter adapter)
{
enforcer.Adapter = adapter;
return enforcer;
}
What's more, with the latest commit of the current preview branch, I could get the correct result without changing IReadOnlyAdapter
to IEpochAdapter
. Hmmm... It confused me, too.
Sounds like (a) I'm not crazy, and (b) we might need a new preview branch soon? :)
Also, any idea when 2.x gets officially released? Enquiring minds want to know...
Haha, it's a question which belongs to @sagilio. I guess it's not far.
@abelfiore v2.0.0 has already been released on 2023-07-25: https://github.com/casbin/Casbin.NET/releases/tag/v2.0.0
Latest version is v2.1.1: https://github.com/casbin/Casbin.NET/releases