Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDAN-724 Restrict download of feature media #1183

Open
wants to merge 1 commit into
base: lockhart
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading