I want to have hierarchical level permissions in my Django Applications.
For example:- Consider there are 4 levels -
Admin
Sub-admin(CountryLevel(CL) Admin)
sub-sub-admin(StateLevel(SL) Admin)
And then normal Users(U).
Admins will create CL, in return CL will create SL, and SL will finally onboard the users. The end goal is to onboard the users.
Admins have the access to apply CRUD operation on any user. CL should have access to those users(objects) which were onboarded(created) by the CL created SLs. In return, SL should have access to only those users(objects) onboarded(created) by him. Also, a user can get himself registered directly without any admins involved. In such case, the user shall have access to his own application.
How can I achieve such tree-level like permissions?
The solution that I can think of is (but not sure about it):-
I've updated auth_group
table and added parent_id
into it. Following is the schema.
id, group_name, parent_id
The significance of parent_id
is to create a tree-like structure of the groups. The number of groups created is equal to the height of the tree. For eg consider following structure:-
id, group_name, parent_id
1 , admin, 0
2, CL, 1
3, SL, 2
Now when any user is created(onboarded), I'll assign an SL group_id
, which will be added to auth_user_groups
table.
Now, I want to ask the following questions
1) How do I manage hierarchy-level permission of the groups, ie SL should only be able to access users onboarded by him, CL should be able to access all the users onboarded by the SLs created by him and so on.
2) (Not sure I'm thinking correctly or not) Now, since I've assigned a group SL to all the users onboarded by the SL, will the user be able to access each other information, being part of the same group?
3) For the users who are onboarde directly(ie not via any SL), I shall not be mapping them to any group. Right?