Skip to content

Commit

Permalink
Merge pull request #164 from Sagilio/fix#106_re
Browse files Browse the repository at this point in the history
fix: Unable replace multiple eval
  • Loading branch information
hsluoyz authored Jun 7, 2021
2 parents 7a720f0 + 8d6c0ee commit d71d87b
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 12 deletions.
16 changes: 16 additions & 0 deletions NetCasbin.UnitTest/EnforcerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -936,5 +936,21 @@ public void TestEnforceWithMultipleRoleManager()
result = e.Enforce("@adm-user", "org::customer1", "cust1", "manage");
Assert.False(result);
}

[Fact]
public void TestEnforceWithMultipleEval()
{
var e = new Enforcer(TestModelFixture.GetNewTestModel(
_testModelFixture._rbacMultipleEvalModelText,
_testModelFixture._rbacMultipleEvalPolicyText));

bool result = e.Enforce(
"domain1",
new { Role = "admin" },
new { Name = "admin_panel" },
"view");

Assert.True(result);
}
}
}
5 changes: 5 additions & 0 deletions NetCasbin.UnitTest/Fixtures/TestModelFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public class TestModelFixture
internal readonly string _rbacMultipleModelText = ReadTestFile("rbac_multiple_rolemanager_model.conf");
internal readonly string _rbacMultiplePolicyText = ReadTestFile("rbac_multiple_rolemanager_policy.csv");

// https://github.com/casbin/Casbin.NET/issues/106
internal readonly string _rbacMultipleEvalModelText = ReadTestFile("rbac_multiple_eval_model.conf");
internal readonly string _rbacMultipleEvalPolicyText = ReadTestFile("rbac_multiple_eval_policy.csv");


public Model.Model GetNewAbacModel()
{
return GetNewTestModel(_abacModelText);
Expand Down
6 changes: 6 additions & 0 deletions NetCasbin.UnitTest/NetCasbin.UnitTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@
<None Update="examples\rbac_muitiple_rolemanager_model.csv">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="examples\rbac_multiple_eval_model.conf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="examples\rbac_multiple_eval_policy.csv">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="examples\rbac_multiple_rolemanager_model.conf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
11 changes: 11 additions & 0 deletions NetCasbin.UnitTest/examples/rbac_multiple_eval_model.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[request_definition]
r = dom, sub, obj, act

[policy_definition]
p = dom, sub_rule, obj_rule, act

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

[matchers]
m = r.dom == p.dom && eval(p.sub_rule) && eval(p.obj_rule) && r.act == p.act
1 change: 1 addition & 0 deletions NetCasbin.UnitTest/examples/rbac_multiple_eval_policy.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
p, domain1, r.sub.Role == "admin", r.obj.Name == "admin_panel", view
28 changes: 16 additions & 12 deletions NetCasbin/Util/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ internal static void LogPrint(string v)

private static readonly Regex s_evalRegex = new Regex(@"\beval\((?<rule>[^)]*)\)");


/// <summary>
/// Determines whether matcher contains eval function
/// </summary>
Expand All @@ -182,21 +181,26 @@ internal static bool HasEval(string expressString)
/// <returns></returns>
internal static bool TryGetEvalRuleNames(string expressString, out IEnumerable<string> evalRuleNames)
{
var match = s_evalRegex.Match(expressString);
var group = match.Groups;
int subMatchCount = group.Count - 1;
if (subMatchCount > 0)
MatchCollection matches = s_evalRegex.Matches(expressString);
int matchCount = matches.Count;
if (matchCount is 0)
{
evalRuleNames = null;
return false;
}
string[] rules = new string[matchCount];
for (int i = 0; i < matchCount; i++)
{
var rules = new string[subMatchCount];
for (int i = 0; i < subMatchCount; i++)
GroupCollection group = matches[i].Groups;
if (group.Count < 2)
{
rules[i] = group[i + 1].Value;
evalRuleNames = null;
return false;
}
evalRuleNames = rules;
return true;
rules[i] = group[1].Value;
}
evalRuleNames = null;
return false;
evalRuleNames = rules;
return true;
}

/// <summary>
Expand Down

0 comments on commit d71d87b

Please sign in to comment.