7373from ..constants import ALL_RIGHTS_RESERVED
7474from ..models import ContentLibrary , ContentLibraryPermission
7575from .exceptions import LibraryAlreadyExists , LibraryPermissionIntegrityError
76+ from .permissions import LEGACY_LIB_PERMISSIONS
7677
7778log = logging .getLogger (__name__ )
7879
103104 "publish_changes" ,
104105 "revert_changes" ,
105106 "get_backup_task_status" ,
106- "require_authz_lib_permission" ,
107- "user_has_permission_in_library" ,
107+ "user_has_permission_across_lib_authz_systems" ,
108108]
109109
110110
@@ -236,7 +236,7 @@ def user_can_create_library(user: AbstractUser) -> bool:
236236 Check if the user has permission to create a content library.
237237 """
238238 library_permission = permissions .CAN_CREATE_CONTENT_LIBRARY
239- lib_permission_in_authz = _transform_lib_permission_to_authz_permission (library_permission )
239+ lib_permission_in_authz = _transform_legacy_lib_permission_to_authz_permission (library_permission )
240240 has_perms = user .has_perm (library_permission ) or authz_api .is_user_allowed (
241241 user ,
242242 lib_permission_in_authz ,
@@ -329,15 +329,7 @@ def require_permission_for_library_key(library_key: LibraryLocatorV2, user: User
329329 library_obj = ContentLibrary .objects .get_by_key (library_key )
330330 # obj should be able to read any valid model object but mypy thinks it can only be
331331 # "User | AnonymousUser | None"
332- authz_permission = _transform_lib_permission_to_authz_permission (permission )
333- if not (
334- user .has_perm (permission , obj = library_obj )
335- or authz_api .is_user_allowed (
336- user ,
337- authz_permission ,
338- str (library_key ),
339- )
340- ): # type:ignore[arg-type]
332+ if not user_has_permission_across_lib_authz_systems (user , permission , library_key ):
341333 raise PermissionDenied
342334
343335 return library_obj
@@ -732,9 +724,9 @@ def get_backup_task_status(
732724 return result
733725
734726
735- def _transform_lib_permission_to_authz_permission (permission : str ) -> str :
727+ def _transform_legacy_lib_permission_to_authz_permission (permission : str ) -> str :
736728 """
737- Transform a content library permission to an openedx-authz permission.
729+ Transform a legacy content library permission to an openedx-authz permission.
738730 """
739731 mapping = {
740732 permissions .CAN_CREATE_CONTENT_LIBRARY : 'create_library' ,
@@ -747,45 +739,65 @@ def _transform_lib_permission_to_authz_permission(permission: str) -> str:
747739 return mapping .get (permission , permission )
748740
749741
750- def require_authz_lib_permission (
751- library_key : LibraryLocatorV2 ,
752- user : UserType ,
753- permission : str
754- ) -> ContentLibrary :
742+ def _transform_authz_permission_to_legacy_lib_permission (permission : str ) -> str :
755743 """
756- This function checks the new permissions if needed using openedx-authz and also checks
757- for the old ones to maintain compatibility.
744+ Transform an openedx-authz permission to a legacy content library permission.
758745 """
759- # Check the new permission along with the edit permission (the old one).
760- # This should apply only for publish, and crud over collections.
761- library_obj = ContentLibrary .objects .get_by_key (library_key )
762- if not (
763- user .has_perm (permissions .CAN_EDIT_THIS_CONTENT_LIBRARY , obj = library_obj )
764- or authz_api .is_user_allowed (
765- user ,
766- permission ,
767- str (library_key ),
768- )
769- ):
770- raise PermissionDenied
771- return library_obj
746+ mapping = {
747+ 'publish_library_content' : permissions .CAN_EDIT_THIS_CONTENT_LIBRARY ,
748+ 'create_library_collection' : permissions .CAN_EDIT_THIS_CONTENT_LIBRARY ,
749+ 'edit_library_collection' : permissions .CAN_EDIT_THIS_CONTENT_LIBRARY ,
750+ 'delete_library_collection' : permissions .CAN_EDIT_THIS_CONTENT_LIBRARY ,
751+ }
752+ return mapping .get (permission , permission )
772753
773- def user_has_permission_in_library (
774- library_key : LibraryLocatorV2 ,
754+
755+ def user_has_permission_across_lib_authz_systems (
775756 user : UserType ,
776- permission : str
757+ permission : str ,
758+ library_key : LibraryLocatorV2 ,
777759) -> bool :
778760 """
779- This function checks if the user has the specified permission in the library.
761+ Check whether a user has a given permission on a content library across both the
762+ legacy edx-platform permission system and the newer openedx-authz system.
763+
764+ The provided permission name is normalized to both systems (legacy and authz), and
765+ authorization is granted if either:
766+ - the user holds the legacy object-level permission on the ContentLibrary instance, or
767+ - the openedx-authz API allows the user for the corresponding permission on the library.
768+
769+ Args:
770+ user: The Django user (or user-like object) to check.
771+ permission: The permission identifier (either a legacy codename or an openedx-authz name).
772+ library_key: The LibraryLocatorV2 identifying the target content library.
773+
774+ Returns:
775+ bool: True if the user is authorized by either system; otherwise False.
776+
777+ Raises:
778+ ContentLibrary.DoesNotExist: If a library does not exist for the given key.
780779 """
781780 library_obj = ContentLibrary .objects .get_by_key (library_key )
782- # Identify if the permission is old or for authz (in this moment we are using old, for the serializer)
783- permission_in_authz = _transform_lib_permission_to_authz_permission (permission )
781+ if _is_legacy_permission (permission ):
782+ legacy_permission = permission
783+ authz_permission = _transform_legacy_lib_permission_to_authz_permission (permission )
784+ else :
785+ authz_permission = permission
786+ legacy_permission = _transform_authz_permission_to_legacy_lib_permission (permission )
784787 return (
785- user .has_perm (permission , obj = library_obj )
788+ # Check both the legacy and the new openedx-authz permissions
789+ user .has_perm (perm = legacy_permission , obj = library_obj )
786790 or authz_api .is_user_allowed (
787791 user ,
788- permission_in_authz ,
792+ authz_permission ,
789793 str (library_key ),
790794 )
791- )
795+ )
796+
797+
798+ def _is_legacy_permission (permission : str ) -> bool :
799+ """
800+ Determine if the specified library permission is part of the legacy
801+ or the new openedx-authz system.
802+ """
803+ return permission in LEGACY_LIB_PERMISSIONS
0 commit comments