How can I write the access control algorithm or helper classfor below conditions.
I have following design:
- Table Customer (Id, Name...)
- Table User (Id, Username, Name, Manager (UserId - from this User Table))
- Table UserCustomer (UserId, CustomerId)
Only User or User's Manager assigned to Customers can see the Customer.
It should be like this.
User1 has Manager UserM1
User2 has Manager UserM2
User3 has Manager UserM3
UserM1 has Manager UserMM1
UserM2,M3 has Manager UserMM2
UserMM1, UserMM2 has Manager UserMMM
UserMMM can have its Manager as well and so and so.
Note: Recursive relationship of User and User's Manager could be deep like 10 levels.
So technically, any customers assigned to the User under the User Manager can see the client.
How can I write this dynamic condition in C#.
======= Additonal explaination ========
I have 10 clients = I can only view 10 clients
Pogba have 5 clients = He can view 5 clients.
Paul is my manager and he also has 5 clients = He can view 15 clients
Logan is Pual Manager and he also has 10 clients = He can view 25 clients (Me, Paul, and Logan clients)
Henry is Pogba and Logan Manager = He can view 30 clients (25 client from Logan and Logan's staff + 5 clients from Pogba )
so and so.
My attempted alogoritm but it could only accomulate 2 level deep.
Im going to create helper class. (CustomerAccessControlHelper.cs)
public static class CustomerAccessControlHelper.cs
{
public static List<Customer> GetAccessClients(int userId)
{
var userListsUnderCurrentUser = new List<User>();
var listOfAssignedStaffsLevel1 = _context.Users.Where(x => x.ManagerId == userId);
userListsUnderCurrentUser.AddRange(listOfAssignedStaffsLevel1);
foreach(var user in listOfAssignedStaffs)
{
var listOfAssignedStaffsLevel2 = _context.Users.Where(x => x.ManagerId == user.Id);
userListsUnderCurrentUser.AddRange(listOfAssignedStaffsLevel1);
}
}
}