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.14k stars 110 forks source link

Trouble removing policies. #218

Closed thoraj closed 2 years ago

thoraj commented 2 years ago

I'm having trouble removing policies.

This section of our code throws:

            var g2Policies = _enforcer.GetFilteredNamedGroupingPolicy("g2", 2, organization.Id);
            _logger.Information("Deleting {PolicyCount} instance assignments for organization {CustomerId}", g2Policies.Count, organization.Id);
            var wasSuccessful = await _enforcer.RemoveGroupingPoliciesAsync(g2Policies);
            if (!wasSuccessful)
            {
                throw new Exception($"Unable to delete {g2Policies.Count} instance assignment policies for organization {organization.Id}");
            }

The docs says something about consistent policies:

RemovePolicies() RemovePolicies removes authorization rules from the current policy. The operation is atomic in nature. Hence, if authorization rules consists of rules which are not consistent with the current policy, the function returns false and no policy rule is removed from the current policy. If all the authorization rules are consistent with the policy rules, the function returns true and each policy rule is removed from the current policy.

Could our issues be related to this?

What does it mean that -- rules are consistent with the current policy?

casbin-bot commented 2 years ago

@sagilio @xcaptain @huazhikui

sagilio commented 2 years ago

When deleting policies, it is necessary to ensure that each policy can be found in the model.

Can you post the value of g2Policies and all g2 policies? The sample code should return success.

The method may return false at line 299 and 318.

https://github.com/casbin/Casbin.NET/blob/41588124dec1f1d9e1d6c71b948e6b2e1794d4da/NetCasbin/InternalEnforcer.cs#L293-L330

thoraj commented 2 years ago

Seems like line 318 is unreachable code since model.RemovePolicies() either throws or returns true. That leaves line 199.

Not exactly sure why this was biting me, but I worked around this by using _enforcer.RemoveFilteredNamedGroupingPolicyAsync()