88
99from crum import get_current_request
1010from django .conf import settings
11- from enterprise .models import EnterpriseCourseEnrollment , EnterpriseCustomerUser
1211from pytz import UTC
1312
1413from common .djangoapps .student .models import CourseEnrollment
1918 DataSharingConsentRequiredAccessError ,
2019 EnrollmentRequiredAccessError ,
2120 IncorrectActiveEnterpriseAccessError ,
22- StartDateEnterpriseLearnerError ,
2321 StartDateError ,
22+ StartDateFiltersError
2423)
2524from lms .djangoapps .courseware .masquerade import get_course_masquerade , is_masquerading_as_student
2625from openedx .features .course_experience import (
2726 COURSE_ENABLE_UNENROLLED_ACCESS_FLAG ,
2827 COURSE_PRE_START_ACCESS_FLAG ,
29- ENFORCE_MASQUERADE_START_DATES ,
28+ ENFORCE_MASQUERADE_START_DATES
3029)
3130from xmodule .course_block import COURSE_VISIBILITY_PUBLIC # pylint: disable=wrong-import-order
3231
@@ -70,60 +69,6 @@ def adjust_start_date(user, days_early_for_beta, start, course_key):
7069 return start
7170
7271
73- def enterprise_learner_enrolled (request , user , course_key ):
74- """
75- Determine if the learner should be redirected to the enterprise learner portal by checking their enterprise
76- memberships/enrollments. If all of the following are true, then we are safe to redirect the learner:
77-
78- * The learner is linked to an enterprise customer,
79- * The enterprise customer has subsidized the learner's enrollment in the requested course,
80- * The enterprise customer has the learner portal enabled.
81-
82- NOTE: This function MUST be called from a view, or it will throw an exception.
83-
84- Args:
85- request (django.http.HttpRequest): The current request being handled. Must not be None.
86- user (User): The requesting enter, potentially an enterprise learner.
87- course_key (str): The requested course to check for enrollment.
88-
89- Returns:
90- bool: True if the learner is enrolled via a linked enterprise customer and can safely be redirected to the
91- enterprise learner dashboard.
92- """
93- from openedx .features .enterprise_support .api import enterprise_customer_from_session_or_learner_data
94-
95- if not user .is_authenticated :
96- return False
97-
98- # enterprise_customer_data is either None (if learner is not linked to any customer) or a serialized
99- # EnterpriseCustomer representing the learner's active linked customer.
100- enterprise_customer_data = enterprise_customer_from_session_or_learner_data (request )
101- learner_portal_enabled = enterprise_customer_data and enterprise_customer_data ["enable_learner_portal" ]
102- if not learner_portal_enabled :
103- return False
104-
105- # Additionally make sure the enterprise learner is actually enrolled in the requested course, subsidized
106- # via the discovered customer.
107- enterprise_enrollments = EnterpriseCourseEnrollment .objects .filter (
108- course_id = course_key ,
109- enterprise_customer_user__user_id = user .id ,
110- enterprise_customer_user__enterprise_customer__uuid = enterprise_customer_data ["uuid" ],
111- )
112- enterprise_enrollment_exists = enterprise_enrollments .exists ()
113- log .info (
114- (
115- "[enterprise_learner_enrolled] Checking for an enterprise enrollment for "
116- "lms_user_id=%s in course_key=%s via enterprise_customer_uuid=%s. "
117- "Exists: %s"
118- ),
119- user .id ,
120- course_key ,
121- enterprise_customer_data ["uuid" ],
122- enterprise_enrollment_exists ,
123- )
124- return enterprise_enrollment_exists
125-
126-
12772def check_start_date (user , days_early_for_beta , start , course_key , display_error_to_user = True , now = None ):
12873 """
12974 Verifies whether the given user is allowed access given the
@@ -133,8 +78,9 @@ def check_start_date(user, days_early_for_beta, start, course_key, display_error
13378 display_error_to_user: If True, display this error to users in the UI.
13479
13580 Returns:
136- AccessResponse: Either ACCESS_GRANTED or StartDateError .
81+ AccessResponse: Either ACCESS_GRANTED, StartDateError, or StartDateFiltersError .
13782 """
83+ from openedx_filters .learning .filters import CourseStartDateValidationFailed
13884 start_dates_disabled = settings .FEATURES ["DISABLE_START_DATES" ]
13985 masquerading_as_student = is_masquerading_as_student (user , course_key )
14086
@@ -155,11 +101,18 @@ def check_start_date(user, days_early_for_beta, start, course_key, display_error
155101 if should_grant_access :
156102 return ACCESS_GRANTED
157103
158- # Before returning a StartDateError, determine if the learner should be redirected to the enterprise learner
159- # portal by returning StartDateEnterpriseLearnerError instead.
104+ # Before returning a StartDateError, give plugins a chance to substitute a more specific access-error payload.
160105 request = get_current_request ()
161- if request and enterprise_learner_enrolled (request , user , course_key ):
162- return StartDateEnterpriseLearnerError (start , display_error_to_user = display_error_to_user )
106+ if request is not None :
107+ error_code , developer_message , user_message , _ , _ = CourseStartDateValidationFailed .run_filter (
108+ error_code = None ,
109+ developer_message = None ,
110+ user_message = None ,
111+ request = request ,
112+ course_key = course_key ,
113+ )
114+ if error_code is not None :
115+ return StartDateFiltersError (error_code , developer_message , user_message )
163116
164117 return StartDateError (start , display_error_to_user = display_error_to_user )
165118
@@ -232,22 +185,20 @@ def check_public_access(course, visibilities):
232185
233186def check_data_sharing_consent (course_id ):
234187 """
235- Grants access if the user is do not need DataSharing consent, otherwise returns data sharing link .
188+ Grants access if no courseware redirect is pending for this course; otherwise returns an access error .
236189
237190 Returns:
238191 AccessResponse: Either ACCESS_GRANTED or DataSharingConsentRequiredAccessError
239192 """
240- from openedx .features .enterprise_support .api import get_enterprise_consent_url
241-
242- consent_url = get_enterprise_consent_url (
243- request = get_current_request (),
244- course_id = str (course_id ),
245- return_to = "courseware" ,
246- enrollment_exists = True ,
247- source = "CoursewareAccess" ,
193+ from openedx_filters .learning .filters import CoursewareViewStarted
194+ request = get_current_request ()
195+ if not request :
196+ return ACCESS_GRANTED
197+ redirect_url , _ , _ = CoursewareViewStarted .run_filter (
198+ redirect_url = None , request = request , course_key = course_id ,
248199 )
249- if consent_url :
250- return DataSharingConsentRequiredAccessError (consent_url = consent_url )
200+ if redirect_url :
201+ return DataSharingConsentRequiredAccessError (consent_url = redirect_url )
251202 return ACCESS_GRANTED
252203
253204
@@ -259,6 +210,7 @@ def check_correct_active_enterprise_customer(user, course_id):
259210 Returns:
260211 AccessResponse: Either ACCESS_GRANTED or IncorrectActiveEnterpriseAccessError
261212 """
213+ from enterprise .models import EnterpriseCourseEnrollment , EnterpriseCustomerUser
262214 enterprise_enrollments = EnterpriseCourseEnrollment .objects .filter (
263215 course_id = course_id , enterprise_customer_user__user_id = user .id
264216 )
0 commit comments