From 016a57c7c8fb4f4d1265ea732f583c1cf93851aa Mon Sep 17 00:00:00 2001 From: AsakusaRinne Date: Mon, 24 Oct 2022 01:41:20 +0800 Subject: [PATCH] fix: Enable the comment in model config files. --- Casbin.UnitTests/Casbin.UnitTests.csproj | 6 +++++ Casbin.UnitTests/Fixtures/TestModelFixture.cs | 3 +++ Casbin.UnitTests/ModelTests/ModelTest.cs | 24 +++++++++++++++++++ Casbin.UnitTests/examples/abac_comment.conf | 13 ++++++++++ Casbin.UnitTests/examples/rbac_comment.conf | 16 +++++++++++++ Casbin/Config/DefaultConfig.cs | 4 +++- Casbin/PermConstants.cs | 1 + 7 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 Casbin.UnitTests/examples/abac_comment.conf create mode 100644 Casbin.UnitTests/examples/rbac_comment.conf diff --git a/Casbin.UnitTests/Casbin.UnitTests.csproj b/Casbin.UnitTests/Casbin.UnitTests.csproj index 7b7663bf..c53e92d0 100644 --- a/Casbin.UnitTests/Casbin.UnitTests.csproj +++ b/Casbin.UnitTests/Casbin.UnitTests.csproj @@ -201,6 +201,12 @@ PreserveNewest + + PreserveNewest + + + PreserveNewest + diff --git a/Casbin.UnitTests/Fixtures/TestModelFixture.cs b/Casbin.UnitTests/Fixtures/TestModelFixture.cs index 7a4ffd17..4b9af0df 100644 --- a/Casbin.UnitTests/Fixtures/TestModelFixture.cs +++ b/Casbin.UnitTests/Fixtures/TestModelFixture.cs @@ -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"); diff --git a/Casbin.UnitTests/ModelTests/ModelTest.cs b/Casbin.UnitTests/ModelTests/ModelTest.cs index b8f2b361..c57921ff 100644 --- a/Casbin.UnitTests/ModelTests/ModelTest.cs +++ b/Casbin.UnitTests/ModelTests/ModelTest.cs @@ -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) diff --git a/Casbin.UnitTests/examples/abac_comment.conf b/Casbin.UnitTests/examples/abac_comment.conf new file mode 100644 index 00000000..b4f46261 --- /dev/null +++ b/Casbin.UnitTests/examples/abac_comment.conf @@ -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 diff --git a/Casbin.UnitTests/examples/rbac_comment.conf b/Casbin.UnitTests/examples/rbac_comment.conf new file mode 100644 index 00000000..3c1d47ee --- /dev/null +++ b/Casbin.UnitTests/examples/rbac_comment.conf @@ -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 diff --git a/Casbin/Config/DefaultConfig.cs b/Casbin/Config/DefaultConfig.cs index db27ab11..7f57de77 100644 --- a/Casbin/Config/DefaultConfig.cs +++ b/Casbin/Config/DefaultConfig.cs @@ -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); } } } diff --git a/Casbin/PermConstants.cs b/Casbin/PermConstants.cs index 33bf5f0f..c75e75d2 100644 --- a/Casbin/PermConstants.cs +++ b/Casbin/PermConstants.cs @@ -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";