2121# or submit itself to any jurisdiction.
2222"""CDS JSON Serializer."""
2323
24-
2524from flask import has_request_context
2625from flask_security import current_user
2726from invenio_records_rest .serializers .json import JSONSerializer
27+ from marshmallow_utils .html import ALLOWED_CSS_STYLES , ALLOWED_HTML_ATTRS , sanitize_html
2828
2929from ..api import CDSRecord
3030from ..permissions import (
3131 has_read_record_eos_path_permission ,
3232 has_read_record_permission ,
33+ has_update_permission ,
3334)
3435from ..utils import HTMLTagRemover , parse_video_chapters
35- from marshmallow_utils .html import sanitize_html , ALLOWED_HTML_ATTRS , ALLOWED_CSS_STYLES
3636
3737CUSTOM_ALLOWED_ATTRS = {
3838 ** ALLOWED_HTML_ATTRS ,
@@ -89,6 +89,15 @@ def _sanitize_metadata(self, metadata):
8989
9090 return metadata
9191
92+ def _remove_access (self , record , metadata ):
93+ """Remove _access from the metadata if the user doesn't have update permissions on the record."""
94+ if "_access" in metadata and not has_update_permission (current_user , record ):
95+ metadata .pop ("_access" )
96+ metadata .pop ("_buckets" )
97+ metadata .pop ("_cds" )
98+ metadata .pop ("_deposit" )
99+ return metadata
100+
92101 def preprocess_record (self , pid , record , links_factory = None ):
93102 """Include ``_eos_library_path`` for single record retrievals."""
94103 result = super (CDSJSONSerializer , self ).preprocess_record (
@@ -106,6 +115,7 @@ def preprocess_record(self, pid, record, links_factory=None):
106115 # sanitize title by unescaping and stripping html tags
107116 try :
108117 metadata = self ._sanitize_metadata (metadata )
118+ metadata = self ._remove_access (record , metadata )
109119 if has_request_context ():
110120 metadata ["videos" ] = [
111121 video
@@ -134,5 +144,6 @@ def preprocess_search_hit(self, pid, record_hit, links_factory=None):
134144 if "metadata" in result :
135145 metadata = result ["metadata" ]
136146 result ["metadata" ] = self ._sanitize_metadata (result ["metadata" ])
147+ result ["metadata" ] = self ._remove_access (record_hit , result ["metadata" ])
137148
138149 return result
0 commit comments