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