File tree Expand file tree Collapse file tree 2 files changed +30
-1
lines changed
Expand file tree Collapse file tree 2 files changed +30
-1
lines changed Original file line number Diff line number Diff line change @@ -343,6 +343,25 @@ def to_json(self) -> JSON:
343343 def update (self , filters : FiltersJSON ) -> Self :
344344 return attr .evolve (self , explicit = {** self .explicit , ** filters })
345345
346+ def reify_implicit_only (self , plugin : MetadataPlugin ) -> FiltersJSON :
347+ """
348+ Construct filters for *only* the implicit restriction on accessible
349+ sources. This is conceptually equivalent to the set difference
350+ `self.reify(limit_access=True) - self.reify(limit_access=False)`.
351+
352+ :param plugin: Metadata plugin for the current request's catalog
353+ """
354+ source_id_field = plugin .special_fields .source_id .name
355+ filters = copy_json (self .explicit )
356+ explicit_sources = self ._extract_filter (filters , source_id_field , default = None )
357+ if explicit_sources is not None :
358+ self ._forbid_explicit_inaccessible_sources (explicit_sources )
359+ return {
360+ source_id_field : {
361+ 'is' : sorted (self .source_ids ),
362+ }
363+ }
364+
346365 def reify (self ,
347366 plugin : MetadataPlugin ,
348367 * ,
Original file line number Diff line number Diff line change @@ -181,6 +181,9 @@ class FilterStage(_ElasticsearchStage[Response, Response]):
181181 def prepare_request (self , request : Search ) -> Search :
182182 query = self .prepare_query (self .prepared_filters )
183183 if self .post_filter :
184+ if self .service .always_limit_access or self ._limit_access :
185+ access_query = self .prepare_query (self .prepared_access_filter )
186+ request = request .query (access_query )
184187 request = request .post_filter (query )
185188 else :
186189 request = request .query (query )
@@ -191,10 +194,17 @@ def process_response(self, response: Response) -> Response:
191194
192195 @cached_property
193196 def prepared_filters (self ) -> TranslatedFilters :
194- limit_access = self .service .always_limit_access or self ._limit_access
197+ # The implicit source filter is always applied via a query, and would
198+ # therefore be redundant in the post_filter
199+ limit_access = (self .service .always_limit_access or self ._limit_access ) and not self .post_filter
195200 filters_json = self .filters .reify (self .plugin , limit_access = limit_access )
196201 return self ._translate_filters (filters_json )
197202
203+ @cached_property
204+ def prepared_access_filter (self ) -> TranslatedFilters :
205+ filters_json = self .filters .reify_implicit_only (self .plugin )
206+ return self ._translate_filters (filters_json )
207+
198208 @property
199209 @abstractmethod
200210 def _limit_access (self ) -> bool :
You can’t perform that action at this time.
0 commit comments