Skip to content

Commit

Permalink
fix: Enable the comment in model config files.
Browse files Browse the repository at this point in the history
  • Loading branch information
AsakusaRinne committed Jun 25, 2022
1 parent 820900f commit d1783ca
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 2 deletions.
8 changes: 7 additions & 1 deletion Casbin.UnitTest/Casbin.UnitTests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net7.0;net6.0;net5.0;netcoreapp3.1;net461;net452</TargetFrameworks>
<TargetFrameworks>net6.0;net5.0;netcoreapp3.1;net461;net452</TargetFrameworks>
<DebugType>full</DebugType>
<IsPackable>false</IsPackable>
<LangVersion>10.0</LangVersion>
Expand Down Expand Up @@ -177,6 +177,12 @@
<None Update="Examples\rbac_with_resource_roles_model.conf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Examples\abac_comment.conf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Examples\rbac_comment.conf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Examples\rbac_with_resource_roles_policy.csv">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
2 changes: 2 additions & 0 deletions Casbin.UnitTest/Fixtures/TestModelFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class TestModelFixture
internal readonly string _rbacWithHierarchyPolicyText = ReadTestFile("rbac_with_hierarchy_policy.csv");
internal readonly string _rbacWithHierarchyWithDomainsPolicyText = ReadTestFile("rbac_with_hierarchy_with_domains_policy.csv");
internal readonly string _rbacWithResourceRolePolicyText = ReadTestFile("rbac_with_resource_roles_policy.csv");
internal readonly string _abacCommentText = ReadTestFile("abac_comment.conf");
internal readonly string _rbacCommentText = ReadTestFile("rbac_comment.conf");

// https://github.com/casbin/Casbin.NET/issues/154
internal readonly string _rbacMultipleModelText = ReadTestFile("rbac_multiple_rolemanager_model.conf");
Expand Down
23 changes: 23 additions & 0 deletions Casbin.UnitTest/ModelTests/ModelTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,29 @@ public void TestMultipleTypeModel()
Assert.False(e.Enforce(context, new { Age = 70 }, "data2", "read"));
}

[Fact]
public void TestAbacComment()
{
var model = TestModelFixture.GetNewTestModel(_testModelFixture._abacCommentText);
Assert.Equal(3, model.Sections["r"]["r"].Tokens.Count);
Assert.Equal(2, model.Sections["r"]["r"].Tokens["act"]);
Assert.Equal(3, model.Sections["p"]["p"].Tokens.Count);
Assert.Equal("some(where (p.eft == allow))", model.Sections["e"]["e"].Value);
Assert.Equal("r.sub == p.sub && r.obj == p.obj && r.act == p.act", model.Sections["m"]["m"].Value);
}

[Fact]
public void TestRbacComment()
{
var model = TestModelFixture.GetNewTestModel(_testModelFixture._rbacCommentText);
Assert.Equal(3, model.Sections["r"]["r"].Tokens.Count);
Assert.Equal(2, model.Sections["r"]["r"].Tokens["act"]);
Assert.Equal(3, model.Sections["p"]["p"].Tokens.Count);
Assert.Equal("_, _", model.Sections["g"]["g"].Value);
Assert.Equal("some(where (p.eft == allow))", model.Sections["e"]["e"].Value);
Assert.Equal("g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act", model.Sections["m"]["m"].Value);
}

public class TestResource
{
public TestResource(string name, string owner)
Expand Down
13 changes: 13 additions & 0 deletions Casbin.UnitTest/examples/abac_comment.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[request_definition]
r = sub, obj, act ### The following text is a test for comment in model config.

[policy_definition]
p = sub, obj, act #This is an inline comment, splited by a comma

[policy_effect]
# This is a comment, splited by a comma
e = some(where (p.eft == allow)) #This is an inline comment, splited by a comma

#This is a comment, splited by a comma
[matchers]
m = r.sub == p.sub && r.obj == p.obj && r.act == p.act # Comment, another comment
16 changes: 16 additions & 0 deletions Casbin.UnitTest/examples/rbac_comment.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[request_definition]
r = sub, obj, act #This is an inline comment, splited by a comma

[policy_definition]
p = sub, obj, act #This is an inline comment, splited by a comma

[role_definition]
# This is a comment, splited by a comma
g = _, _ # This is an inline comment, splited by a comma

# This is a comment, splited by a comma
[policy_effect]
e = some(where (p.eft == allow)) #This is an inline comment, splited by a comma

[matchers]
m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act # This is an inline comment, splited by a comma
3 changes: 2 additions & 1 deletion Casbin/Model/DefaultModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,11 @@ public bool AddDef(string section, string key, string value)
return false;
}

int commentStartIdx = value.IndexOf(PermConstants.PolicyCommentChar);
var assertion = new Assertion
{
Key = key,
Value = value
Value = (commentStartIdx == -1 ? value : value.Remove(commentStartIdx)).Trim()
};

if (section.Equals(PermConstants.Section.RequestSection)
Expand Down
1 change: 1 addition & 0 deletions Casbin/PermConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public static class PermConstants
{
public const char PolicySeparatorChar = ',';
public const string PolicySeparatorString = ", "; // include a white space
public const char PolicyCommentChar = '#';

public const string DefaultRequestType = "r";
public const string RequestType2 = "r2";
Expand Down

0 comments on commit d1783ca

Please # to comment.