Skip to content

Commit

Permalink
SDAN-724 Restrict download of feature media
Browse files Browse the repository at this point in the history
  • Loading branch information
marwoodandrew committed Dec 14, 2023
1 parent ab8d973 commit 67769be
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
4 changes: 3 additions & 1 deletion newsroom/wire/formatters/downloadninjs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from superdesk.logging import logger
from .ninjs import NINJSFormatter
from .utils import remove_internal_renditions, rewire_featuremedia, log_media_downloads, remove_unpermissioned_embeds
from .utils import remove_internal_renditions, rewire_featuremedia, log_media_downloads, remove_unpermissioned_embeds,\
remove_unpermissioned_featuremedia
from newsroom.utils import update_embeds_in_body


Expand Down Expand Up @@ -71,6 +72,7 @@ def update_video_or_audio(item, elem, group):
update_embeds_in_body(item, update_image, update_video_or_audio, update_video_or_audio)

def _transform_to_ninjs(self, item):
remove_unpermissioned_featuremedia(item)
remove_unpermissioned_embeds(item)
# Remove the renditions we should not be showing the world
remove_internal_renditions(item, remove_media=False)
Expand Down
4 changes: 3 additions & 1 deletion newsroom/wire/formatters/htmlpackage.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import flask
from .base import BaseFormatter
from .utils import remove_internal_renditions, rewire_featuremedia, log_media_downloads, remove_unpermissioned_embeds
from .utils import remove_internal_renditions, rewire_featuremedia, log_media_downloads, remove_unpermissioned_embeds,\
remove_unpermissioned_featuremedia
from newsroom.utils import update_embeds_in_body
from superdesk.logging import logger

Expand Down Expand Up @@ -67,6 +68,7 @@ def update_video_or_audio(item, elem, group):
update_embeds_in_body(item, update_image, update_video_or_audio, update_video_or_audio)

def format_item(self, item, item_type='items'):
remove_unpermissioned_featuremedia(item)
remove_unpermissioned_embeds(item)
remove_internal_renditions(item, remove_media=False)
self.rewire_embeded_images(item)
Expand Down
4 changes: 3 additions & 1 deletion newsroom/wire/formatters/htmlwithmedia.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import flask
from .base import BaseFormatter
from .utils import remove_internal_renditions, log_media_downloads, remove_unpermissioned_embeds
from .utils import remove_internal_renditions, log_media_downloads, remove_unpermissioned_embeds,\
remove_unpermissioned_featuremedia
from newsroom.utils import update_embeds_in_body
from ...upload import ASSETS_RESOURCE
import base64
Expand Down Expand Up @@ -72,6 +73,7 @@ def rewire_featuremedia(self, item):
mimetype) + base64.b64encode(file.read()).decode()

def format_item(self, item, item_type='items'):
remove_unpermissioned_featuremedia(item)
remove_unpermissioned_embeds(item)
remove_internal_renditions(item)
self.rewire_embedded_images(item)
Expand Down
25 changes: 25 additions & 0 deletions newsroom/wire/formatters/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,28 @@ def remove_unpermissioned_embeds(item, company_id=None, section='wire'):
item.get("associations", {}).pop(key, None)
if "refs" in item:
item["refs"] = [r for r in item.get("refs", []) if r["key"] != key]


def remove_unpermissioned_featuremedia(item):
"""
Remove the feature media if it's not permitted, used by the interactive download formatters
:param item:
:return:
"""
if not app.config.get("EMBED_PRODUCT_FILTERING"):
return

user = get_user(required=True)
company_id = user.get('company')

permitted_products = {p.get('sd_product_id') for p in get_products_by_company(company_id, None, 'wire') if
p.get('sd_product_id')}
feature_media_products = {p.get('code') for p in
((item.get('associations') or {}).get('featuremedia') or {}).get('products', {})}

permitted = any(feature_media_products & permitted_products) if feature_media_products else True

if not permitted:
item.get('associations', {}).pop('featuremedia', None)
if not item.get('associations'):
item.pop('associations', None)

0 comments on commit 67769be

Please sign in to comment.