casbin-net / efcore-adapter

Entity Framework Core Adapter for Casbin.NET
https://github.com/casbin/Casbin.NET
Apache License 2.0
48 stars 21 forks source link

RBAC and argument out of range exception #38

Closed pavolsimon closed 3 years ago

pavolsimon commented 3 years ago

Hello;

When using this adapter (code from master branch) and deleting policy, I am getting exception. The same configuration and code is working as expected when using only in memory Enforcer and Model.

Any help would be much appreciated.

[request_definition]
r = sub, dom, obj, act

[policy_definition]
p = sub, dom, obj, act, eft

[role_definition]
g = _, _, _
g2 = _, _, _

[policy_effect]
e = some(where (p.eft == allow)) && !some(where (p.eft == deny))

[matchers]
m = g(r.sub, p.sub, r.dom) && g2(r.obj, p.obj, r.dom) && r.act == p.act && r.dom == p.dom

And test:

    var policyAdded = enforcer.AddNamedPolicy("p", "alice", "orga1", "site1:orga1", "READ", "allow");
    var policyRemoved = enforcer.RemoveNamedPolicy("p", "alice", "orga1", "site1:orga1", "READ", "allow");

I am getting ArgumentOutOfRange exception: Message: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')

StackTrace:

   at System.ThrowHelper.ThrowArgumentOutOfRange_IndexException()
   at System.SZArrayHelper.get_Item[T](Int32 index)
   at Casbin.Adapter.EFCore.Extensions.CasbinRuleExtenstion.ApplyQueryFilter[TCasbinRule](IQueryable`1 query, String policyType, Int32 fieldIndex, IEnumerable`1 fieldValues) in C:\Repo\EFCore-Adapter-master\Casbin.Adapter.EFCore\Extensions\CasbinRuleExtenstion.cs:line 131
   at Casbin.Adapter.EFCore.EFCoreAdapter`3.RemoveFilteredPolicyInMemory(String section, String policyType, Int32 fieldIndex, String[] fieldValues) in C:\Repo\EFCore-Adapter-master\Casbin.Adapter.EFCore\EFCoreAdapter.cs:line 320
   at Casbin.Adapter.EFCore.EFCoreAdapter`3.RemoveFilteredPolicy(String section, String policyType, Int32 fieldIndex, String[] fieldValues) in C:\Repo\EFCore-Adapter-master\Casbin.Adapter.EFCore\EFCoreAdapter.cs:line 233
   at Casbin.Adapter.EFCore.EFCoreAdapter`3.RemovePolicyInMemory(String section, String policyType, IEnumerable`1 rule) in C:\Repo\EFCore-Adapter-master\Casbin.Adapter.EFCore\EFCoreAdapter.cs:line 315
   at Casbin.Adapter.EFCore.EFCoreAdapter`3.RemovePolicy(String section, String policyType, IList`1 rule) in C:\Repo\EFCore-Adapter-master\Casbin.Adapter.EFCore\EFCoreAdapter.cs:line 211
   at NetCasbin.InternalEnforcer.InternalRemovePolicy(String sec, String ptype, List`1 rule)
   at NetCasbin.ManagementEnforcer.RemoveNamedPolicy(String ptype, List`1 parameters)
   at NetCasbin.ManagementEnforcer.RemoveNamedPolicy(String ptype, String[] parameters)
   at Casbin.Adapter.EFCore.UnitTest.AdapterTest.TestAddRemoveNamedPolicy() in C:\Repo\EFCore-Adapter-master\Casbin.Adapter.EFCore.UnitTest\AdapterTest.cs:line 284

Code location: Class CasbinRuleExtension.cs Method

 internal static IQueryable<TCasbinRule> ApplyQueryFilter<TCasbinRule>(this IQueryable<TCasbinRule> query, 
            string policyType , int fieldIndex, IEnumerable<string> fieldValues)
            where TCasbinRule : ICasbinRule

Failing line (fieldValueList count is 5, index is 5-0):

if (lastIndex is 5)
            {
                string field = fieldValuesList[5 - fieldIndex]; //here
                if (string.IsNullOrWhiteSpace(field) is false)
                {
                    query = query.Where(p => p.V5 == field);
                }
            }
hsluoyz commented 3 years ago

@Sagilio

sagilio commented 3 years ago

@pavolsimon Thank you! this bug will be fixed by #39, could you have a review?

pavolsimon commented 3 years ago

Thank you very much, for fast fix and amazing work :)

@hsluoyz, @Sagilio : When is the new release planned?

Thanks!

Pavol

hsluoyz commented 3 years ago

v1.5.1 released: https://github.com/casbin-net/EFCore-Adapter/releases/tag/v1.5.1