Skip to content

fix: Enable the comment in model config files. #262

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Oct 25, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix: Enable the comment in model config files.
  • Loading branch information
AsakusaRinne committed Oct 23, 2022
commit 016a57c7c8fb4f4d1265ea732f583c1cf93851aa
6 changes: 6 additions & 0 deletions Casbin.UnitTests/Casbin.UnitTests.csproj
Original file line number Diff line number Diff line change
@@ -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>
3 changes: 3 additions & 0 deletions Casbin.UnitTests/Fixtures/TestModelFixture.cs
Original file line number Diff line number Diff line change
@@ -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");
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;
@@ -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)
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
@@ -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);
}
}
}
1 change: 1 addition & 0 deletions Casbin/PermConstants.cs
Original file line number Diff line number Diff line change
@@ -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";