Hi everyone,
I recently have designed a flow that grants 2-level access to sharepoint records. To give you context I will explain it with an example:
I have a list named UserManagement that has the following structure:

(It has columns like Name, Email, Experience, DHId (Delivery Head Id), ReportingManagerEmail) like:

[Most of the columns are of Single line text type].
So I had constructed a flow which looks like:

Basically the intention of creating the above flow was to grant user -> Manager (RM) -> Manager's RM access so that what you see at the data source level is what you get in the application (without requiring any additional expression to manage it at application level using Filter expression).
Working of the flow:
-> Using Get Items I am retrieving all the records from the UserManagement list.
-> Then a check is (Condition action) been done to see if the record (as it is in loop: so it will take access all records one-by-one) ReportingManagerEmail column is empty or not (that means a given user does he/she have a Reporting Manager or not)
a) If the given user (record) has Reporting Manager (i.e. ReportingManagerEmail field is not empty/null):
-> Here first I am cutting off all the access for all users except (the Owners group of Sharepoint: They will be having the full control; So they will be able to see all the records this custom permission flow won't have any impact on them)
-> Then using the Grant access action: the record is being granted access for the user (Email), ReportingManagerEmail

Now to grant 2nd level access i.e. Reporting Manager's manager
[Assume Rahul (employee) -> Rohan (Reporting manager of Rahul) -> Sam (RM of Rohan)
So now all the actions below this are to give Rohan's RM i.e. Sam the access if his record is there in list]

In the Get Items 2 action I am retreiving RM's manager manager (in my example Rohan's RM is Sam) details and then granting the access (before that performing a check whether RM's manager manager is not empty: If it's not empty then proceed to grant access else do nothing.
b) If User has no reporting manager: (Condition 1: Returns null)
-> Then in this case I am just granting access only to the user. {This can be the case for Freshers}
As of now I have provided 2 level of access, but this won't be the case for all records like some might 2 levels, 3 levels, 4 levels.
For example:
Assume Shubham is a manager under him there are 3 individuals : Sushant, Radha, John. Out of these 3 users John has 2 reportee's (that report to him). So now Shubham should have access to 3 individuals that directly report to him (that is in ReportingManagerEmail column shubham's email will be present) and also he should be able to access John's team data as well.
And Shubham's manager is Samuel, so he should be able to access Shubham's record as well as the team that works under him and their team data as well.
So we cannot be sure how many levels we would have so I wanted to know how to achieve this dynamic behavior (currently I have designed my flow for 2/3 levels) even if I keep adding more apply to each as I am not sure how many levels, it would be difficult to track (current flow design is from bottom to up).
So I had a rough idea:
Suppose we get Samuels record we first check if he is RM for any of the user in our list or not:
If he is then we initialize a list: Add all the members in it (his members in the list)
[Lets say 2 users report to Samuel: Jim, Ryan]
Then check if the members if they are Reporting manager for any of the other user if there is anyone who is RM, then for that individual create a list and all the members that report to him/her [Jim is RM: Sam]. Similarly check for the rest of them (i.e. Ryan)
And now Samuel add the sub-reportee's info (email) into Samuel's list) and now Samuel will have access to Jim,Ryan and Sam.
This is just rough idea but I am not sure how to implement that, whether it will be feasible or not. In the above approach will the flow size increase i.e. number of actions (then present structure). So if you guys have any idea about on how to approach this problem please let me know it will be quite helpful.
Regards,
Sidhant.