Skip to content

Commit 09ed8db

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

1 file changed

Lines changed: 17 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: 17 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
@@ -31,6 +34,10 @@
3134
)
3235
from ...auth import has_view_object_tags_access, should_use_authz_for_object
3336
from ...rules import get_admin_orgs
37+
38+
if TYPE_CHECKING:
39+
from opaque_keys.edx.keys import CourseKey
40+
3441
from .filters import ObjectTagTaxonomyOrgFilterBackend, UserOrgFilterBackend
3542
from .serializers import (
3643
ObjectTagCopiedMinimalSerializer,
@@ -161,15 +168,17 @@ class ObjectTagOrgView(ObjectTagView):
161168

162169
filter_backends = [ObjectTagTaxonomyOrgFilterBackend]
163170

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

174183
def get_permissions(self):
175184
"""
@@ -179,7 +188,7 @@ def get_permissions(self):
179188
permission classes set by the parent ObjectTagView so that only openedx-authz
180189
permissions are used.
181190
"""
182-
if self._should_use_authz():
191+
if self._authz_check[0]:
183192
return [IsAuthenticated()]
184193

185194
return super().get_permissions()
@@ -190,7 +199,7 @@ def ensure_has_view_object_tag_permission(self, user, taxonomy, object_id):
190199
191200
This method is overridden to conditionally use openedx-authz when the toggle is enabled.
192201
"""
193-
should_use_authz, course_key = should_use_authz_for_object(object_id)
202+
should_use_authz, course_key = self._authz_check
194203
if should_use_authz and not authz_api.is_user_allowed(
195204
user.username, COURSES_VIEW_COURSE.identifier, str(course_key)
196205
):
@@ -208,7 +217,7 @@ def ensure_user_has_can_tag_object_permissions(self, user, tags_data, object_id)
208217
When using openedx-authz, if the user has manage tags permission for the course,
209218
they can tag the object regardless of the taxonomy.
210219
"""
211-
should_use_authz, course_key = should_use_authz_for_object(object_id)
220+
should_use_authz, course_key = self._authz_check
212221
if should_use_authz and not authz_api.is_user_allowed(
213222
user.username, COURSES_MANAGE_TAGS.identifier, str(course_key)
214223
):

0 commit comments

Comments
 (0)