Skip to content

Commit 6be2f80

Browse files
authored
feat: support concurrent roles (#363)
Signed-off-by: Taoyuesong <634774653@qq.com>
1 parent 91904e9 commit 6be2f80

File tree

3 files changed

+12
-30
lines changed

3 files changed

+12
-30
lines changed

Casbin.UnitTests/ModelTests/RbacApiTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,8 +423,8 @@ public void TestGetImplicitPermissionsForUserWithDomain()
423423

424424
TestGetImplicitPermissions(e, "alice", AsList(
425425
AsList("alice", "domain1", "data2", "read"),
426-
AsList("role:reader", "domain1", "data1", "read"),
427-
AsList("role:writer", "domain1", "data1", "write")),
426+
AsList("role:writer", "domain1", "data1", "write"),
427+
AsList("role:reader", "domain1", "data1", "read")),
428428
"domain1");
429429
}
430430

Casbin.UnitTests/Util/TestUtil.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,12 +210,11 @@ internal static void TestGetPermissions(IEnforcer e, string name, List<List<stri
210210
Assert.True(res.DeepEquals(myRes), message);
211211
}
212212

213-
internal static void TestGetImplicitPermissions(IEnforcer e, string name, List<List<string>> res,
213+
internal static void TestGetImplicitPermissions(IEnforcer e, string name, List<List<string>> except,
214214
string domain = null)
215215
{
216-
IEnumerable<IEnumerable<string>> myRes = e.GetImplicitPermissionsForUser(name, domain);
217-
string message = "Implicit permissions for " + name + ": " + myRes + ", supposed to be " + res;
218-
Assert.True(res.DeepEquals(myRes), message);
216+
IEnumerable<IEnumerable<string>> actual = e.GetImplicitPermissionsForUser(name, domain);
217+
Assert.True(except.DeepEquals(actual));
219218
}
220219

221220
internal static void TestHasPermission(IEnforcer e, string name, List<string> permission, bool res)

Casbin/Rbac/Role.cs

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Concurrent;
23
using System.Collections.Generic;
34
using System.Linq;
45

@@ -9,7 +10,7 @@ namespace Casbin.Rbac
910
/// </summary>
1011
public class Role
1112
{
12-
private readonly Lazy<Dictionary<string, Role>> _roles = new();
13+
private readonly Lazy<ConcurrentDictionary<string, Role>> _roles = new();
1314

1415
public Role(string name)
1516
{
@@ -28,17 +29,7 @@ public Role(string name, string domain)
2829

2930
public void AddRole(Role role)
3031
{
31-
if (_roles.IsValueCreated is false)
32-
{
33-
_roles.Value.Add(role.Name, role);
34-
}
35-
36-
if (_roles.Value.ContainsKey(role.Name))
37-
{
38-
return;
39-
}
40-
41-
_roles.Value.Add(role.Name, role);
32+
_roles.Value.TryAdd(role.Name, role);
4233
}
4334

4435
public void DeleteRole(Role role)
@@ -47,11 +38,7 @@ public void DeleteRole(Role role)
4738
{
4839
return;
4940
}
50-
51-
if (_roles.Value.ContainsKey(role.Name))
52-
{
53-
_roles.Value.Remove(role.Name);
54-
}
41+
_roles.Value.TryRemove(role.Name, out _);
5542
}
5643

5744
public bool HasRole(string name, int hierarchyLevel, Func<string, string, bool> matchingFunc = null)
@@ -71,7 +58,8 @@ public bool HasRole(string name, int hierarchyLevel, Func<string, string, bool>
7158
return false;
7259
}
7360

74-
return _roles.Value.Values.Any(role => role.HasRole(name, hierarchyLevel - 1));
61+
return _roles.Value.Values.Any(role =>
62+
role.HasRole(name, hierarchyLevel - 1));
7563
}
7664

7765
public bool HasDirectRole(string name, Func<string, string, bool> matchingFunc = null)
@@ -86,7 +74,7 @@ public bool HasDirectRole(string name, Func<string, string, bool> matchingFunc =
8674
return _roles.Value.ContainsKey(name);
8775
}
8876

89-
77+
9078
foreach (var role in _roles.Value.Values)
9179
{
9280
if (name == role.Name || matchingFunc(role.Name, name) && role.Name != name)
@@ -101,10 +89,5 @@ public IEnumerable<string> GetRoles()
10189
{
10290
return _roles.IsValueCreated ? _roles.Value.Keys : Enumerable.Empty<string>();
10391
}
104-
105-
public override string ToString()
106-
{
107-
return $"{Name}{string.Join(",", _roles.Value)}";
108-
}
10992
}
11093
}

0 commit comments

Comments
 (0)