Skip to content

Commit 05af5d9

Browse files
fixup! feat: check authz permissions for course tagging
1 parent 5c93291 commit 05af5d9

1 file changed

Lines changed: 16 additions & 8 deletions

File tree

  • openedx/core/djangoapps/content_tagging/rest_api/v1

openedx/core/djangoapps/content_tagging/rest_api/v1/views.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
"""
44
from __future__ import annotations
55

6+
import functools
7+
from typing import TYPE_CHECKING
8+
69
from django.db.models import Count
710
from django.http import StreamingHttpResponse
811
from openedx_authz import api as authz_api
@@ -40,6 +43,9 @@
4043
TaxonomyUpdateOrgBodySerializer,
4144
)
4245

46+
if TYPE_CHECKING:
47+
from opaque_keys.edx.keys import CourseKey
48+
4349

4450
class TaxonomyOrgView(TaxonomyView):
4551
"""
@@ -161,15 +167,17 @@ class ObjectTagOrgView(ObjectTagView):
161167

162168
filter_backends = [ObjectTagTaxonomyOrgFilterBackend]
163169

164-
def _should_use_authz(self) -> bool:
170+
@functools.cached_property
171+
def _authz_check(self) -> tuple[bool, CourseKey | None]:
165172
"""
166-
Determine if we should use openedx-authz for the current object_id.
173+
Cache the authz toggle + key-parsing result for the current object_id.
174+
175+
Safe to cache per-instance because DRF creates a new view instance per request.
167176
"""
168177
object_id = self.kwargs.get('object_id')
169178
if object_id:
170-
should_use_authz, _ = should_use_authz_for_object(object_id)
171-
return should_use_authz
172-
return False
179+
return should_use_authz_for_object(object_id)
180+
return False, None
173181

174182
def get_permissions(self):
175183
"""
@@ -179,7 +187,7 @@ def get_permissions(self):
179187
permission classes set by the parent ObjectTagView so that only openedx-authz
180188
permissions are used.
181189
"""
182-
if self._should_use_authz():
190+
if self._authz_check[0]:
183191
return [IsAuthenticated()]
184192

185193
return super().get_permissions()
@@ -190,7 +198,7 @@ def ensure_has_view_object_tag_permission(self, user, taxonomy, object_id):
190198
191199
This method is overridden to conditionally use openedx-authz when the toggle is enabled.
192200
"""
193-
should_use_authz, course_key = should_use_authz_for_object(object_id)
201+
should_use_authz, course_key = self._authz_check
194202
if should_use_authz and not authz_api.is_user_allowed(
195203
user.username, COURSES_VIEW_COURSE.identifier, str(course_key)
196204
):
@@ -208,7 +216,7 @@ def ensure_user_has_can_tag_object_permissions(self, user, tags_data, object_id)
208216
When using openedx-authz, if the user has manage tags permission for the course,
209217
they can tag the object regardless of the taxonomy.
210218
"""
211-
should_use_authz, course_key = should_use_authz_for_object(object_id)
219+
should_use_authz, course_key = self._authz_check
212220
if should_use_authz and not authz_api.is_user_allowed(
213221
user.username, COURSES_MANAGE_TAGS.identifier, str(course_key)
214222
):

0 commit comments

Comments
 (0)