1+ from waffle import switch_is_active
12from apps .fhir .bluebutton .views .generic import FhirDataView
23from apps .fhir .bluebutton .permissions import (SearchCrosswalkPermission ,
34 ResourcePermission ,
45 ApplicationActivePermission )
56from apps .authorization .permissions import DataAccessGrantPermission
67from apps .capabilities .permissions import TokenHasProtectedCapability
8+ from django .http import JsonResponse
79
810from rest_framework import permissions # pyright: ignore[reportMissingImports]
911
1012
11- def _is_not_empty (s : set ):
13+ def _is_not_empty (s : set ) -> bool :
1214 if len (s ) > 0 :
1315 return True
1416 else :
@@ -21,23 +23,41 @@ class HasDigitalInsuranceCardScope(permissions.BasePermission):
2123 required_patient_read_scopes = ['patient/Patient.r' , 'patient/Patient.rs' , 'patient/Patient.read' ]
2224
2325 def has_permission (self , request , view ) -> bool : # type: ignore
26+ print ("HasDigitalInsuranceCardScope has_permission" )
27+
2428 # Is this an authorized request? If not, exit.
25- if request .get ('auth' , None ) is None :
29+ if not hasattr (request , 'auth' ):
30+ return False
31+ if request .auth is None :
2632 return False
2733
2834 # If we're authenticated, then we can check the scopes from the token.
29- token_scopes = request .auth .scope
35+ token_scope_string = request .auth .scope
36+ # This will be a space-separated string.
37+ token_scopes = list (map (lambda s : s .strip (), token_scope_string .split (" " )))
38+
3039 # Two things need to be true:
3140 # 1. At least one of the scopes in the token needs to be one of the above coverage scopes.
3241 # 2. At leaset one of the scopes in the token needs to be one of the above read scopes.
3342 coverage_set = set (HasDigitalInsuranceCardScope .required_coverage_search_scopes )
3443 patient_set = set (HasDigitalInsuranceCardScope .required_patient_read_scopes )
3544 token_set = set (token_scopes )
45+
46+ # print()
47+ # print("CS", coverage_set)
48+ # print("PS", patient_set)
49+ # print("TS", token_set)
50+
3651 return (_is_not_empty (coverage_set .intersection (token_set ))
3752 and _is_not_empty (patient_set .intersection (token_set )))
3853
3954
40- class DigitalInsuranceCardSearchView (FhirDataView ):
55+ class WaffleSwitchV3IsActive (permissions .BasePermission ):
56+ def has_permission (self , request , view ):
57+ return switch_is_active ('v3_endpoints' )
58+
59+
60+ class DigitalInsuranceCardView (FhirDataView ):
4161 '''Digital Insurance Card view for handling BFD Endpoint'''
4262
4363 permission_classes = [
@@ -59,7 +79,20 @@ def __init__(self, version=1):
5979 super ().__init__ (version )
6080 self .resource_type = 'Bundle'
6181
82+ def initial (self , request , * args , ** kwargs ):
83+ return super ().initial (request , self .resource_type , * args , ** kwargs )
84+
85+ def get (self , request , * args , ** kwargs ):
86+ # return super().get(request, self.resource_type, *args, **kwargs)
87+ return JsonResponse (status = 200 , data = {"ok" : "go" })
88+
89+ # How do the has_permission herre and the has_permission in the permission classes
90+ # play together? If they pass, can this fail? Visa-versa?
91+
6292 def has_permission (self , request , view ):
93+ # TODO: Why is this not being called?
94+ # A print statement where this comment is does not appear when unit tests are run.
95+ # But, the permission classes run. Where/when does has_permission get called?
6396 required_scopes = getattr (view , 'required_scopes' , None )
6497 if required_scopes is None :
6598 return False
0 commit comments