casbin / Casbin.NET

An authorization library that supports access control models like ACL, RBAC, ABAC in .NET (C#)
https://casbin.org
Apache License 2.0
1.13k stars 110 forks source link

IReadonlyAdapter not getting set in preview? #298

Closed abelfiore closed 3 months ago

abelfiore commented 1 year ago

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.

casbin-bot commented 1 year ago

@sagilio @sociometry @AsakusaRinne

AsakusaRinne commented 1 year ago

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

abelfiore commented 1 year ago

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

AsakusaRinne commented 1 year ago

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?

abelfiore commented 1 year ago

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

AsakusaRinne commented 1 year ago

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.

abelfiore commented 1 year ago

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 commented 1 year ago

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

abelfiore commented 1 year ago

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);
}
AsakusaRinne commented 1 year ago

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.

abelfiore commented 1 year ago

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

AsakusaRinne commented 1 year ago

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

hsluoyz commented 5 months ago

@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