From 305ed9501c6a9ae7397a13ba117df9fdb2961019 Mon Sep 17 00:00:00 2001 From: AsakusaRinne Date: Sun, 19 Jun 2022 00:52:08 +0800 Subject: [PATCH] fix: Enable the comment in model config files. --- Casbin.UnitTest/Casbin.UnitTests.csproj | 8 ++++++- Casbin.UnitTest/Fixtures/TestModelFixture.cs | 2 ++ Casbin.UnitTest/ModelTests/ModelTest.cs | 23 ++++++++++++++++++++ Casbin.UnitTest/examples/abac_comment.conf | 13 +++++++++++ Casbin.UnitTest/examples/rbac_comment.conf | 16 ++++++++++++++ Casbin/Model/DefaultModel.cs | 3 ++- Casbin/PermConstants.cs | 1 + 7 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 Casbin.UnitTest/examples/abac_comment.conf create mode 100644 Casbin.UnitTest/examples/rbac_comment.conf diff --git a/Casbin.UnitTest/Casbin.UnitTests.csproj b/Casbin.UnitTest/Casbin.UnitTests.csproj index 7d57750c..c8ea39b5 100644 --- a/Casbin.UnitTest/Casbin.UnitTests.csproj +++ b/Casbin.UnitTest/Casbin.UnitTests.csproj @@ -1,7 +1,7 @@ - net7.0;net6.0;net5.0;netcoreapp3.1;net461;net452 + net6.0;net5.0;netcoreapp3.1;net461;net452 full false 10.0 @@ -177,6 +177,12 @@ PreserveNewest + + PreserveNewest + + + PreserveNewest + PreserveNewest diff --git a/Casbin.UnitTest/Fixtures/TestModelFixture.cs b/Casbin.UnitTest/Fixtures/TestModelFixture.cs index bf975ac4..ee474af3 100644 --- a/Casbin.UnitTest/Fixtures/TestModelFixture.cs +++ b/Casbin.UnitTest/Fixtures/TestModelFixture.cs @@ -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"); diff --git a/Casbin.UnitTest/ModelTests/ModelTest.cs b/Casbin.UnitTest/ModelTests/ModelTest.cs index 8c5f1c7b..7673fd1c 100644 --- a/Casbin.UnitTest/ModelTests/ModelTest.cs +++ b/Casbin.UnitTest/ModelTests/ModelTest.cs @@ -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) diff --git a/Casbin.UnitTest/examples/abac_comment.conf b/Casbin.UnitTest/examples/abac_comment.conf new file mode 100644 index 00000000..1b4e6e83 --- /dev/null +++ b/Casbin.UnitTest/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.UnitTest/examples/rbac_comment.conf b/Casbin.UnitTest/examples/rbac_comment.conf new file mode 100644 index 00000000..3c1d47ee --- /dev/null +++ b/Casbin.UnitTest/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/Model/DefaultModel.cs b/Casbin/Model/DefaultModel.cs index c59e9289..90a3bd73 100644 --- a/Casbin/Model/DefaultModel.cs +++ b/Casbin/Model/DefaultModel.cs @@ -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) diff --git a/Casbin/PermConstants.cs b/Casbin/PermConstants.cs index f86bc1be..d7ac9499 100644 --- a/Casbin/PermConstants.cs +++ b/Casbin/PermConstants.cs @@ -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";