44"""
55
66from unique_permission import get_unique_action_flags_from_s3
7+ from enum import StrEnum
78from clients import logger , s3_client
89from make_and_upload_ack_file import make_and_upload_ack_file
910from utils_for_recordprocessor import get_csv_content_dict_reader , invoke_filename_lambda
1011from errors import InvalidHeaders , NoOperationPermissions
1112from logging_decorator import file_level_validation_logging_decorator
1213from audit_table import change_audit_table_status_to_processed , get_next_queued_file_details
13- from constants import SOURCE_BUCKET_NAME , EXPECTED_CSV_HEADERS , Permission , ActionFlag , AllowedPermission
14+ from constants import SOURCE_BUCKET_NAME , EXPECTED_CSV_HEADERS
15+
16+
17+ class ActionFlag (StrEnum ):
18+ CREATE = "NEW"
19+ UPDATE = "UPDATE"
20+ DELETE = "DELETE"
21+
22+ class Permission (StrEnum ):
23+ CREATE = "C"
24+ UPDATE = "U"
25+ DELETE = "D"
26+
27+
28+ permission_to_action_flag_map = {
29+ Permission .CREATE : ActionFlag .CREATE ,
30+ Permission .UPDATE : ActionFlag .UPDATE ,
31+ Permission .DELETE : ActionFlag .DELETE
32+ }
1433
1534
1635def validate_content_headers (csv_content_reader ) -> None :
@@ -29,45 +48,50 @@ def validate_action_flag_permissions(
2948 """
3049
3150 # Get unique ACTION_FLAG values from the S3 file
32- operations_requested = get_unique_action_flags_from_s3 (csv_data )
51+ required_action_flags = get_unique_action_flags_from_s3 (csv_data )
3352
34- # Map ACTION_FLAGs to single-letter permissions
35- requested_permissions = {
36- ActionFlag [flag ].value .value
37- for flag in operations_requested
38- if flag in ActionFlag .__members__
39- }
53+ raw_action_flags = get_unique_action_flags_from_s3 (csv_data )
54+ valid_action_flag_values = {flag .value for flag in ActionFlag }
55+ required_action_flags = raw_action_flags & valid_action_flag_values # intersection
4056
41- if not requested_permissions :
57+ if not required_action_flags :
4258 logger .warning ("No valid ACTION_FLAGs found in file. Skipping permission validation." )
4359 return set ()
4460
45- # Get allowed permission in single letters from allowed_permissions_list
46- allowed_ops = set ()
47- for perm in allowed_permissions_list :
48- if not perm .startswith (f"{ vaccine_type } ." ):
49- continue
61+ # Check if supplier has permission for the subject vaccine type and extract permissions
62+ permission_strs_for_vaccine_type = set (
63+ permission_str
64+ for permission_str in allowed_permissions_list
65+ if permission_str .split ("." )[0 ].upper () == vaccine_type .upper ()
66+ )
67+
68+ # Extract permissions letters to get map key from the allowed vaccine type
69+ permissions_for_vaccine_type = set (
70+ Permission (permission )
71+ for permission_str in permission_strs_for_vaccine_type
72+ for permission in permission_str .split ("." )[1 ].upper () # CRUDS, CRUD etc
73+ if permission in list (Permission )
74+ )
5075
51- _ , op_code = perm . split ( "." )
52- if op_code in AllowedPermission . __members__ :
53- allowed_ops . update ( AllowedPermission [ op_code ].value )
54- else :
55- allowed_ops . add ( op_code )
76+ # Map Permission key to action flag
77+ permitted_action_flags_for_vaccine_type = set (
78+ permission_to_action_flag_map [ permission ].value
79+ for permission in permissions_for_vaccine_type
80+ )
5681
57- if not requested_permissions .intersection (allowed_ops ):
82+ if not required_action_flags .intersection (permitted_action_flags_for_vaccine_type ):
5883 raise NoOperationPermissions (
5984 f"{ supplier } does not have permissions to perform any of the requested actions."
60- )
85+ )
6186
6287 logger .info (
63- "%s permissions %s match one of the requested permissions required to %s" ,
64- supplier ,
65- allowed_permissions_list ,
66- requested_permissions ,
67- )
88+ "%s permissions %s match one of the requested permissions required to %s" ,
89+ supplier ,
90+ allowed_permissions_list ,
91+ permitted_action_flags_for_vaccine_type ,
92+ )
6893
69- # Return allowed ops in full-word format for downstream logic
70- return {perm .name for perm in Permission if perm .value in allowed_ops }
94+ return {permission .name for permission in permissions_for_vaccine_type }
7195
7296
7397def move_file (bucket_name : str , source_file_key : str , destination_file_key : str ) -> None :
0 commit comments