Skip to content

Commit

Permalink
fix: Error result returned in HasLink method
Browse files Browse the repository at this point in the history
Signed-off-by: Sagilio <Sagilio@outlook.com>
  • Loading branch information
sagilio committed May 17, 2021
1 parent b172914 commit 49a8cb2
Show file tree
Hide file tree
Showing 7 changed files with 257 additions and 109 deletions.
21 changes: 21 additions & 0 deletions NetCasbin.UnitTest/EnforcerTest.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using NetCasbin.Extensions;
using NetCasbin.Persist;
using NetCasbin.Persist.FileAdapter;
using NetCasbin.Rbac;
using NetCasbin.UnitTest.Fixtures;
using NetCasbin.UnitTest.Mock;
using Xunit;
Expand Down Expand Up @@ -915,5 +917,24 @@ public void TestEnforceExApiLog()
}
#endif
#endregion

[Fact]
public void TestEnforceWithMultipleRoleManager()
{
var e = new Enforcer(TestModelFixture.GetNewTestModel(
_testModelFixture._rbacMultipleModelText,
_testModelFixture._rbacMultiplePolicyText));

var roleManager = new DefaultRoleManager(5);
roleManager.AddMatchingFunc((arg1, arg2) => arg1.Equals(arg2));
e.SetRoleManager(roleManager);
bool result = e.Enforce("@adm-user", "org::customer1", "cust1", "manage");
Assert.True(result);

roleManager.AddMatchingFunc((arg1, arg2) => !arg1.Equals(arg2));
e.SetRoleManager(roleManager);
result = e.Enforce("@adm-user", "org::customer1", "cust1", "manage");
Assert.False(result);
}
}
}
4 changes: 4 additions & 0 deletions NetCasbin.UnitTest/Fixtures/TestModelFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public class TestModelFixture
internal readonly string _rbacWithHierarchyWithDomainsPolicyText = ReadTestFile("rbac_with_hierarchy_with_domains_policy.csv");
internal readonly string _rbacWithResourceRolePolicyText = ReadTestFile("rbac_with_resource_roles_policy.csv");

// https://github.com/casbin/Casbin.NET/issues/154
internal readonly string _rbacMultipleModelText = ReadTestFile("rbac_multiple_rolemanager_model.conf");
internal readonly string _rbacMultiplePolicyText = ReadTestFile("rbac_multiple_rolemanager_policy.csv");

public Model.Model GetNewAbacModel()
{
return GetNewTestModel(_abacModelText);
Expand Down
16 changes: 16 additions & 0 deletions NetCasbin.UnitTest/examples/rbac_multiple_rolemanager_model.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[request_definition]
r = sub, dom, obj, act

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

[role_definition]
g = _, _, _ # users and role hierarcies
g2 = _, _ # organization hierarchies
g3 = _, _ # functional hierarchies

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

[matchers]
m = g(r.sub, p.sub, r.dom)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Policies

// role and functions to permissions
p, grp::system_admin, org::customer1, cust1, manage

//
// user => group mappings & group hierarchies
//
g, @adm-user, grp::system_admin, org::customer1
1 change: 1 addition & 0 deletions NetCasbin/CoreEnforcer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ public void SetWatcher(IWatcher watcher, bool useAsync = true)
public void SetRoleManager(IRoleManager roleManager)
{
SetRoleManager(PermConstants.DefaultRoleType, roleManager);
ExpressionHandler.SetGFunctions();
}

/// <summary>
Expand Down
Loading

0 comments on commit 49a8cb2

Please sign in to comment.