-
Notifications
You must be signed in to change notification settings - Fork 4.3k
grant xblock edit access via authz edit_course_content permission #39050
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jacobo-dominguez-wgu
wants to merge
1
commit into
openedx:master
Choose a base branch
from
WGU-Open-edX:course-editor-content
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+89
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -585,7 +585,19 @@ class TestXBlockViewHandlerHeaderActionsAuthz(ItemTest): | |
| AUTHZ_PERMISSION_PATH = ( | ||
| "cms.djangoapps.contentstore.views.block.user_has_course_permission" | ||
| ) | ||
| # Patch has_studio_write_access at the block.py binding so the legacy | ||
| # authoring path can be toggled independently of the authz path. | ||
| STUDIO_WRITE_ACCESS_PATH = ( | ||
| "cms.djangoapps.contentstore.views.block.has_studio_write_access" | ||
| ) | ||
| HEADER_ACTIONS_DIV = 'class="header-actions"' | ||
| # The component (content) "Edit" button is rendered only when | ||
| # ``not show_inline and can_edit`` in studio_xblock_wrapper.html. Match on | ||
| # its full class string so this does NOT collide with the separate | ||
| # "Edit Title" button (``title-edit-button``), which is rendered on the | ||
| # opposite condition (``can_edit_title and not can_edit``) and would | ||
| # otherwise match a bare ``edit-button`` substring. | ||
| CONTENT_EDIT_BUTTON = 'class="btn-default edit-button action-button"' | ||
|
|
||
| @staticmethod | ||
| def _permission_side_effect(*, can_edit_course_content, can_manage_tags=True): | ||
|
|
@@ -633,6 +645,35 @@ def _get_container_preview_html(self): | |
| assert resp.status_code == 200 | ||
| return json.loads(resp.content.decode("utf-8"))["html"] | ||
|
|
||
| def _get_leaf_component_preview_html(self): | ||
| """ | ||
| Return the rendered HTML for a leaf ``html`` component card. | ||
|
|
||
| The component (content) "Edit" button is gated on | ||
| ``not show_inline and can_edit`` in studio_xblock_wrapper.html, where | ||
| ``show_inline = xblock.has_children and not xblock_url``. A vertical has | ||
| children, so its card is rendered inline and never shows that edit | ||
| button regardless of ``can_edit``. We therefore create a leaf ``html`` | ||
| component (no children -> ``show_inline`` is False) inside a vertical and | ||
| request ``container_child_preview`` for it, which is the branch that the | ||
| ``can_edit`` fix actually controls. | ||
| """ | ||
| parent_usage_key = self._create_vertical() | ||
| resp = self.create_xblock( | ||
| parent_usage_key=parent_usage_key, category="html" | ||
| ) | ||
| self.assertEqual(resp.status_code, 200) # noqa: PT009 | ||
| child_usage_key = self.response_usage_key(resp) | ||
|
|
||
| preview_url = reverse_usage_url( | ||
| "xblock_view_handler", | ||
| child_usage_key, | ||
| {"view_name": "container_child_preview"}, | ||
| ) | ||
| resp = self.client.get(preview_url, HTTP_ACCEPT="application/json") | ||
| self.assertEqual(resp.status_code, 200) # noqa: PT009 | ||
| return json.loads(resp.content.decode("utf-8"))["html"] | ||
|
|
||
| def test_header_actions_visible_when_flag_off(self): | ||
| """ | ||
| When enable_authz_course_authoring is off, is_authz_authoring_enabled | ||
|
|
@@ -678,6 +719,50 @@ def test_header_actions_hidden_when_flag_on_and_user_denied(self): | |
|
|
||
| assert self.HEADER_ACTIONS_DIV not in html | ||
|
|
||
| def test_can_edit_true_via_authz_without_legacy_write_access(self): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we include the legacy testing path? Can edit with legacy write access or is this tested elsewhere? |
||
| """ | ||
| Regression test for the ``can_edit`` fix in xblock_view_handler. | ||
|
|
||
| ``can_edit`` is computed as:: | ||
|
|
||
| has_studio_write_access(...) or ( | ||
| is_authz_authoring_enabled and authz_can_edit_course_content | ||
| ) | ||
|
|
||
| A user granted courses.edit_course_content through the authz rollout may | ||
| not hold legacy studio write access. Before the fix ``can_edit`` was | ||
| derived solely from ``has_studio_write_access`` and such a user would | ||
| lose the per-block "Edit" button. With the flag on and the permission | ||
| granted, ``can_edit`` must be True even when legacy write access is | ||
| False, so the edit button must be rendered. | ||
| """ | ||
| with patch(self.STUDIO_WRITE_ACCESS_PATH, return_value=False), \ | ||
| patch(self.AUTHZ_FLAG_PATH, return_value=True), \ | ||
| patch( | ||
| self.AUTHZ_PERMISSION_PATH, | ||
| side_effect=self._permission_side_effect(can_edit_course_content=True), | ||
| ): | ||
| html = self._get_leaf_component_preview_html() | ||
|
|
||
| assert self.CONTENT_EDIT_BUTTON in html | ||
|
|
||
| def test_can_edit_false_without_legacy_write_access_or_authz_permission(self): | ||
| """ | ||
| When the user lacks legacy studio write access and, with the authz flag | ||
| on, is not granted courses.edit_course_content, both operands of the | ||
| ``can_edit`` expression are False. ``can_edit`` must therefore be False | ||
| and the per-block "Edit" button must be absent. | ||
| """ | ||
| with patch(self.STUDIO_WRITE_ACCESS_PATH, return_value=False), \ | ||
| patch(self.AUTHZ_FLAG_PATH, return_value=True), \ | ||
| patch( | ||
| self.AUTHZ_PERMISSION_PATH, | ||
| side_effect=self._permission_side_effect(can_edit_course_content=False), | ||
| ): | ||
| html = self._get_leaf_component_preview_html() | ||
|
|
||
| assert self.CONTENT_EDIT_BUTTON not in html | ||
|
|
||
|
|
||
| class TestXBlockViewHandlerManageTagsAuthz(ItemTest): | ||
| """ | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem with this
oris that legacy permissions are always being validated even if AuthZ is enabled, and we don't want that.And now that I'm checking the code again, one suggestion to simplify this further:
is_authz_authoring_enableddoesn't need to reach the template at all.can_editandcan_manage_tagscould each resolve their own "flag is off" default internally (e.g.can_editdelegating entirely touser_has_course_permission(..., legacy_permission=WRITE), and the tags check returning True when the flag is off, since tag management has no legacy-permission concept). That way only two already-final booleans (can_edit,can_manage_tags) need to reachstudio_xblock_wrapper.html.This also fixes another bug: "Manage Tags" is nested inside the div gated on
edit_course_content, so a role granted only manage_tags (no edit access) could never reach it. The div should open oncan_editorcan_manage_tagsinstead.What do you think?