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

Casbin.net Breaking changes while migrating from V1.13.0 to V2.9.1 - Enforcer class

Open shrey-shah opened this issue 1 year ago • 7 comments

I am trying to migrate Casbin.net package from V1.13.0 to V2.9.1. My Enforcer class used to look like this in the previous version.

public class CustomEnforcer : Enforcer
    {
        public CustomEnforcer() : this("", "")
        {
        }
        public CustomEnforcer(string modelContent, string policyContent) : this(NewModel(modelContent), new DefaultFileContentAdapter(policyContent))
        {

        }

        public CustomEnforcer(Model m, IAdapter adapter)
        {
            SetAdapter(adapter);
            SetWatcher(null, false);
            SetModel(m);
            FunctionMap.LoadFunctionMap();
            EnforcerOptions enforcerOptions = new EnforcerOptions() 
            { 
            
            };
            Initialize(enforcerOptions);
            if (this.adapter != null)
            {
                LoadPolicy();
            }
        }
    }

I have changed it like this.

public class CustomEnforcer : Enforcer
    {
        public CustomEnforcer() : this("", "")
        {
        }
        public CustomEnforcer(string modelContent, string policyContent)
        {
            Model = DefaultModel.CreateFromText(modelContent);
            Adapter = new FileAdapter(policyContent);
        }

    }

is this correct? Do I need to do anything else? earlier version had a separate class with the implementation of DefaultFileContentAdapter. Do we need this in the latest version?

shrey-shah avatar Sep 06 '24 07:09 shrey-shah

@sagilio @sociometry @AsakusaRinne

casbin-bot avatar Sep 06 '24 07:09 casbin-bot

@shrey-shah why create two issues? https://github.com/casbin/Casbin.NET/issues/367

hsluoyz avatar Sep 06 '24 08:09 hsluoyz

@shrey-shah why create two issues? #367

It looks like two different issues

sagilio avatar Sep 06 '24 08:09 sagilio

Here is a sample for this issue.

public class CustomEnforcer : Enforcer
{
    public CustomEnforcer(string modelText, string policyText)
        : base(DefaultModel.CreateFromText(modelText),
            new FileAdapter(ReadPolicyText(policyText)))
    {

    }

    private static MemoryStream ReadPolicyText(string policyText)
    {
        return new MemoryStream(Encoding.UTF8.GetBytes(policyText));
    }
}

sagilio avatar Sep 06 '24 08:09 sagilio

my model and policy are like this: https://github.com/casbin/Casbin.NET/issues/367#issuecomment-2333998981 it gets passed in the string format itself. also, do we need the DefaultFileContentAdapter class now?

shrey-shah avatar Sep 06 '24 13:09 shrey-shah

any update here?

shrey-shah avatar Sep 16 '24 05:09 shrey-shah

my model and policy are like this: #367 (comment) it gets passed in the string format itself. also, do we need the DefaultFileContentAdapter class now?

It need not now. we will add new API for it.

sagilio avatar Sep 21 '24 06:09 sagilio