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 Oct 23, 2022
1 parent 8a77971 commit 016a57c
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 1 deletion.
6 changes: 6 additions & 0 deletions Casbin.UnitTests/Casbin.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,12 @@
<None Update="Examples\rbac_in_operator_policy.csv">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Examples\abac_comment.conf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Examples\rbac_comment.conf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
Expand Down
3 changes: 3 additions & 0 deletions Casbin.UnitTests/Fixtures/TestModelFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public class TestModelFixture
internal readonly string _rbacMultipleEvalModelText = ReadTestFile("rbac_multiple_eval_model.conf");
internal readonly string _rbacMultipleEvalPolicyText = ReadTestFile("rbac_multiple_eval_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");
internal readonly string _rbacMultiplePolicyText = ReadTestFile("rbac_multiple_rolemanager_policy.csv");
Expand Down
24 changes: 24 additions & 0 deletions Casbin.UnitTests/ModelTests/ModelTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using Casbin.Model;
using Casbin.UnitTests.Fixtures;
using Casbin.UnitTests.Mock;
using Xunit;
Expand Down Expand Up @@ -600,6 +601,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.GetRequestAssertion("r").Tokens.Count);
Assert.Equal(2, model.Sections.GetRequestAssertion("r").Tokens["act"]);
Assert.Equal(3, model.Sections.GetPolicyAssertion("p").Tokens.Count);
Assert.Equal("some(where (p.eft == allow))", model.Sections.GetPolicyEffectAssertion("e").Value);
Assert.Equal("r.sub == p.sub && r.obj == p.obj && r.act == p.act", model.Sections.GetMatcherAssertion("m").Value);
}

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

public class TestResource
{
public TestResource(string name, string owner)
Expand Down
13 changes: 13 additions & 0 deletions Casbin.UnitTests/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.UnitTests/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
4 changes: 3 additions & 1 deletion Casbin/Config/DefaultConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,9 @@ private void ParseBuffer(TextReader reader)
}
string option = optionVal[0].Trim();
string value = optionVal[1].Trim();
AddConfig(section, option, value);
int commentStartIdx = value.IndexOf(PermConstants.PolicyCommentChar);
var processedValue = (commentStartIdx == -1 ? value : value.Remove(commentStartIdx)).Trim();
AddConfig(section, option, processedValue);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions Casbin/PermConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public static class PermConstants
public const char PolicySeparatorChar = ',';
public const string PolicySeparatorString = ", "; // include a white space
internal const string SubjectPrioritySeparatorString = "::";
public const char PolicyCommentChar = '#';

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

0 comments on commit 016a57c

Please # to comment.