Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display users list based on user access limitation #1742

Merged
merged 8 commits into from
May 15, 2024
32 changes: 32 additions & 0 deletions care/users/api/viewsets/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
UserListSerializer,
UserSerializer,
)
from care.utils.cache.cache_allowed_facilities import get_accessible_facilities

User = get_user_model()

Expand Down Expand Up @@ -122,6 +123,37 @@ class UserViewSet(
# DRYPermissions(),
# ]

def get_queryset(self):
if self.request.user.is_superuser:
return self.queryset
if (
self.request.user.user_type == User.TYPE_VALUE_MAP["StateAdmin"]
or self.request.user.user_type == User.TYPE_VALUE_MAP["StateReadOnlyAdmin"]
):
return self.queryset.filter(
state=self.request.user.state,
user_type__lt=User.TYPE_VALUE_MAP["StateAdmin"],
is_superuser=False,
)
if (
self.request.user.user_type == User.TYPE_VALUE_MAP["DistrictAdmin"]
or self.request.user.user_type
== User.TYPE_VALUE_MAP["DistrictReadOnlyAdmin"]
):
return self.queryset.filter(
district=self.request.user.district,
user_type__lt=User.TYPE_VALUE_MAP["DistrictAdmin"],
is_superuser=False,
)
else:
return self.queryset.filter(
facilityuser__facility_id__in=get_accessible_facilities(
self.request.user
),
user_type__lt=User.TYPE_VALUE_MAP["DistrictAdmin"],
is_superuser=False,
).distinct()
sainak marked this conversation as resolved.
Show resolved Hide resolved

def get_serializer_class(self):
if self.action == "list":
return UserListSerializer
Expand Down
Loading