@@ -41,6 +41,8 @@ def __init__(
41
41
client_app_id : Optional [str ],
42
42
tenant_id : Optional [str ],
43
43
require_access_control : bool = False ,
44
+ enable_global_documents : bool = False ,
45
+ enable_unauthenticated_access : bool = False ,
44
46
):
45
47
self .use_authentication = use_authentication
46
48
self .server_app_id = server_app_id
@@ -62,18 +64,23 @@ def __init__(
62
64
field_names = [field .name for field in search_index .fields ] if search_index else []
63
65
self .has_auth_fields = "oids" in field_names and "groups" in field_names
64
66
self .require_access_control = require_access_control
67
+ self .enable_global_documents = enable_global_documents
68
+ self .enable_unauthenticated_access = enable_unauthenticated_access
65
69
self .confidential_client = ConfidentialClientApplication (
66
70
server_app_id , authority = self .authority , client_credential = server_app_secret , token_cache = TokenCache ()
67
71
)
68
72
else :
69
73
self .has_auth_fields = False
70
74
self .require_access_control = False
75
+ self .enable_global_documents = True
76
+ self .enable_unauthenticated_access = True
71
77
72
78
def get_auth_setup_for_client (self ) -> dict [str , Any ]:
73
79
# returns MSAL.js settings used by the client app
74
80
return {
75
81
"useLogin" : self .use_authentication , # Whether or not login elements are enabled on the UI
76
- "requireAccessControl" : self .require_access_control , # Whether or not access control is required to use the application
82
+ "requireAccessControl" : self .require_access_control , # Whether or not access control is required to access documents with access control lists
83
+ "enableUnauthenticatedAccess" : self .enable_unauthenticated_access , # Whether or not the user can access the app without login
77
84
"msalConfig" : {
78
85
"auth" : {
79
86
"clientId" : self .client_app_id , # Client app id used for login
@@ -150,17 +157,24 @@ def build_security_filters(self, overrides: dict[str, Any], auth_claims: dict[st
150
157
else None
151
158
)
152
159
153
- # If only one security filter is specified, return that filter
160
+ # If only one security filter is specified, use that filter
154
161
# If both security filters are specified, combine them with "or" so only 1 security filter needs to pass
155
162
# If no security filters are specified, don't return any filter
163
+ security_filter = None
156
164
if oid_security_filter and not groups_security_filter :
157
- return oid_security_filter
165
+ security_filter = f" { oid_security_filter } "
158
166
elif groups_security_filter and not oid_security_filter :
159
- return groups_security_filter
167
+ security_filter = f" { groups_security_filter } "
160
168
elif oid_security_filter and groups_security_filter :
161
- return f"({ oid_security_filter } or { groups_security_filter } )"
162
- else :
163
- return None
169
+ security_filter = f"({ oid_security_filter } or { groups_security_filter } )"
170
+
171
+ # If global documents are allowed, append the public global filter
172
+ if self .enable_global_documents :
173
+ global_documents_filter = "(not oids/any() and not groups/any())"
174
+ if security_filter :
175
+ security_filter = f"({ security_filter } or { global_documents_filter } )"
176
+
177
+ return security_filter
164
178
165
179
@staticmethod
166
180
async def list_groups (graph_resource_access_token : dict ) -> list [str ]:
@@ -230,12 +244,12 @@ async def get_auth_claims_if_enabled(self, headers: dict) -> dict[str, Any]:
230
244
return auth_claims
231
245
except AuthError as e :
232
246
logging .exception ("Exception getting authorization information - " + json .dumps (e .error ))
233
- if self .require_access_control :
247
+ if self .require_access_control and not self . enable_unauthenticated_access :
234
248
raise
235
249
return {}
236
250
except Exception :
237
251
logging .exception ("Exception getting authorization information" )
238
- if self .require_access_control :
252
+ if self .require_access_control and not self . enable_unauthenticated_access :
239
253
raise
240
254
return {}
241
255
0 commit comments