3333 COURSES_EDIT_FILES ,
3434)
3535from openedx_filters .content_authoring .filters import LMSPageURLRequested
36+ from openedx .core .toggles import enable_authz_course_authoring
3637from xmodule .contentstore .content import StaticContent # lint-amnesty, pylint: disable=wrong-import-order
3738from xmodule .contentstore .django import contentstore # lint-amnesty, pylint: disable=wrong-import-order
3839from xmodule .exceptions import NotFoundError # lint-amnesty, pylint: disable=wrong-import-order
@@ -80,7 +81,32 @@ def handle_assets(request, course_key_string=None, asset_key_string=None):
8081 json: delete an asset
8182 '''
8283 course_key = CourseKey .from_string (course_key_string )
83- # Everyone should have at least view access to proceedd.
84+ # Enforce file permissions.
85+ _authz_enforce_file_permissions (request , course_key )
86+
87+ response_format = get_response_format (request )
88+ if request_response_format_is_json (request , response_format ):
89+ if request .method == 'GET' :
90+ return _assets_json (request , course_key )
91+
92+ # POST, PUT, DELETE typically invoke this
93+ asset_key = AssetKey .from_string (asset_key_string ) if asset_key_string else None
94+ return update_asset (request , course_key , asset_key )
95+
96+ elif request .method == 'GET' : # assume html
97+ return _asset_index (request , course_key )
98+
99+ return HttpResponseNotFound ()
100+
101+ def _authz_enforce_file_permissions (request , course_key ):
102+ """
103+ Enforce permissions for file operations in asset handler.
104+ When the authz.enable_course_authoring flag is enabled for the specified course,
105+ This function enforces the appropiate file permission depending on request content.
106+ When the flag is disabled, it enforces the legacy has_studio_write_access permission.
107+ """
108+ # Enforce permisison to view files.
109+ # This is the minimum permission needed for handling assets.
84110 if not user_has_course_permission (
85111 request .user ,
86112 COURSES_VIEW_FILES .identifier ,
@@ -89,11 +115,7 @@ def handle_assets(request, course_key_string=None, asset_key_string=None):
89115 ):
90116 raise PermissionDenied ()
91117
92- response_format = get_response_format (request )
93- if request_response_format_is_json (request , response_format ):
94- if request .method == 'GET' :
95- return _assets_json (request , course_key )
96-
118+ if enable_authz_course_authoring (course_key ):
97119 # Check create, edit and delete permissions for AuthZ-enabled courses.
98120 # When we get a PUT or POST that includes a file, it's a create.
99121 if request .method in ('PUT' , 'POST' ) and 'file' in request .FILES and not user_has_course_permission (
@@ -120,16 +142,6 @@ def handle_assets(request, course_key_string=None, asset_key_string=None):
120142 ):
121143 raise PermissionDenied ()
122144
123- # POST, PUT, DELETE typically invoke this
124- asset_key = AssetKey .from_string (asset_key_string ) if asset_key_string else None
125- return update_asset (request , course_key , asset_key )
126-
127- elif request .method == 'GET' : # assume html
128- return _asset_index (request , course_key )
129-
130- return HttpResponseNotFound ()
131-
132-
133145def get_asset_usage_path_json (request , course_key , asset_key_string ):
134146 """
135147 Get a list of units with ancestors that use given asset.
0 commit comments