Skip to content

Commit 7d3a6a5

Browse files
committed
Fix permision issues
1 parent ac8c73f commit 7d3a6a5

File tree

5 files changed

+39
-4
lines changed

5 files changed

+39
-4
lines changed

care/emr/api/viewsets/notes.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ def get_patient(self):
4242
Patient, external_id=self.kwargs["patient_external_id"]
4343
)
4444

45+
def authorize_create(self, instance):
46+
pass
47+
48+
def authorize_update(self, request_obj, model_instance):
49+
pass
50+
51+
def authorize_delete(self, instance):
52+
pass
53+
4554
def perform_create(self, instance):
4655
instance.patient = self.get_patient()
4756
if instance.encounter and instance.encounter.patient != instance.patient:

care/emr/api/viewsets/patient.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,22 @@ class PatientViewSet(EMRModelViewSet):
3636
filterset_class = PatientFilters
3737
filter_backends = [DjangoFilterBackend]
3838

39-
# TODO : Retrieve will work if an active encounter exists on the patient
39+
def authorize_update(self, request_obj, model_instance):
40+
if not AuthorizationController.call(
41+
"can_write_patient_obj", self.request.user , model_instance
42+
):
43+
raise PermissionDenied("Cannot Create Patient")
44+
45+
46+
def authorize_create(self, request_obj):
47+
if not AuthorizationController.call(
48+
"can_create_patient", self.request.user
49+
):
50+
raise PermissionDenied("Cannot Create Patient")
51+
52+
def authorize_delete(self, instance):
53+
if not self.request.user.is_superuser:
54+
raise PermissionDenied("Cannot delete patient")
4055

4156
def get_queryset(self):
4257
qs = (
@@ -121,6 +136,7 @@ def add_user(self, request, *args, **kwargs):
121136
user = get_object_or_404(User, external_id=request_data.user)
122137
role = get_object_or_404(RoleModel, external_id=request_data.role)
123138
patient = self.get_object()
139+
self.authorize_update({}, patient)
124140
if PatientUser.objects.filter(user=user, patient=patient).exists():
125141
raise ValidationError("User already exists")
126142
PatientUser.objects.create(user=user, patient=patient, role=role)
@@ -134,6 +150,7 @@ def delete_user(self, request, *args, **kwargs):
134150
request_data = self.PatientUserDeleteSpec(**self.request.data)
135151
user = get_object_or_404(User, external_id=request_data.user)
136152
patient = self.get_object()
153+
self.authorize_update({}, patient)
137154
if not PatientUser.objects.filter(user=user, patient=patient).exists():
138155
raise ValidationError("User does not exist")
139156
PatientUser.objects.filter(user=user, patient=patient).delete()

care/emr/resources/facility/spec.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ class FacilityBareMinimumSpec(EMRResource):
2020

2121
class FacilityBaseSpec(FacilityBareMinimumSpec):
2222
description: str
23-
longitude: float
24-
latitude: float
23+
longitude: float | None = None
24+
latitude: float | None = None
2525
pincode: int
2626
address: str
2727
phone_number: str

care/security/authorization/patient.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ def find_roles_on_patient(self, user, patient):
3535
)
3636
return role_ids.union(set(roles))
3737

38+
39+
3840
def can_view_patient_obj(self, user, patient):
3941
if user.is_superuser:
4042
return True
@@ -53,6 +55,13 @@ def can_write_patient_obj(self, user, patient):
5355
role__in=user_roles,
5456
).exists()
5557

58+
def can_create_patient(self, user):
59+
return self.check_permission_in_facility_organization(
60+
[PatientPermissions.can_create_patient.name],
61+
user
62+
)
63+
64+
5665
def can_view_clinical_data(self, user, patient):
5766
if user.is_superuser:
5867
return True

care/security/permissions/facility.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
class FacilityPermissions(enum.Enum):
1515
can_create_facility = Permission(
16-
"Can Read on Facility",
16+
"Can Create on Facility",
1717
"Something Here",
1818
PermissionContext.FACILITY,
1919
[GEO_ADMIN, ADMIN_ROLE],

0 commit comments

Comments
 (0)