Skip to content

Commit

Permalink
Modify Patient Access Control; Add Home Facility to List Serializer (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
gigincg authored Aug 2, 2022
1 parent 76481f8 commit ab3e690
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
19 changes: 19 additions & 0 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,25 @@ Using Docker
.. _`docker`: https://docs.docker.get-started/
.. _`docker-compose`: https://docs.docker.com/compose

Local Setup
~~~~~~~~~~~~
Make sure that Postgres is Installed.

Run `pip install -r local/requirements.txt`

Troubleshooting Local Setup
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If you're on Mac and you have installed Postgres.app Run:
`export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/14/bin`

If you're pip install is failing on Pillow Consider installing

`brew install libjpeg libtiff little-cms2 openjpeg webp`

and then:

`brew install freetype harfbuzz fribidi`

Running the server
``````````````````
Expand Down
3 changes: 2 additions & 1 deletion care/facility/models/mixins/permissions/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ def has_object_write_permission(self, request):
return request.user.is_superuser or (
(hasattr(self, "created_by") and request.user == self.created_by)
or (doctor_allowed)
or (self.facility and request.user in self.facility.users.all())
# or (self.facility and request.user in self.facility.users.all())
or (self.facility == request.user.home_facility)
or (
request.user.user_type >= User.TYPE_VALUE_MAP["DistrictLabAdmin"]
and (
Expand Down
22 changes: 17 additions & 5 deletions care/users/api/serializers/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class UserCreateSerializer(SignUpSerializer):
child=serializers.UUIDField(), required=False, allow_empty=True, write_only=True
)
home_facility = ExternalIdSerializerField(queryset=Facility.objects.all())

class Meta:
model = User
include = ("facilities",)
Expand Down Expand Up @@ -97,7 +98,8 @@ def validate_local_body(self, value):
and not self.context["created_by"].is_superuser
and not self.context["created_by"].user_type >= User.TYPE_VALUE_MAP["DistrictAdmin"]
):
raise serializers.ValidationError("Cannot create for a different local body")
raise serializers.ValidationError(
"Cannot create for a different local body")
return value

def validate_district(self, value):
Expand All @@ -124,7 +126,8 @@ def validate(self, attrs):
if self.context["created_by"].user_type in READ_ONLY_USER_TYPES:
if validated["user_type"] not in READ_ONLY_USER_TYPES:
raise exceptions.ValidationError(
{"user_type": ["Read only users can create other read only users only"]}
{"user_type": [
"Read only users can create other read only users only"]}
)
if (
self.context["created_by"].user_type == User.TYPE_VALUE_MAP["Staff"]
Expand All @@ -135,15 +138,17 @@ def validate(self, attrs):
validated["user_type"] > self.context["created_by"].user_type
and not self.context["created_by"].is_superuser
):
raise exceptions.ValidationError({"user_type": ["User cannot create another user with higher permissions"]})
raise exceptions.ValidationError(
{"user_type": ["User cannot create another user with higher permissions"]})

if (
not validated.get("ward")
and not validated.get("local_body")
and not validated.get("district")
and not validated.get("state")
):
raise exceptions.ValidationError({"__all__": ["One of ward, local body, district or state is required"]})
raise exceptions.ValidationError(
{"__all__": ["One of ward, local body, district or state is required"]})

return validated

Expand Down Expand Up @@ -193,9 +198,11 @@ class UserSerializer(SignUpSerializer):
district_object = DistrictSerializer(source="district", read_only=True)
state_object = StateSerializer(source="state", read_only=True)
alt_phone_number = PhoneNumberIsPossibleField(required=False, allow_blank=True)
home_facility_object = FacilityBareMinimumSerializer(source="home_facility", read_only=True)
home_facility_object = FacilityBareMinimumSerializer(
source="home_facility", read_only=True)

home_facility = ExternalIdSerializerField(queryset=Facility.objects.all())

class Meta:
model = User
fields = (
Expand Down Expand Up @@ -279,6 +286,9 @@ class UserListSerializer(serializers.ModelSerializer):
state_object = StateSerializer(source="state", read_only=True)
user_type = ChoiceField(choices=User.TYPE_CHOICES, read_only=True)
created_by = serializers.CharField(source="created_by_user", read_only=True)
home_facility_object = FacilityBareMinimumSerializer(
source="home_facility", read_only=True)
home_facility = ExternalIdSerializerField(queryset=Facility.objects.all())

class Meta:
model = User
Expand All @@ -293,4 +303,6 @@ class Meta:
"user_type",
"created_by",
"last_login",
"home_facility_object",
"home_facility",
)

0 comments on commit ab3e690

Please sign in to comment.