|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +""" |
| 4 | +Backend for the CourseKey validations that works under the open-release/maple.master tag |
| 5 | +""" |
| 6 | +# pylint: disable=import-error, protected-access |
| 7 | +from __future__ import absolute_import, unicode_literals |
| 8 | + |
| 9 | +from django.conf import settings |
| 10 | +from opaque_keys import InvalidKeyError |
| 11 | +from opaque_keys.edx.keys import CourseKey |
| 12 | +from rest_framework.serializers import ValidationError |
| 13 | + |
| 14 | +try: |
| 15 | + from openedx.core.djangoapps.site_configuration.helpers import get_all_orgs, get_current_site_orgs |
| 16 | +except ImportError: |
| 17 | + get_all_orgs, get_current_site_orgs = object, object # pylint: disable=invalid-name |
| 18 | + |
| 19 | + |
| 20 | +def get_valid_course_key(course_id): |
| 21 | + """ |
| 22 | + Return the CourseKey if the course_id is valid |
| 23 | + """ |
| 24 | + try: |
| 25 | + return CourseKey.from_string(course_id) |
| 26 | + except InvalidKeyError: |
| 27 | + raise ValidationError(f"Invalid course_id {course_id}") from InvalidKeyError |
| 28 | + |
| 29 | + |
| 30 | +def validate_org(course_id): |
| 31 | + """ |
| 32 | + Validate the course organization against all possible orgs for the site |
| 33 | +
|
| 34 | + To determine if the Org is valid we must look at 3 things |
| 35 | + 1 Orgs in the current site |
| 36 | + 2 Orgs in other sites |
| 37 | + 3 flag EOX_CORE_USER_ENABLE_MULTI_TENANCY |
| 38 | + """ |
| 39 | + |
| 40 | + if not settings.EOX_CORE_USER_ENABLE_MULTI_TENANCY: |
| 41 | + return True |
| 42 | + |
| 43 | + course_key = get_valid_course_key(course_id) |
| 44 | + course_org_filter = getattr(settings, "course_org_filter", []) |
| 45 | + course_org_filter = course_org_filter if isinstance(course_org_filter, list) else [course_org_filter] |
| 46 | + current_site_orgs = get_current_site_orgs() or course_org_filter or [] |
| 47 | + |
| 48 | + if not current_site_orgs: # pylint: disable=no-else-return |
| 49 | + if course_key.org in get_all_orgs(): |
| 50 | + return False |
| 51 | + return True |
| 52 | + else: |
| 53 | + return course_key.org in current_site_orgs |
0 commit comments