|
2 | 2 | Helper functions for the Consent application. |
3 | 3 | """ |
4 | 4 |
|
| 5 | +import logging |
| 6 | +from urllib.parse import urlencode |
| 7 | + |
| 8 | +from edx_django_utils.cache import TieredCache |
| 9 | + |
5 | 10 | from django.apps import apps |
| 11 | +from django.conf import settings |
| 12 | +from django.contrib.sites.models import Site |
| 13 | +from django.urls import reverse |
6 | 14 |
|
7 | 15 | from consent.models import ProxyDataSharingConsent |
8 | 16 | from enterprise.api_client.discovery import get_course_catalog_api_service_client |
9 | | -from enterprise.utils import get_enterprise_customer |
| 17 | +from enterprise.utils import get_active_enterprise_customer_user, get_enterprise_customer |
| 18 | + |
| 19 | +# ENT-11576: CONSENT_FAILED_PARAMETER, ConsentApiClient, enterprise_customer_uuid_for_request, |
| 20 | +# and get_data_consent_share_cache_key will be migrated from the platform's enterprise_support |
| 21 | +# module into edx-enterprise, eliminating these cross-boundary imports. |
| 22 | +try: |
| 23 | + from openedx.features.enterprise_support.api import ( |
| 24 | + CONSENT_FAILED_PARAMETER, |
| 25 | + ConsentApiClient, |
| 26 | + enterprise_customer_uuid_for_request, |
| 27 | + ) |
| 28 | + from openedx.features.enterprise_support.utils import get_data_consent_share_cache_key |
| 29 | +except ImportError: |
| 30 | + CONSENT_FAILED_PARAMETER = 'consent_failed' |
| 31 | + ConsentApiClient = None |
| 32 | + enterprise_customer_uuid_for_request = None |
| 33 | + get_data_consent_share_cache_key = None |
| 34 | + |
| 35 | +LOGGER = logging.getLogger(__name__) |
| 36 | + |
| 37 | + |
| 38 | +def consent_needed_for_course(request, user, course_id, enrollment_exists=False): |
| 39 | + """ |
| 40 | + Determine whether ``user`` must grant data-sharing consent before accessing ``course_id``. |
| 41 | + """ |
| 42 | + # Consent is never required if the enterprise feature is disabled. |
| 43 | + if not getattr(settings, 'ENABLE_ENTERPRISE_INTEGRATION', False): |
| 44 | + return False |
| 45 | + |
| 46 | + LOGGER.info( |
| 47 | + "[ENTERPRISE DSC] Determining if user [%s] must consent to data sharing for course [%s]", |
| 48 | + user.username, course_id, |
| 49 | + ) |
| 50 | + |
| 51 | + active_enterprise_learner_info = get_active_enterprise_customer_user(user) |
| 52 | + if not active_enterprise_learner_info: |
| 53 | + LOGGER.info( |
| 54 | + "[ENTERPRISE DSC] Consent from user [%s] is not needed for course [%s]. " |
| 55 | + "The user is not linked to an enterprise.", |
| 56 | + user.username, course_id, |
| 57 | + ) |
| 58 | + return False |
| 59 | + |
| 60 | + active_enterprise_customer = active_enterprise_learner_info.enterprise_customer |
| 61 | + |
| 62 | + consent_cache_key = get_data_consent_share_cache_key( |
| 63 | + user.id, course_id, str(active_enterprise_customer.uuid), |
| 64 | + ) |
| 65 | + cached = TieredCache.get_cached_response(consent_cache_key) |
| 66 | + if cached.is_found and cached.value == 0: |
| 67 | + LOGGER.info( |
| 68 | + "[ENTERPRISE DSC] Consent from user [%s] is not needed for course [%s]. " |
| 69 | + "The DSC cache was checked and the value was 0.", |
| 70 | + user.username, course_id, |
| 71 | + ) |
| 72 | + return False |
| 73 | + |
| 74 | + if not active_enterprise_customer.enable_data_sharing_consent: |
| 75 | + LOGGER.info( |
| 76 | + "[ENTERPRISE DSC] DSC is disabled for enterprise customer [%s]. " |
| 77 | + "Consent from user [%s] is not needed for course [%s]", |
| 78 | + active_enterprise_customer.slug, user.username, course_id, |
| 79 | + ) |
| 80 | + TieredCache.set_all_tiers(consent_cache_key, 0, settings.DATA_CONSENT_SHARE_CACHE_TIMEOUT) |
| 81 | + return False |
| 82 | + |
| 83 | + current_enterprise_uuid = enterprise_customer_uuid_for_request(request) |
| 84 | + if str(current_enterprise_uuid) != str(active_enterprise_customer.uuid): |
| 85 | + LOGGER.info( |
| 86 | + '[ENTERPRISE DSC] Enterprise mismatch. USER: [%s], RequestEnterprise: [%s], ' |
| 87 | + 'LearnerEnterprise: [%s]', |
| 88 | + user.username, current_enterprise_uuid, active_enterprise_customer.uuid, |
| 89 | + ) |
| 90 | + TieredCache.set_all_tiers(consent_cache_key, 0, settings.DATA_CONSENT_SHARE_CACHE_TIMEOUT) |
| 91 | + return False |
| 92 | + |
| 93 | + enterprise_domain = Site.objects.get(domain=active_enterprise_customer.site.domain) |
| 94 | + if enterprise_domain != request.site: |
| 95 | + LOGGER.info( |
| 96 | + '[ENTERPRISE DSC] Site mismatch. USER: [%s], RequestSite: [%s], ' |
| 97 | + 'LearnerEnterpriseDomain: [%s]', |
| 98 | + user.username, request.site, enterprise_domain, |
| 99 | + ) |
| 100 | + TieredCache.set_all_tiers(consent_cache_key, 0, settings.DATA_CONSENT_SHARE_CACHE_TIMEOUT) |
| 101 | + return False |
| 102 | + |
| 103 | + client = ConsentApiClient(user=request.user) |
| 104 | + consent_required = client.consent_required( |
| 105 | + username=user.username, |
| 106 | + course_id=course_id, |
| 107 | + enterprise_customer_uuid=current_enterprise_uuid, |
| 108 | + enrollment_exists=enrollment_exists, |
| 109 | + ) |
| 110 | + if not consent_required: |
| 111 | + LOGGER.info( |
| 112 | + "[ENTERPRISE DSC] Consent from user [%s] is not needed for course [%s]. " |
| 113 | + "The user's current enterprise does not require data sharing consent.", |
| 114 | + user.username, course_id, |
| 115 | + ) |
| 116 | + TieredCache.set_all_tiers(consent_cache_key, 0, settings.DATA_CONSENT_SHARE_CACHE_TIMEOUT) |
| 117 | + return False |
| 118 | + |
| 119 | + LOGGER.info( |
| 120 | + "[ENTERPRISE DSC] Consent from user [%s] is needed for course [%s]. " |
| 121 | + "The user's current enterprise requires data sharing consent, and it has not been given.", |
| 122 | + user.username, course_id, |
| 123 | + ) |
| 124 | + return True |
| 125 | + |
| 126 | + |
| 127 | +def get_enterprise_consent_url(request, course_id, user=None, return_to=None, enrollment_exists=False, source='lms'): |
| 128 | + """ |
| 129 | + Build a URL to redirect the user to the data-sharing consent page for a specific course. |
| 130 | +
|
| 131 | + Arguments: |
| 132 | + request: Django request object. |
| 133 | + course_id: Course key/identifier string. |
| 134 | + user: user to check for consent. If None, uses ``request.user``. |
| 135 | + return_to: url name for the page to return to after consent is granted; defaults to |
| 136 | + ``request.path``. |
| 137 | + enrollment_exists: forwarded to ``consent_needed_for_course``. |
| 138 | + source: opaque string identifying the caller, recorded on the consent URL. |
| 139 | + """ |
| 140 | + user = user or request.user |
| 141 | + LOGGER.info( |
| 142 | + 'Getting enterprise consent url for user [%s] and course [%s].', |
| 143 | + user.username, |
| 144 | + course_id, |
| 145 | + ) |
| 146 | + if not consent_needed_for_course(request, user, course_id, enrollment_exists=enrollment_exists): |
| 147 | + return None |
| 148 | + return_path = request.path if return_to is None else reverse(return_to, args=(course_id,)) |
| 149 | + url_params = { |
| 150 | + 'enterprise_customer_uuid': enterprise_customer_uuid_for_request(request), |
| 151 | + 'course_id': course_id, |
| 152 | + 'source': source, |
| 153 | + 'next': request.build_absolute_uri(return_path), |
| 154 | + 'failure_url': request.build_absolute_uri( |
| 155 | + reverse('dashboard') + '?' + urlencode({CONSENT_FAILED_PARAMETER: course_id}) |
| 156 | + ), |
| 157 | + } |
| 158 | + full_url = reverse('grant_data_sharing_permissions') + '?' + urlencode(url_params) |
| 159 | + LOGGER.info('Redirecting to %s to complete data sharing consent', full_url) |
| 160 | + return full_url |
10 | 161 |
|
11 | 162 |
|
12 | 163 | def get_data_sharing_consent(username, enterprise_customer_uuid, course_id=None, program_uuid=None): |
|
0 commit comments