-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
249 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace casbinet.Rbac | ||
{ | ||
public class GroupRoleManager : DefaultRoleManager | ||
{ | ||
public GroupRoleManager(int maxHierarchyLevel) : base(maxHierarchyLevel) | ||
{ | ||
} | ||
|
||
public override bool HasLink(string roleName1, string roleName2, string domain) | ||
{ | ||
if (base.HasLink(roleName1, roleName2, domain)) | ||
{ | ||
return true; | ||
} | ||
|
||
try | ||
{ | ||
List<string> roles = base.GetRoles(roleName1); | ||
List<string> groups = roles.Count > 0 ? base.GetRoles(roleName1) : new List<string>(); | ||
foreach (string group in groups) | ||
{ | ||
if (this.HasLink(group, roleName2, domain)) | ||
{ | ||
return true; | ||
} | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
return false; | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace casbinet.Rbac | ||
{ | ||
|
||
public interface IDomainsRoleManager : IRoleManager | ||
{ | ||
void AddLink(string roleOrUser, string role, params object[] domain); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,112 @@ | ||
namespace casbinet.rbac | ||
namespace casbinet.Rbac | ||
{ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
public class Role | ||
{ | ||
private string name; | ||
|
||
private List<Role> roles = new List<Role>(); | ||
private Dictionary<string, Role> roles = new Dictionary<string, Role>(); | ||
|
||
public Role(string name) | ||
{ | ||
this.name = name; | ||
} | ||
|
||
public string Name | ||
{ | ||
get => this.name; | ||
|
||
set | ||
{ | ||
this.name = value; | ||
} | ||
} | ||
|
||
public void AddRole(Role role) | ||
{ | ||
foreach (Role role in roles) | ||
if (this.roles.ContainsKey(role.name)) | ||
{ | ||
return; | ||
} | ||
|
||
this.roles.Add(role.name, role); | ||
} | ||
|
||
public void DeleteRole(Role role) | ||
{ | ||
if (this.roles.ContainsKey(role.name)) | ||
{ | ||
this.roles.Remove(role.name); | ||
} | ||
} | ||
|
||
public bool HasRole(string roleName, int hierarchyLevel) | ||
{ | ||
if (this.name == roleName) | ||
{ | ||
return true; | ||
} | ||
|
||
if (hierarchyLevel <= 0) | ||
{ | ||
return false; | ||
} | ||
|
||
foreach (Role role in this.roles.Values.ToArray()) | ||
{ | ||
if (role.HasRole(roleName, hierarchyLevel - 1)) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public bool HasDirectRole(string roleName) | ||
{ | ||
foreach (Role role in this.roles.Values.ToArray()) | ||
{ | ||
|
||
if (role.name == roleName) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public List<string> GetRoles() | ||
{ | ||
List<string> names = new List<string>(); | ||
foreach (Role role in this.roles.Values.ToArray()) | ||
{ | ||
names.Add(role.name); | ||
} | ||
|
||
return names; | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
Role[] allRoles = this.roles.Values.ToArray(); | ||
StringBuilder names = new StringBuilder(allRoles[0].name); | ||
for (int i = 0; i < allRoles.Length; i++) | ||
{ | ||
if (i == 0) | ||
{ | ||
names.Append(allRoles[i].name); | ||
} | ||
else | ||
{ | ||
names.Append(", " + allRoles[i].name); | ||
} | ||
} | ||
|
||
return this.name + " < " + names; | ||
} | ||
} | ||
} |