Skip to content

Commit

Permalink
feat: support concurrent roles (#363)
Browse files Browse the repository at this point in the history
Signed-off-by: Taoyuesong <634774653@qq.com>
  • Loading branch information
Taoyuesong authored Aug 20, 2024
1 parent 91904e9 commit 6be2f80
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 30 deletions.
4 changes: 2 additions & 2 deletions Casbin.UnitTests/ModelTests/RbacApiTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,8 @@ public void TestGetImplicitPermissionsForUserWithDomain()

TestGetImplicitPermissions(e, "alice", AsList(
AsList("alice", "domain1", "data2", "read"),
AsList("role:reader", "domain1", "data1", "read"),
AsList("role:writer", "domain1", "data1", "write")),
AsList("role:writer", "domain1", "data1", "write"),
AsList("role:reader", "domain1", "data1", "read")),
"domain1");
}

Expand Down
7 changes: 3 additions & 4 deletions Casbin.UnitTests/Util/TestUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,11 @@ internal static void TestGetPermissions(IEnforcer e, string name, List<List<stri
Assert.True(res.DeepEquals(myRes), message);
}

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

internal static void TestHasPermission(IEnforcer e, string name, List<string> permission, bool res)
Expand Down
31 changes: 7 additions & 24 deletions Casbin/Rbac/Role.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;

Expand All @@ -9,7 +10,7 @@ namespace Casbin.Rbac
/// </summary>
public class Role
{
private readonly Lazy<Dictionary<string, Role>> _roles = new();
private readonly Lazy<ConcurrentDictionary<string, Role>> _roles = new();

public Role(string name)
{
Expand All @@ -28,17 +29,7 @@ public Role(string name, string domain)

public void AddRole(Role role)
{
if (_roles.IsValueCreated is false)
{
_roles.Value.Add(role.Name, role);
}

if (_roles.Value.ContainsKey(role.Name))
{
return;
}

_roles.Value.Add(role.Name, role);
_roles.Value.TryAdd(role.Name, role);
}

public void DeleteRole(Role role)
Expand All @@ -47,11 +38,7 @@ public void DeleteRole(Role role)
{
return;
}

if (_roles.Value.ContainsKey(role.Name))
{
_roles.Value.Remove(role.Name);
}
_roles.Value.TryRemove(role.Name, out _);
}

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

return _roles.Value.Values.Any(role => role.HasRole(name, hierarchyLevel - 1));
return _roles.Value.Values.Any(role =>
role.HasRole(name, hierarchyLevel - 1));
}

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


foreach (var role in _roles.Value.Values)
{
if (name == role.Name || matchingFunc(role.Name, name) && role.Name != name)
Expand All @@ -101,10 +89,5 @@ public IEnumerable<string> GetRoles()
{
return _roles.IsValueCreated ? _roles.Value.Keys : Enumerable.Empty<string>();
}

public override string ToString()
{
return $"{Name}{string.Join(",", _roles.Value)}";
}
}
}

0 comments on commit 6be2f80

Please sign in to comment.