From 18e3516b36aef94da9d259bf1574103764ceb300 Mon Sep 17 00:00:00 2001 From: Ahtisham Shahid Date: Wed, 30 Mar 2022 15:34:09 +0500 Subject: [PATCH] feat: Added permissions for course staff to discussion MFE (#30143) --- lms/djangoapps/discussion/tests/test_views.py | 2 +- lms/djangoapps/discussion/views.py | 20 +++++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/lms/djangoapps/discussion/tests/test_views.py b/lms/djangoapps/discussion/tests/test_views.py index 0cec540d3c..a44bd19775 100644 --- a/lms/djangoapps/discussion/tests/test_views.py +++ b/lms/djangoapps/discussion/tests/test_views.py @@ -2287,7 +2287,7 @@ class ForumMFETestCase(ForumsEnableMixin, SharedModuleStoreTestCase): self.staff_user = AdminFactory.create() CourseEnrollmentFactory.create(user=self.user, course_id=self.course.id) - @ddt.data(*itertools.product(("http://test.url", None), (True, False), (True, False))) + @ddt.data(*itertools.product(("http://test.url", None), (True, False), (True, True))) @ddt.unpack def test_staff_user(self, mfe_url, toggle_enabled, is_staff): """ diff --git a/lms/djangoapps/discussion/views.py b/lms/djangoapps/discussion/views.py index 84cf8ce119..91a41fd06e 100644 --- a/lms/djangoapps/discussion/views.py +++ b/lms/djangoapps/discussion/views.py @@ -61,6 +61,7 @@ from openedx.core.djangoapps.django_comment_common.models import CourseDiscussio from openedx.core.djangoapps.django_comment_common.utils import ThreadContext from openedx.core.djangoapps.plugin_api.views import EdxFragmentView from openedx.features.course_duration_limits.access import generate_course_expired_fragment +from common.djangoapps.student.roles import CourseInstructorRole, CourseStaffRole, GlobalStaff User = get_user_model() log = logging.getLogger("edx.discussions") @@ -703,7 +704,10 @@ def followed_threads(request, course_key, user_id): raise Http404 # lint-amnesty, pylint: disable=raise-missing-from -def _discussions_mfe_context(query_params: Dict, course_key: CourseKey, legacy_only_view=False) -> Optional[Dict]: +def _discussions_mfe_context(query_params: Dict, + course_key: CourseKey, + is_educator_or_staff=False, + legacy_only_view=False) -> Optional[Dict]: """ Returns the context for rendering the MFE banner and MFE. @@ -723,7 +727,7 @@ def _discussions_mfe_context(query_params: Dict, course_key: CourseKey, legacy_o # and if the current view isn't only that's only supported by the legacy view show_mfe = ( query_params.get("discussions_experience", "").lower() != "legacy" - and discussions_mfe_enabled + and (discussions_mfe_enabled and is_educator_or_staff) and not legacy_only_view ) forum_url = reverse("forum_form_discussion", args=[course_key]) @@ -733,11 +737,18 @@ def _discussions_mfe_context(query_params: Dict, course_key: CourseKey, legacy_o "mfe_url": f"{forum_url}?discussions_experience=new", "share_feedback_url": settings.DISCUSSIONS_MFE_FEEDBACK_URL, "course_key": course_key, - "show_banner": discussions_mfe_enabled, + "show_banner": (discussions_mfe_enabled and is_educator_or_staff), "discussions_mfe_url": mfe_url, } +def is_course_staff(course_key: CourseKey, user: User): + """ + Check if user has course instructor or course staff role. + """ + return CourseInstructorRole(course_key).has_user(user) or CourseStaffRole(course_key).has_user(user) + + class DiscussionBoardFragmentView(EdxFragmentView): """ Component implementation of the discussion board. @@ -767,7 +778,8 @@ class DiscussionBoardFragmentView(EdxFragmentView): course_key = CourseKey.from_string(course_id) # Force using the legacy view if a user profile is requested or the URL contains a specific topic or thread force_legacy_view = (profile_page_context or thread_id or discussion_id) - mfe_context = _discussions_mfe_context(request.GET, course_key, force_legacy_view) + is_educator_or_staff = is_course_staff(course_key, request.user) or GlobalStaff().has_user(request.user) + mfe_context = _discussions_mfe_context(request.GET, course_key, is_educator_or_staff, force_legacy_view) if mfe_context["show_mfe"]: fragment = Fragment(render_to_string('discussion/discussion_mfe_embed.html', mfe_context)) fragment.add_css(