I'm working on a Django 1.5 Project.
The application contains the user hierarchy of
Owner
Admin
Viewer
- Owner owns all the data in the database.
- Admin users are created by the owner and have different login credentials. Can add/edit/delete data under Owner.
- Viewer users are created by the owner and have different login credentials. Can view data created under the Owner.
Users model have a field is_shared (boolean) to identify main user and shared user and access_level is mapped in another model MultiUser.
I have thought of to implement it in the way
- Update all viewsets and put check in
get_queryset. - where first check will be made if user has
is_shared=True - then check in MultiUser model,
- get the
userfrom MultiUser and then filter records on behalf of the user.
But this will require changes throughout the application viewsets.
Is there a way to do this without making changes to the whole application.
May be middleware.
Edit 2: MultiUser model
class MultiUser(models.Model):
user = models.ForeignKey(User, related_name='owner_user')
shared_user = models.ForeignKey(User, related_name='shared_user')
access_level = models.CharField(max_length=50)
I solved this by creating a middleware with content
This middleware check for if the user's account is a shared user based on
is_sharedand then change the request.user with the owner user object. This also prevents changing the request owner for specific paths defined in the settings file (Ex., profile, change_password, etc).