Skip to content

Commit ee4cd55

Browse files
refactor: use constants instead of action plain string
1 parent 1bf58ae commit ee4cd55

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

openedx/core/djangoapps/content_libraries/permissions.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
from django.conf import settings
77
from django.db.models import Q
88

9+
from openedx_authz.api.data import PermissionData
910
from openedx_authz.api.users import is_user_allowed, get_scopes_for_user_and_permission
11+
from openedx_authz.constants.permissions import VIEW_LIBRARY
1012

1113
from openedx.core.djangoapps.content_libraries.models import ContentLibraryPermission
1214

@@ -134,15 +136,15 @@ class HasPermissionInContentLibraryScope(Rule):
134136
org.short_name='DemoX' and slug='CSPROB'.
135137
"""
136138

137-
def __init__(self, action_external_key: str, filter_keys: list[str] | None = None):
139+
def __init__(self, permission: PermissionData, filter_keys: list[str] | None = None):
138140
"""Initialize the rule with the action and filter keys to filter on.
139141
140142
Args:
141-
action_external_key (str): The action/permission to check (e.g., 'view', 'edit').
143+
permission (PermissionData): The permission to check (e.g., 'view', 'edit').
142144
filter_keys (list[str]): The model fields to filter on when building QuerySet filters.
143145
Defaults to ['org', 'slug'] for ContentLibrary.
144146
"""
145-
self.action_external_key = action_external_key
147+
self.permission = permission
146148
self.filter_keys = filter_keys if filter_keys is not None else ["org", "slug"]
147149

148150
def query(self, user):
@@ -179,7 +181,7 @@ def query(self, user):
179181
"""
180182
scopes = get_scopes_for_user_and_permission(
181183
user.username,
182-
self.action_external_key
184+
self.permission.identifier
183185
)
184186

185187
library_keys = [scope.library_key for scope in scopes]
@@ -216,7 +218,7 @@ def check(self, user, instance, *args, **kwargs): # pylint: disable=arguments-d
216218
>>> can_view = rule.check(user, library)
217219
>>> # Checks if user has 'view' permission in scope 'lib:DemoX:CSPROB'
218220
"""
219-
return is_user_allowed(user.username, self.action_external_key, str(instance.library_key))
221+
return is_user_allowed(user.username, self.permission.identifier, str(instance.library_key))
220222

221223

222224
########################### Permissions ###########################
@@ -253,7 +255,7 @@ def check(self, user, instance, *args, **kwargs): # pylint: disable=arguments-d
253255
# Libraries with "public read" permissions can be accessed only by course creators
254256
(Attribute('allow_public_read', True) & is_course_creator) |
255257
# Users can access libraries within their authorized scope (via Casbin/role-based permissions)
256-
HasPermissionInContentLibraryScope("view_library") |
258+
HasPermissionInContentLibraryScope(VIEW_LIBRARY) |
257259
# Fallback to: the user must be part of the library's team (legacy permission system)
258260
has_explicit_read_permission_for_library
259261
)

0 commit comments

Comments
 (0)