From 5475f661d97a1368b4a15cd308d916229a659015 Mon Sep 17 00:00:00 2001 From: adibmbrk Date: Tue, 17 Nov 2026 21:40:09 +0530 Subject: [PATCH 1/2] Check channel permissions in language_exists and languages actions Load the channel through get_object() so users who can't view it get the same 404 as a nonexistent channel. Signed-off-by: adibmbrk --- .../tests/viewsets/test_channel.py | 43 +++++++++++++- .../contentcuration/viewsets/channel.py | 57 +++---------------- 2 files changed, 49 insertions(+), 51 deletions(-) diff --git a/contentcuration/contentcuration/tests/viewsets/test_channel.py b/contentcuration/contentcuration/tests/viewsets/test_channel.py index 8421848da3..57907292e8 100644 --- a/contentcuration/contentcuration/tests/viewsets/test_channel.py +++ b/contentcuration/contentcuration/tests/viewsets/test_channel.py @@ -1193,6 +1193,12 @@ def setUp(self): self.channel.language_id = "en" self.channel.save() + self.editor = testdata.user(email="language-editor@user.com") + self.channel.editors.add(self.editor) + self.viewer = testdata.user(email="language-viewer@user.com") + self.channel.viewers.add(self.viewer) + self.non_member = testdata.user(email="language-nonmember@user.com") + self.channel_id = self.channel.id self.node_id = "00000000000000000000000000000003" self.public_node = PublicContentNode.objects.create( @@ -1276,9 +1282,40 @@ def test_get_languages_in_channel_invalid_request(self): response = self._perform_action("channel-languages", None) self.assertEqual(response.status_code, 404, response.content) - def _perform_action(self, url_path, channel_id): - user = testdata.user() - self.client.force_authenticate(user=user) + def test_language_actions_viewer_has_access(self): + ContentNode.objects.filter(node_id=self.public_node.id).update(language_id="en") + response = self._perform_action( + "channel-language-exists", self.channel.id, user=self.viewer + ) + self.assertEqual(response.status_code, 200, response.content) + self.assertTrue(response.json()["exists"]) + + response = self._perform_action( + "channel-languages", self.channel.id, user=self.viewer + ) + self.assertEqual(response.status_code, 200, response.content) + + def test_language_actions_non_member_gets_404(self): + for url_path in ("channel-language-exists", "channel-languages"): + response = self._perform_action( + url_path, self.channel.id, user=self.non_member + ) + self.assertEqual(response.status_code, 404, response.content) + + def test_language_actions_nonexistent_channel_matches_non_member(self): + for url_path in ("channel-language-exists", "channel-languages"): + missing = self._perform_action( + url_path, uuid.uuid4().hex, user=self.non_member + ) + denied = self._perform_action( + url_path, self.channel.id, user=self.non_member + ) + self.assertEqual(missing.status_code, 404, missing.content) + self.assertEqual(missing.status_code, denied.status_code) + self.assertEqual(missing.content, denied.content) + + def _perform_action(self, url_path, channel_id, user=None): + self.client.force_authenticate(user=user or self.editor) response = self.client.get( reverse(url_path, kwargs={"pk": channel_id}), format="json" ) diff --git a/contentcuration/contentcuration/viewsets/channel.py b/contentcuration/contentcuration/viewsets/channel.py index 3f9a866105..543ceaa9ba 100644 --- a/contentcuration/contentcuration/viewsets/channel.py +++ b/contentcuration/contentcuration/viewsets/channel.py @@ -15,7 +15,6 @@ from django.db.models import Value from django.db.models.functions import Coalesce from django.http import HttpResponse -from django.http import HttpResponseNotFound from django.http import JsonResponse from django.utils.decorators import method_decorator from django.views.decorators.cache import cache_page @@ -883,12 +882,12 @@ def channel_language_exists( :return: JsonResponse with exists=True if the language exists, False otherwise :rtype: JsonResponse """ - if not self._channel_exists(pk): - return HttpResponseNotFound("No channel matching: {}".format(pk)) + channel = self.get_object() - channel_lang, main_tree_id = self._get_channel_details(pk).values() - langs_in_channel = self._get_channel_content_languages(pk, main_tree_id) - lang_exists = channel_lang in langs_in_channel + langs_in_channel = self._get_channel_content_languages( + channel.id, channel.main_tree_id + ) + lang_exists = channel.language_id in langs_in_channel return JsonResponse({"exists": lang_exists}) @@ -904,12 +903,11 @@ def get_languages_in_channel( :return: JsonResponse with a list of languages present in the channel :rtype: JsonResponse """ - if not self._channel_exists(pk): - return HttpResponseNotFound("No channel matching: {}".format(pk)) + channel = self.get_object() - channel_details = self._get_channel_details(pk) - main_tree_id = channel_details.get("main_tree_id") - langs_in_content = self._get_channel_content_languages(pk, main_tree_id) + langs_in_content = self._get_channel_content_languages( + channel.id, channel.main_tree_id + ) return JsonResponse({"languages": langs_in_content}) @action( @@ -979,43 +977,6 @@ def has_community_library_submission(self, request, pk=None) -> Response: ).exists() return Response({"has_community_library_submission": has_submission}) - def _channel_exists(self, channel_id) -> bool: - """ - Check if a channel exists. - - :param channel_id: The ID of the channel - :return: True if the channel exists, False otherwise - :rtype: bool - """ - try: - return Channel.objects.filter(pk=channel_id).exists() - except Exception as e: - logging.error(f"Error checking if channel exists: {e}") - return False - - def _get_channel_details(self, channel_id) -> Dict[str, any]: - """ - Get the language set for a channel. - - :param channel_id: The ID of the channel - :return: The language code set for the channel - :rtype: str - """ - try: - channel_details = ( - Channel.objects.filter(pk=channel_id) - .values("language_id", "main_tree_id") - .first() - ) - except Channel.DoesNotExist as e: - logging.error(str(e)) - channel_details = None - - if not channel_details: - channel_details = dict(language_id=None, main_tree_id=None) - - return channel_details - def _get_channel_content_languages( self, channel_id, main_tree_id=None ) -> List[str]: From 4a71345b2d6805ebbfd4898ae6ffe24c45ba78b0 Mon Sep 17 00:00:00 2001 From: adibmbrk Date: Tue, 24 Nov 2026 13:36:00 +0530 Subject: [PATCH 2/2] Slim channel lookup in language actions and drop redundant test Signed-off-by: adibmbrk --- .../tests/viewsets/test_channel.py | 7 ------ .../contentcuration/viewsets/channel.py | 22 ++++++++++++++----- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/contentcuration/contentcuration/tests/viewsets/test_channel.py b/contentcuration/contentcuration/tests/viewsets/test_channel.py index 57907292e8..b4eb5d4b4b 100644 --- a/contentcuration/contentcuration/tests/viewsets/test_channel.py +++ b/contentcuration/contentcuration/tests/viewsets/test_channel.py @@ -1295,13 +1295,6 @@ def test_language_actions_viewer_has_access(self): ) self.assertEqual(response.status_code, 200, response.content) - def test_language_actions_non_member_gets_404(self): - for url_path in ("channel-language-exists", "channel-languages"): - response = self._perform_action( - url_path, self.channel.id, user=self.non_member - ) - self.assertEqual(response.status_code, 404, response.content) - def test_language_actions_nonexistent_channel_matches_non_member(self): for url_path in ("channel-language-exists", "channel-languages"): missing = self._perform_action( diff --git a/contentcuration/contentcuration/viewsets/channel.py b/contentcuration/contentcuration/viewsets/channel.py index 543ceaa9ba..09c8558d03 100644 --- a/contentcuration/contentcuration/viewsets/channel.py +++ b/contentcuration/contentcuration/viewsets/channel.py @@ -1,7 +1,6 @@ import logging from functools import reduce from operator import or_ -from typing import Dict from typing import List from typing import Union @@ -16,6 +15,7 @@ from django.db.models.functions import Coalesce from django.http import HttpResponse from django.http import JsonResponse +from django.shortcuts import get_object_or_404 from django.utils.decorators import method_decorator from django.views.decorators.cache import cache_page from django_cte import With @@ -882,12 +882,17 @@ def channel_language_exists( :return: JsonResponse with exists=True if the language exists, False otherwise :rtype: JsonResponse """ - channel = self.get_object() + channel = get_object_or_404( + models.Channel.filter_view_queryset( + models.Channel.objects.all(), request.user + ).values("id", "language_id", "main_tree_id"), + pk=pk, + ) langs_in_channel = self._get_channel_content_languages( - channel.id, channel.main_tree_id + channel["id"], channel["main_tree_id"] ) - lang_exists = channel.language_id in langs_in_channel + lang_exists = channel["language_id"] in langs_in_channel return JsonResponse({"exists": lang_exists}) @@ -903,10 +908,15 @@ def get_languages_in_channel( :return: JsonResponse with a list of languages present in the channel :rtype: JsonResponse """ - channel = self.get_object() + channel = get_object_or_404( + models.Channel.filter_view_queryset( + models.Channel.objects.all(), request.user + ).values("id", "language_id", "main_tree_id"), + pk=pk, + ) langs_in_content = self._get_channel_content_languages( - channel.id, channel.main_tree_id + channel["id"], channel["main_tree_id"] ) return JsonResponse({"languages": langs_in_content})