1- from enums .feature_flags import FeatureFlags
21from enums .lambda_error import LambdaError
32from enums .repository_role import RepositoryRole
43from pydantic import ValidationError
54from pydantic_core import PydanticSerializationError
6- from services .feature_flags_service import FeatureFlagService
75from services .manage_user_session_access import ManageUserSessionAccess
86from utils .audit_logging_setup import LoggingService
97from utils .exceptions import (
@@ -24,9 +22,13 @@ def __init__(self, user_role, user_ods_code):
2422 self .user_role = user_role
2523 self .user_ods_code = user_ods_code
2624 self .manage_user_session_service = ManageUserSessionAccess ()
27- self .feature_flag_service = FeatureFlagService ()
2825
29- def handle_search_patient_request (self , nhs_number , update_session = True ):
26+ def handle_search_patient_request (
27+ self ,
28+ nhs_number ,
29+ update_session = True ,
30+ can_access_not_my_record = False ,
31+ ):
3032 """
3133 Handle search patient request and return patient details if authorised.
3234
@@ -43,15 +45,20 @@ def handle_search_patient_request(self, nhs_number, update_session=True):
4345 try :
4446 patient_details = self ._fetch_patient_details (nhs_number )
4547
48+ can_manage_record = patient_details .deceased
49+
4650 if not patient_details .deceased :
47- self ._check_authorization (patient_details .general_practice_ods )
51+ can_manage_record = self ._check_authorization (
52+ patient_details .general_practice_ods , can_access_not_my_record
53+ )
4854
4955 logger .info ("Searched for patient details" , {"Result" : "Patient found" })
5056
51- if update_session :
57+ if update_session and can_manage_record :
5258 self ._update_session (nhs_number , patient_details .deceased )
5359
54- # Return the patient details object directly
60+ patient_details .can_manage_record = can_manage_record
61+
5562 return patient_details
5663
5764 except PatientNotFoundException as e :
@@ -86,7 +93,9 @@ def _fetch_patient_details(self, nhs_number):
8693 pds_service = get_pds_service ()
8794 return pds_service .fetch_patient_details (nhs_number )
8895
89- def _check_authorization (self , gp_ods_for_patient ):
96+ def _check_authorization (
97+ self , gp_ods_for_patient , can_access_not_my_record
98+ ) -> bool :
9099 """
91100 Check if the current user is authorised to view the patient details.
92101
@@ -97,33 +106,18 @@ def _check_authorization(self, gp_ods_for_patient):
97106 UserNotAuthorisedException: If the user is not authorised
98107 """
99108 patient_is_active = is_ods_code_active (gp_ods_for_patient )
100- is_arf_journey_on = self ._is_arf_upload_enabled ()
109+ user_is_data_controller = gp_ods_for_patient == self .user_ods_code
101110
102111 match self .user_role :
103- case RepositoryRole .GP_ADMIN .value :
104- if patient_is_active and gp_ods_for_patient != self .user_ods_code :
105- raise UserNotAuthorisedException
106- elif not patient_is_active and not is_arf_journey_on :
107- raise UserNotAuthorisedException
108-
109- case RepositoryRole .GP_CLINICAL .value :
110- if not patient_is_active or gp_ods_for_patient != self .user_ods_code :
111- raise UserNotAuthorisedException
112+ case RepositoryRole .GP_ADMIN .value | RepositoryRole .GP_CLINICAL .value :
113+ if user_is_data_controller or can_access_not_my_record :
114+ return user_is_data_controller
112115
113116 case RepositoryRole .PCSE .value :
114- if patient_is_active :
115- raise UserNotAuthorisedException
117+ if not patient_is_active :
118+ return True
116119
117- case _:
118- raise UserNotAuthorisedException
119-
120- def _is_arf_upload_enabled (self ):
121- """Check if ARF upload workflow is enabled via feature flags"""
122- upload_flag_name = FeatureFlags .UPLOAD_ARF_WORKFLOW_ENABLED .value
123- upload_lambda_enabled_flag_object = (
124- self .feature_flag_service .get_feature_flags_by_flag (upload_flag_name )
125- )
126- return upload_lambda_enabled_flag_object [upload_flag_name ]
120+ raise UserNotAuthorisedException
127121
128122 def _update_session (self , nhs_number , is_deceased ):
129123 """Update the user session with permitted search information"""
0 commit comments