Casbin.NET icon indicating copy to clipboard operation
Casbin.NET copied to clipboard

IReadonlyAdapter not getting set in preview?

Open abelfiore opened this issue 2 years ago • 13 comments

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.

abelfiore avatar Sep 26 '22 13:09 abelfiore

@sagilio @sociometry @AsakusaRinne

casbin-bot avatar Sep 26 '22 13:09 casbin-bot

Hey, I could see the Enforcer.Adapter being set when I tried it. Maybe the myTestAdapter was not initialized, I guess?

AsakusaRinne avatar Sep 26 '22 14:09 AsakusaRinne

No I passed in an instance of the adapter. Hmmm... which preview are you using?

abelfiore avatar Sep 26 '22 14:09 abelfiore

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?

AsakusaRinne avatar Sep 26 '22 14:09 AsakusaRinne

https://github.com/casbin/Casbin.NET/releases/tag/v2.0.0-preview.4

abelfiore avatar Sep 26 '22 14:09 abelfiore

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.

AsakusaRinne avatar Sep 26 '22 15:09 AsakusaRinne

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;
    }
}

abelfiore avatar Sep 26 '22 15:09 abelfiore

If I change my TestAdapter to implement IEpochAdapter instead, it works! Strange, eh?

abelfiore avatar Sep 26 '22 15:09 abelfiore

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);
}

abelfiore avatar Sep 26 '22 15:09 abelfiore

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.

AsakusaRinne avatar Sep 26 '22 16:09 AsakusaRinne

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...

abelfiore avatar Sep 26 '22 16:09 abelfiore

Haha, it's a question which belongs to @sagilio. I guess it's not far.

AsakusaRinne avatar Sep 26 '22 16:09 AsakusaRinne

@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

hsluoyz avatar Jan 27 '24 12:01 hsluoyz