Skip to content

Commit

Permalink
fix: Support custom g type name (#334)
Browse files Browse the repository at this point in the history
* fix: Fix bug when first role_definition is not g

Signed-off-by: Tan <2912363476@qq.com>

* feat: Revise according to the suggestions

Signed-off-by: Tan <2912363476@qq.com>

* feat: Revise according to the suggestions(2)

Signed-off-by: Tan <2912363476@qq.com>

---------

Signed-off-by: Tan <2912363476@qq.com>
  • Loading branch information
Tanyuu authored Sep 28, 2023
1 parent cdbda9e commit 4ccef44
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
25 changes: 25 additions & 0 deletions Casbin.UnitTests/ModelTests/EnforcerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,31 @@ public void TestMultipleGroupTypeModelInMemory()
Assert.True(e.Enforce("alice", "data2", "write"));
}

[Fact]
public async Task TestNonGNamedMultipleGroupTypeModelInMemoryAsync()
{
IModel m = DefaultModel.Create();
m.AddDef("r", "r", "sub, obj, act");
m.AddDef("p", "p", "sub, obj, act");
m.AddDef("g", "firstGroup", "_, _");
m.AddDef("g", "secondGroup", "_, _");
m.AddDef("e", "e", "some(where (p.eft == allow))");
m.AddDef("m", "m", "firstGroup(r.sub, p.sub) && secondGroup(r.obj, p.obj) && r.act == p.act");

Enforcer e = new(m);
await e.AddPolicyAsync("alice", "data1", "read");
await e.AddPolicyAsync("bob", "data2", "write");
await e.AddPolicyAsync("data_group_admin", "data_group", "write");
await e.AddNamedGroupingPolicyAsync("firstGroup", "alice", "data_group_admin");
await e.AddNamedGroupingPolicyAsync("secondGroup", "data1", "data_group");
await e.AddNamedGroupingPolicyAsync("secondGroup", "data2", "data_group");

Assert.True(await e.EnforceAsync("alice", "data1", "read"));
Assert.True(await e.EnforceAsync("alice", "data1", "write"));
Assert.False(await e.EnforceAsync("alice", "data2", "read"));
Assert.True(await e.EnforceAsync("alice", "data2", "write"));
}

[Fact]
public async Task TestMultipleGroupTypeModelInMemoryAsync()
{
Expand Down
8 changes: 1 addition & 7 deletions Casbin/Model/DefaultSections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,7 @@ public bool AddSection(string section, string key, string value)

Dictionary<string, IReadOnlyAssertion> assertionMap = new() { [key] = assertion };

// TryAdd is not supported in .NET 4.5
if (_assertionsMap.ContainsKey(key))
{
return false;
}

_assertionsMap.Add(key, assertionMap);
_assertionsMap.Add(section, assertionMap);
return true;
}

Expand Down

0 comments on commit 4ccef44

Please sign in to comment.